我需要使用CXF创建一个文件上载处理程序作为REST Web服务.我已经能够使用以下代码上传包含元数据的单个文件:
@POST
@Path("/uploadImages")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadImage(@Multipart("firstName") String firstName,
@Multipart("lastName") String lastName,
List<Attachment> attachments) {
for (Attachment att : attachments) {
if (att.getContentType().getType().equals("image")) {
InputStream is = att.getDataHandler().getInputStream();
// read and store image file
}
}
return Response.ok().build();
}
Run Code Online (Sandbox Code Playgroud)
现在我需要添加对在同一请求中上传多个文件的支持.在这种情况下,image/jpeg我得到一个内容类型的附件,而不是带内容类型的附件multipart/mixed,它本身包含image/jpeg我需要的各个附件.
我已经看到了使用元数据上传多个JSON或JAXB对象的示例,但是我无法使用二进制图像数据.我已尝试直接使用MultipartBody,但它只返回multipart/mixed附件,而不是其中image/jpeg嵌入的附件.
有没有办法递归解析multipart/mixed附件以获取嵌入的附件?我当然可以得到multipart/mixed附件的输入流,并自己解析文件,但我希望有更好的方法.
UPDATE
这看起来像kludgey,但下面的代码现在已经足够好了.我希望看到更好的方式.
for (Attachment att : attachments) {
LOG.debug("attachment content type: {}", att.getContentType().toString());
if (att.getContentType().getType().equals("multipart")) {
String ct = att.getContentType().toString();
Message msg = …Run Code Online (Sandbox Code Playgroud)