博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot RestTemplate文件上传
阅读量:5030 次
发布时间:2019-06-12

本文共 2506 字,大约阅读时间需要 8 分钟。

@ResponseBody  @RequestMapping(value = "/upload.do", method = RequestMethod.POST)  public String upload(String fileName, MultipartFile jarFile) {      // 下面是测试代码      System.out.println(fileName);      String originalFilename = jarFile.getOriginalFilename();      System.out.println(originalFilename);      try {          String string = new String(jarFile.getBytes(), "UTF-8");          System.out.println(string);      } catch (UnsupportedEncodingException e) {          e.printStackTrace();      } catch (IOException e) {          e.printStackTrace();      }      // TODO 处理文件内容...      return "OK";  }
@Test  public void testUpload() throws Exception {      String url = "http://127.0.0.1:8080/test/upload.do";      String filePath = "C:\\Users\\MikanMu\\Desktop\\test.txt";        RestTemplate rest = new RestTemplate();      FileSystemResource resource = new FileSystemResource(new File(filePath));      MultiValueMap
param = new LinkedMultiValueMap<>(); param.add("jarFile", resource); param.add("fileName", "test.txt"); String string = rest.postForObject(url, param, String.class); System.out.println(string); }
String string = rest.postForObject(url, param, String.class);  可以换成以下方式HttpEntity
> httpEntity = new HttpEntity
>(param); ResponseEntity
responseEntity = rest.exchange(url, HttpMethod.POST, httpEntity, String.class); System.out.println(responseEntity.getBody());

 

    
@PostMapping(value = "/uploadFile")    @ResponseBody    @ApiOperation(value = "上传附件", notes = "返回结果,SUCCESS:200,FAILED:500", httpMethod = "POST")    public String upload(@ApiParam MultipartFile importFile) throws Exception {        String suffix = importFile.getOriginalFilename().substring(importFile.getOriginalFilename().lastIndexOf("."));        String showUrl = aLiYunOssService.upload(importFile.getBytes(), suffix);        return showUrl;    }

 

MultiValueMap
parts = new LinkedMultiValueMap
();ByteArrayResource arrayResource = new ByteArrayResource(getBytes(),"test.jpg"); parts.add("importFile", arrayResource); HttpEntity
> httpEntity2 = new HttpEntity
>(parts); ResponseEntity
responseEntity2 = rest.exchange(url, HttpMethod.POST, httpEntity2, String.class); System.out.println("返回地址1==="+responseEntity2.getBody());

 

转载于:https://www.cnblogs.com/yy123/p/6956720.html

你可能感兴趣的文章
建造者模式(屌丝专用)
查看>>
UVALive 4730 Kingdom +段树和支票托收
查看>>
[APIO2010]特别行动队
查看>>
SpringBoot 集成ehcache
查看>>
初步swift语言学习笔记2(可选类型?和隐式可选类型!)
查看>>
Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点
查看>>
在Vs2012 中使用SQL Server 2012 Express LocalDB打开Sqlserver2012数据库
查看>>
Excel催化剂开源第42波-与金融大数据TuShare对接实现零门槛零代码获取数据
查看>>
【转】常用的latex宏包
查看>>
[TMS320C674x] 一、GPIO认识
查看>>
酷狗的皮肤文件存放在哪
查看>>
C++的引用
查看>>
T-SQL查询进阶--深入浅出视图
查看>>
MapKeyboard 键盘按键映射 机械革命S1 Pro-02
查看>>
Android读取url图片保存及文件读取
查看>>
完整ASP.Net Excel导入
查看>>
判断CPU大小端示例代码
查看>>
ARTS打卡第13周
查看>>
循环队列的运用---求K阶斐波那契序列
查看>>
pta 编程题14 Huffman Codes
查看>>