将文件上传到骆驼休息路线

发布于 2025-01-17 09:18:43 字数 884 浏览 5 评论 0原文

我正在尝试使用 multipart/form-data 将文件上传到 Camel 路线。 一切都很好,但是,我无法获取原始文件名。 Camel版本是:3.14.1

更新 对路线进行以下修改。我设法处理二进制文件(获取文件名并存储它们)。但是,对于文本文件,文件会附加边界页脚:

------WebKitFormBoundary7BH9nQ2RqDXvTRAJ--

路线定义:

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

提前谢谢

爱德华多

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

壹場煙雨 2025-01-24 09:18:43

编辑:由于您已经有其他所有已正常工作,因此我建议使用流缓存选项。

如nicolas 建议“ https://camel.apache.org/components/3.14.x/dataformats/mime-mime-multipart-dataformat.html” rel =“ nofollow noreferrer”>数据格式

同样,您获得“缺少启动边界”的原因是因为您的处理器正在消耗输入式流。您可以尝试reset()它,但是最好只使用InputStream,或者启用流缓存

您还可以将流转换为字符串,而不是流式缓存。在处理器之前,请补充:

.convertBodyTo(String.class)

可以一遍又一遍地读取字符串。如果您仍然遇到丢失的启动边界错误,请尝试在Unmarshal操作之前记录身体。确保消息完好无损,并且确实包含了开始边界。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文