如何解决Feign Client Multipart 文件上传为资源
我正在尝试使用 feign 客户端将文件上传到 REST api。如下所示,工作正常。
@PostMapping(value = "/test/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResponseEntity<String> upload(@RequestPart(value = "data") MultipartFile zipFile);
为了为更新创建多部分文件,我这样做,
public MultipartFile createFile(){
String zipFilePath = "some/path/to/file";
File file = new File(zipFilePath);
FileItem fileItem = new DiskFileItem(FIELD_NAME,Files.probeContentType(file.toPath()),false,file.getName(),(int) file.length(),file.getParentFile());
try (InputStream input = new FileInputStream(file); OutputStream output = fileItem.getOutputStream()){
IOUtils.copy(input,output);
}
return new CommonsMultipartFile(fileItem);
}
在调用 feign 客户端时,正在使用通过上述方法创建的多部分文件。不是像上面那样创建 CommonsMultipartFile
并将其加载到内存中,我决定将它加载到 Resource
如下,
public Resource createFile(){
String zipFilePath = "some/path/to/file";
Resource resource = new FileSystemResource(zipFilePath );
return resource;
}
并像这样更改了 feign client 方法,但它在 feign client 中不起作用。
@PostMapping(value = "/test/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResponseEntity<String> upload(@RequestPart(value = "data") Resource zipFile);
我从 Status: 409 CONFLICT. Body: Nothing to upload
端点的响应中收到 /test/upload
错误。但是后来我尝试使用休息模板上传资源,该模板对于作为文件数据的资源工作正常,
public ResponseEntity<String> uploadFile(Resource file){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String,Object> parts = new LinkedMultiValueMap<>();
HttpEntity<Resource> fileResource = new HttpEntity<>(file);
parts.add("data",fileResource);
HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<>(parts,headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url,HttpMethod.POST,request,String.class);
}
是什么原因在feign client中传递一个Resource类型作为部分数据不起作用。我使用的是 spring spring boot 2.x。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。