Uploading multiple files and metadata with CXF

2019-03-29 06:10发布

问题:

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

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.

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.

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.

UPDATE

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

回答1:

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