Spring Camel Rest路由问题与文件上载
在过去的两天里,我一直在为以下问题而挣扎。我正在尝试通过POST路由上传csv文件。从html表单中选择要上载的文件Spring Camel Rest路由问题与文件上载,spring,spring-boot,file-upload,multipartform-data,spring-camel,Spring,Spring Boot,File Upload,Multipartform Data,Spring Camel,在过去的两天里,我一直在为以下问题而挣扎。我正在尝试通过POST路由上传csv文件。从html表单中选择要上载的文件 <form method="POST" action="uploadFile" enctype="multipart/form-data"> <div> <div style="display:inline;"> &l
<form method="POST" action="uploadFile" enctype="multipart/form-data">
<div>
<div style="display:inline;">
<label path="file">Select a file to upload</label>
</div>
<div style="display:inline;">
<input type="file" name="file" />
</div>
</div>
<div>
<input type="submit" value="Submit" />
</div>
</form>
您可以尝试用以下方法替换
.process
方法
.process(exchange -> {
String strMessage = FileUtils.readFileToString(
new File(yourFileName), StandardCharsets.UTF_8);
exchange.getMessage().setBody(strMessage);
exchange.getMessage().setHeader(Exchange.FILE_NAME, yourFileName);
})
下面是检索上载文件内容的方法
...
.post()
.bindingMode(RestBindingMode.off)
.route()
.routeId("postUploadForm")
.unmarshal().mimeMultipart()
.process((exchange) -> {
InputStream is = exchange.getIn().getBody(InputStream.class);
// is contains the content file
...
})
但是用这种方法,我无法获得文件名。
我希望它能帮上一点忙谢谢您的回复!不幸的是,它不起作用。它将文件数据作为文件名读取,并抛出IOException。因此,在尝试之后,通过解组,您将丢失文件名,正如您所说的,但我已经在处理器中获取了该文件名。我将编辑我的问题以显示最终代码。非常感谢你的指导!!
.process(exchange -> {
String strMessage = FileUtils.readFileToString(
new File(yourFileName), StandardCharsets.UTF_8);
exchange.getMessage().setBody(strMessage);
exchange.getMessage().setHeader(Exchange.FILE_NAME, yourFileName);
})
...
.post()
.bindingMode(RestBindingMode.off)
.route()
.routeId("postUploadForm")
.unmarshal().mimeMultipart()
.process((exchange) -> {
InputStream is = exchange.getIn().getBody(InputStream.class);
// is contains the content file
...
})