如何通过 RESTEasy 发送文件列表 java
How to send list of files by RESTEasy java
我们如何通过 RESTEasy java 客户端发送文件列表?
Spring REST 是:
@PostMapping()
public ResponseEntity<?> send(@RequestPart(value = "message") String message, @RequestPart(value = "attachment", required = false) List<MultipartFile> attachments)
Postman中是在form-data中一键指定多个文件"attachment",但是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);
我们如何通过 RESTEasy java 客户端发送文件列表? Spring REST 是:
@PostMapping()
public ResponseEntity<?> send(@RequestPart(value = "message") String message, @RequestPart(value = "attachment", required = false) List<MultipartFile> attachments)
Postman中是在form-data中一键指定多个文件"attachment",但是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);