我们如何通过RESTEasy java客户端发送文件列表? Spring REST是:
@PostMapping()
public ResponseEntity<?> send(@RequestPart(value = "message") String message, @RequestPart(value = "attachment", required = false) List<MultipartFile> attachments)
在Postman中,它是通过在一个键“附件”中指定form-data中的多个文件来制作的,但是MultipartFormDataOutput里面有Map,所以它只记住最后添加的文件。
我使用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);