SpringRestTemplate后上载多个文件
假设我有一个端点,如下所示:SpringRestTemplate后上载多个文件,spring,spring-boot,file-upload,multipartform-data,resttemplate,Spring,Spring Boot,File Upload,Multipartform Data,Resttemplate,假设我有一个端点,如下所示: @PostMapping( value = "/something", consumes = MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_VALUE) public SomeDTO post2Files( @RequestPart("file1") MultipartFile file1, @RequestPart("file2")
@PostMapping(
value = "/something",
consumes = MULTIPART_FORM_DATA_VALUE,
produces = APPLICATION_JSON_VALUE)
public SomeDTO post2Files(
@RequestPart("file1") MultipartFile file1,
@RequestPart("file2") MultipartFile file2 {
在另一个服务中,我想从文件系统读取一个文件,然后重新发送它,而file2实际上是一个字符串,我想通过rest模板作为文件传递。
我试过这样的方法:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file1", new FileSystemResource(somePath));
body.add("file2", new ByteArrayResource(someString.getBytes()));
restTemplate.postForObject("/something", new HttpEntity<>(body, headers), SomeDTO.class)
HttpHeaders=newhttpheaders();
headers.setContentType(MediaType.MULTIPART\u FORM\u DATA);
MultiValueMap body=新链接的MultiValueMap();
add(“file1”,新文件系统资源(somePath));
add(“file2”,新的ByteArrayResource(someString.getBytes());
restTemplate.postForObject(“/something”,新的HttpEntity(主体、标题),SomeDTO.class)
它不起作用,我也不知道为什么。我得到400英镑。我该怎么做才能让请求通过呢?我想明白了。
这就是解决方案:
body.add("dataSchema", new ByteArrayResource(someString.getBytes()) {
@Override
public String getFilename() {
return "file2";
}
});
因为文件名与@RequestPart不匹配,所以它不起作用。解决了这个问题。
这就是解决方案:
body.add("dataSchema", new ByteArrayResource(someString.getBytes()) {
@Override
public String getFilename() {
return "file2";
}
});
因为文件名与@RequestPart不匹配,所以它无法工作