使用CXF上传多个文件和元数据 [英] Uploading multiple files and metadata with CXF

查看:502
本文介绍了使用CXF上传多个文件和元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用CXF创建一个文件上传处理程序作为REST Web服务。我已经能够使用以下代码上传包含元数据的单个文件:

I need to create a file upload handler as a REST web service with CXF. I've been able to upload a single file with metadata using code like the following:

@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();
}

现在我需要添加对在同一请求中上传多个文件的支持。在这种情况下,我得到一个带有 multipart / mixed 内容的附件,而不是带有 image / jpeg 内容类型的附件type,它本身包含我需要的个别 image / jpeg 附件。

Now I need to add support for uploading multiple files in the same request. In this case, instead of an attachment with image/jpeg content type, I get an attachment with multipart/mixed content type, which itself contains the individual image/jpeg attachments that I need.

我见过上传的例子多个带有元数据的JSON或JAXB对象,但我无法使用二进制图像数据。我已尝试直接使用MultipartBody,但它只返回 multipart / mixed 附件,而不是 image / jpeg 附件嵌入其中。

I've seen examples for uploading multiple JSON or JAXB objects with metadata, but I have not been able to get anything to work with binary image data. I have tried using the MultipartBody directly, but it only returns the multipart/mixed attachment, not the image/jpeg attachments embedded within it.

有没有办法以递归方式解析 multipart / mixed 附件以获取嵌入式附件?我当然可以获取 multipart / mixed 附件的输入流,并自己解析文件,但我希望有更好的方法。

Is there a way to recursively parse a multipart/mixed attachment to get the embedded attachments? I can of course get the input stream of the multipart/mixed attachment, and parse out the files myself, but I'm hoping there is a better way.

更新

这看起来很笨拙,但下面的代码就足够了。我希望看到一个更好的方法。

This seems kludgey, but the following bit of code is good enough for now. I would love to see a better way though.

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 = new MessageImpl();
        msg.put(Message.CONTENT_TYPE, ct);
        msg.setContent(InputStream.class, att.getDataHandler().getInputStream());
        AttachmentDeserializer ad = new AttachmentDeserializer(msg, Arrays.asList(ct));
        ad.initializeAttachments();

        // store the first embedded attachment
        storeFile(msg.getContent(InputStream.class));

        // store remaining embedded attachments
        for (org.apache.cxf.message.Attachment child : msg.getAttachments()) {
            storeFile(child.getDataHandler().getInputStream());
        }
    }
    else if (att.getContentType().getType().equals("image")) {
        storeFile(att.getDataHandler().getInputStream());
    }
}


推荐答案

我已经建立了类似的服务来上传多个图像。我的实现如下所示(可能有帮助)

I've build a similar service to upload multiple images. My implementation looks like the following (maybe it helps)

@Consumes({MediaType.MULTIPART_FORM_DATA,"multipart/mixed" })
public Response uploadImages(final List<Attachment> attachments) {

    Map<String, InputStream> imageMap = new HashMap<String, InputStream>();

    for (Attachment attachment : attachments) {
        String imageName = attachment.getContentDisposition().getParameter("filename");
        if (imageName == null) {
            imageName = UUID.randomUUID().toString();
        }

        InputStream image = attachment.getDataHandler().getInputStream();
        imageMap.put(imageName, image);
    }

    return imageMap;

}

如果有人喜欢再见数组而不是输入流,它可以使用这个帮助方法轻松转换

if someone prefers bye arrays instead of input streams, it can be converted easily using this helper method

private static byte[] extractByteArray(final InputStream inputStream) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    byte[] dataChunk = new byte[1024 * 16];
    int numRead = 0;
    while (numRead != -1) {
        numRead = inputStream.read(dataChunk, 0, dataChunk.length);

        if (numRead != -1) {
            buffer.write(dataChunk, 0, numRead);
        }
    }

    buffer.flush();
    return buffer.toByteArray();
}

这篇关于使用CXF上传多个文件和元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆