带有 Spring 的多部分文件 http 请求 Rest 模板到达时没有文件
Multipart files http request with Spring Rest Template arrives without the files
我有这个客户端,用于使用 Rest 模板发送多部分文件 http 请求
@Component
public class RestTemplatePost {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@PostConstruct
public void prepareMessage() throws Exception {
File file = new File("****");
File file2 = new File("****");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("files", file);
form.add("files", file2);
form.add("usertoken", "test");
form.add("sendTo", "test);
form.add("subject", "test");
form.add("content", "test");
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(form, httpHeaders);
String serverUrl = "http://localhost:8080/api/mails/send";
List<HttpMessageConverter<?>> httpMessageConverters = new ArrayList<>();
httpMessageConverters.add(new FormHttpMessageConverter());
httpMessageConverters.add(new MappingJackson2HttpMessageConverter());
restTemplate().setMessageConverters(httpMessageConverters);
restTemplate().postForEntity(serverUrl, requestEntity, String.class);
}
}
然后我有这个服务器端应该接收请求:
@RestController
@RequestMapping("/api")
public class MainConroller {
private static final Logger log = LoggerFactory.getLogger(MainConroller.class);
@Autowired
private MainService mainService;
public MainConroller(MainService mainService) {
this.mainService = mainService;
}
@PostMapping("/mails/send")
public void send(
@RequestParam("usertoken") String usertoken,
@RequestParam("sendTo") String sendTo,
@RequestParam("subject") String subject,
@RequestParam("content") String content,
@RequestParam(required = false, name = "files") List<MultipartFile> multipartFiles) {
log.debug("{}, {}, {}, {}", usertoken, sendTo, subject, content);
mainService.processMessage(usertoken, sendTo, subject, content, multipartFiles);
}
}
当我从客户端发送此请求时,除文件外,所有内容都到达了服务器端。
收到请求后RequestParam文件为空。
更新
按照建议明确删除了消息转换器,没有任何改变。
没有文件被发送,因为没有 HttpMessageConverter
能够转换 java.io.File
。
另一方面,有一个 ResourceHttpMessageConverter
which converts all kinds of Resource
, including FileSystemResource
.
PS FormHttpMessageConverter
在创建 RestTemplate
时默认添加,FormHttpMessageConverter
也在后台使用它。
我有这个客户端,用于使用 Rest 模板发送多部分文件 http 请求
@Component
public class RestTemplatePost {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@PostConstruct
public void prepareMessage() throws Exception {
File file = new File("****");
File file2 = new File("****");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("files", file);
form.add("files", file2);
form.add("usertoken", "test");
form.add("sendTo", "test);
form.add("subject", "test");
form.add("content", "test");
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(form, httpHeaders);
String serverUrl = "http://localhost:8080/api/mails/send";
List<HttpMessageConverter<?>> httpMessageConverters = new ArrayList<>();
httpMessageConverters.add(new FormHttpMessageConverter());
httpMessageConverters.add(new MappingJackson2HttpMessageConverter());
restTemplate().setMessageConverters(httpMessageConverters);
restTemplate().postForEntity(serverUrl, requestEntity, String.class);
}
}
然后我有这个服务器端应该接收请求:
@RestController
@RequestMapping("/api")
public class MainConroller {
private static final Logger log = LoggerFactory.getLogger(MainConroller.class);
@Autowired
private MainService mainService;
public MainConroller(MainService mainService) {
this.mainService = mainService;
}
@PostMapping("/mails/send")
public void send(
@RequestParam("usertoken") String usertoken,
@RequestParam("sendTo") String sendTo,
@RequestParam("subject") String subject,
@RequestParam("content") String content,
@RequestParam(required = false, name = "files") List<MultipartFile> multipartFiles) {
log.debug("{}, {}, {}, {}", usertoken, sendTo, subject, content);
mainService.processMessage(usertoken, sendTo, subject, content, multipartFiles);
}
}
当我从客户端发送此请求时,除文件外,所有内容都到达了服务器端。
收到请求后RequestParam文件为空。
更新 按照建议明确删除了消息转换器,没有任何改变。
没有文件被发送,因为没有 HttpMessageConverter
能够转换 java.io.File
。
另一方面,有一个 ResourceHttpMessageConverter
which converts all kinds of Resource
, including FileSystemResource
.
PS FormHttpMessageConverter
在创建 RestTemplate
时默认添加,FormHttpMessageConverter
也在后台使用它。