用例定义了使用OpenLiberty 19.0.0.5在Java中下载几个文件以及一些json
实际上,在OpenLiberty中,可以将IMultipartBody用作REST服务的参数。我可以使用文件以及json。但是,当要发送多部分消息时,我会遇到各种各样的问题。
@POST
@Consumes("multipart/form-data")
@Produces("multipart/form-data")
public Response postFormData(IMultipartBody multipartBody) {
public Response postFormData(IMultipartBody multipartBody) {
FileOutputStream outputStream=null;
List<IAttachment> attachments = multipartBody.getAllAttachments();
InputStream stream = null;
String fileKey = null;
FileOutputStream fileOutputStream = null;
for (Iterator<IAttachment> it = attachments.iterator(); it.hasNext(); ) {
IAttachment attachment = it.next();
if (attachment == null) {
continue;
}
DataHandler dataHandler = attachment.getDataHandler();
try {
stream = dataHandler.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
MultivaluedMap<String, String> map = attachment.getHeaders();
String contentDisposition = map.getFirst("Content-Disposition");
if (contentDisposition != null && contentDisposition.toLowerCase().contains("filename=")) {
<write stream to file and do work>
<how can I send back a file using multipartBody>
}
}
return Response.ok(multipartBody).build();
}
}
我想发回mulitpartBody中的流,但找不到允许我这样做的任何方法。
当我使用SOAPUI测试服务并将文件附加到请求并用作媒体类型multipart/form-data
时,我得到以下响应:
HTTP/1.1 200 OK
X-Powered-By: Servlet/4.0
Content-Type: multipart/form-data; boundary="uuid:082bc4df-b829-4ec0-80d1-6a3c059b88a7"
Date: Mon, 17 Jun 2019 07:52:15 GMT
Content-Language: en-US
Content-Length: 325
--uuid:082bc4df-b829-4ec0-80d1-6a3c059b88a7
Content-Type: application/octet-stream;name=global-groovy.log
Content-Transfer-Encoding: binary
Content-ID: <root.message@cxf.apache.org>
Content-Disposition: form-data; name="global-groovy.log"; filename="global-groovy.log"
--uuid:082bc4df-b829-4ec0-80d1-6a3c059b88a7--
因此响应返回但没有文件,我该如何发回文件?
有没有一种已知的方法将多部分消息用作响应?
我找到了另一种使用"multipart/mixed"
数据进行发送的方式,但是我不确定这是否是开放自由社区的意图:
@POST
@Consumes("multipart/form-data")
@Produces("multipart/mixed")
public Response postFormData(IMultipartBody multipartBody) {
FileOutputStream outputStream=null;
List<IAttachment> attachments = multipartBody.getAllAttachments();
InputStream stream = null;
String fileKey = null;
FileOutputStream fileOutputStream = null;
for (Iterator<IAttachment> it = attachments.iterator(); it.hasNext(); ) {
IAttachment attachment = it.next();
if (attachment == null) {
continue;
}
DataHandler dataHandler = attachment.getDataHandler();
try {
stream = dataHandler.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
MultivaluedMap<String, String> map = attachment.getHeaders();
String contentDisposition = map.getFirst("Content-Disposition");
if (contentDisposition != null && contentDisposition.toLowerCase().contains("filename=")) {
// <write the file and process it>
}
}
//https://cwiki.apache.org/confluence/display/CXF20DOC/JAX-RS+Multiparts#JAX-RSMultiparts-Writingattachments
Map<String, Object> map = new LinkedHashMap<>();
map.put("application/json", "{\"menu\": {\"id\": \"file\",\"value\": \"File\"}}");
try {
final File file = new File("myFile");
map.put("application/octet-stream", Files.newInputStream(file.toPath()));
} catch (IOException e) {
e.printStackTrace();
}
return Response.ok(map).build();
}
}
有人暗示吗?