我们如何通过RESTEasy Java客户端发送文件列表? Spring REST是:
@PostMapping()
public ResponseEntity<?> send(@RequestPart(value = "message") String message, @RequestPart(value = "attachment", required = false) List<MultipartFile> attachments)
在Postman中,它是通过用一个键“附件”在表单数据中指定多个文件来完成的,但是MultipartFormDataOutput的内部具有Map,因此它仅记住最后添加的文件。
答案 0 :(得分:0)
我已经通过使用org.apache.http.entity.mime.MultipartEntityBuilder解决了这个问题:
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("message", messageStr, ContentType.TEXT_PLAIN.withCharset(UTF_8));
for (File file: files) {
builder.addBinaryBody(
"attachment",
new FileInputStream(file),
ContentType.APPLICATION_OCTET_STREAM,
file.getName()
);
}
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost uploadFile = new HttpPost(url);
uploadFile.setEntity(builder.build());
CloseableHttpResponse response = httpClient.execute(uploadFile);