使用RestTemplate上传MultipartFile文件给远程接口
程序员文章站
2022-06-26 08:05:48
...
参考于:https://blog.csdn.net/weixin_30357231/article/details/97925465
方法头:
public AjaxJson fileUpload(@RequestPart(value = "file") MultipartFile file) throws Exception
MultiValueMap request = new LinkedMultiValueMap(1);
ByteArrayResource is = new ByteArrayResource(file.getBytes()) {
@Override
public String getFilename() {
return file.getOriginalFilename();
}
};
request.add("file", is);
RestTemplate restTemplate = new RestTemplate();
AjaxJson ajaxJson = null;
try {
ajaxJson = restTemplate.postForObject(url, request, AjaxJson.class);
} catch (RestClientException e) {
logger.error("文件上传异常");
e.printStackTrace();
return AjaxJson.returnExceptionInfo("文件上传异常");
}