Uploading a file to Camel Rest route

Viewed 314

I'm trying to upload a file using multipart/form-data to a Camel route. All is good, however, I can't get the original file name. Camel version is: 3.14.1

Update With the following modification to the route. I managed to process binary files (getting the file name and storing them). However, with text files, the file is appended with the boundary footer:

------WebKitFormBoundary7BH9nQ2RqDXvTRAJ--

The route definition:

        rest("/v1/file-upload-form")
            .post()
            .consumes(MediaType.MULTIPART_FORM_DATA_VALUE)
            .route()
            .process((exchange) -> {
                InputStream is = exchange.getIn().getBody(InputStream.class);
                MimeBodyPart mimeMessage = new MimeBodyPart(is);
                DataHandler dh = mimeMessage.getDataHandler();
                exchange.getIn().setBody(dh.getInputStream());
                exchange.getIn().setHeader(Exchange.FILE_NAME, dh.getName());
            })
            .to("file://" + incomingFolder);

Thank you in advance

Edwardo

1 Answers

Edit: Since you have everything else already working, I'd recommend the Stream Caching option.

As Nicolas suggested, checkout Camel's MIME Multipart data format.

Also, the reason you're getting "Missing start boundary" is because your processor is consuming the InputStream. You can try to reset() it, but it might be better to just consume the InputStream once, or enable Stream Caching.

Instead of stream caching, you could also just convert the stream to a string. Before your processor, add:

.convertBodyTo(String.class)

The string can be read over and over. If you still get the missing start boundary error, try logging the body before the unmarshal operation. Make sure the message is intact and that it indeed contains the start boundary.

Related