将文件上传到骆驼休息路线
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:由于您已经有其他所有已正常工作,因此我建议使用流缓存选项。
如nicolas 建议“ https://camel.apache.org/components/3.14.x/dataformats/mime-mime-multipart-dataformat.html” rel =“ nofollow noreferrer”>数据格式。
同样,您获得“缺少启动边界”的原因是因为您的处理器正在消耗输入式流。您可以尝试
reset()
它,但是最好只使用InputStream,或者启用流缓存。您还可以将流转换为字符串,而不是流式缓存。在处理器之前,请补充:
可以一遍又一遍地读取字符串。如果您仍然遇到丢失的启动边界错误,请尝试在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:
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.