springboot 使用RestTemplate调用post api上传文件
InputStreamResource
教程
Spring官方文档介绍,仅当没有其他特定的资源实现适用时才应使用,在可能的情况下更推荐使用 ByteArrayResource或任何基于文件的Resource实现。
InputStreamResource是Spring中标准的输入流资源对象,读取常见的字节数据,使用InputStreamResource对象将接收的文件内容转成字节流的方式和ByteArrayResource类似,也需要覆写对应的getFilename()
和contentLength()
方法,以此来存放文件的名称和大小信息。
Contoller:
@ResponseBody @ResponseStatus(HttpStatus.OK) @PostMapping(value = "/uploadFile") @ApiOperation(value = "Upload zip file ") public JSONObject uploadFile(@Param("file") MultipartFile multipartFile,Integer parames1, String parames2) { JSONObject result = new JSONObject(); try { String uploadZipFileUrl = "http://xx.com";
String url=uploadZipFileUrl +"?Name="+parames1+"&BatchValue="+parames2; //拼接url参数
// 设置请求body
JSONObject responseData= postFormData(multipartFile, url);
return responseData;
} catch (Exception e) { logger.info("Invoke api failed!"); result.put("msg", "Invoke api failed!");
result.put("status", false);
return result;
}
}
Server:
public JSONObject postFormData(MultipartFile file, String url) throws IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); MultiValueMap<String, Object> params = new LinkedMultiValueMap<String, Object>(); InputStreamResource inputStreamResource = new InputStreamResource(file.getInputStream()){ @Override public String getFilename() { return file.getOriginalFilename(); } @Override public long contentLength() { return file.getSize(); } }; params.add("file",inputStreamResource); HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(params, headers); //String urlParams = url + "?Name=xx1&BatchValue=xx2"; ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class); //ResponseEntity<String> responseEntity = restTemplate.exchange(urlHeader+modelUrl, HttpMethod.POST, httpEntity, String.class); return JSON.parseObject(responseEntity.getBody()); }
案例二: Controller:
@RequestMapping("/upload") public String upload(MultipartFile file, String id) throws IOException { RestTemplate restTemplate = new RestTemplate(); String url = "http://localhost:8080/excel/upload"; MultiValueMap<String,Object> params = new LinkedMultiValueMap<>(); InputStreamResource inputStreamResource = new InputStreamResource(file.getInputStream()){ @Override public String getFilename() { return file.getOriginalFilename(); } @Override public long contentLength() { return file.getSize(); } }; params.add("file", inputStreamResource); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); HttpEntity<MultiValueMap<String,Object>> requestEntity = new HttpEntity<>(params, headers); String result = restTemplate.postForObject(url,requestEntity ,String.class); return result; }
PS:在调用api 时遇到一个报错:
文件上传遇到org.springframework.web.multipart.MultipartException:Maximum upload size exceeded;解决方法:
在使用springboot进行文件上传时,因为springboot对上传的文件大小有默认大小不能1MB,超过1MB会出现这个错误:org.springframework.web.multipart.MultipartException:Maximum upload size exceeded;。
解决方法:配置第一行是设置单个文件的大小,第二行是设置单次请求的文件的总大小,配置方法根据SpringBoot版本不同而不同。
1.Spring Boot 1.3 或之前的版本,配置:
multipart.maxFileSize = 100Mb multipart.maxRequestSize = 200Mb
2.Spring Boot 1.4 版本后配置:
spring.http.multipart.maxFileSize = 100Mb spring.http.multipart.maxRequestSize = 200Mb
Spring Boot 2.0 之后的版本配置修改为:
#单位由Mb改为MB了 spring.servlet.multipart.max-file-size = 100MB spring.servlet.multipart.max-request-size = 200MB #如果想要不限制文件上传的大小,那么就把两个值都设置为-1
就可以正常的上传了!