[英]Upload a file to a rest server using apache camel
I need to sync a file from a folder to a rest endpoint. 我需要将文件从文件夹同步到其他端点。 So if a file is placed in a specific folder I need to send that file to a REST endpoint accepting multipart files.
因此,如果文件放置在特定文件夹中,则需要将该文件发送到接受多部分文件的REST端点。 I am using apache camel to achieve this.
我正在使用apache骆驼来实现这一目标。
The REST endpoint is written in Spring and is as below: REST端点是用Spring编写的,如下所示:
@PostMapping("/")
public String handleFileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
storageService.store(file);
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded " + file.getOriginalFilename() + "!");
return "redirect:/";
}
I am new to Camel and have figured out how to poll the directory by building a route and fetch the file, but I am unable to figure out how this route has to be used to put this file to the rest endpoint. 我是Camel的新手,并且想出了如何通过构建路由和获取文件来轮询目录的方法,但是我无法弄清楚必须如何使用此路由将此文件放置到其余端点。 This is what I have tried:
这是我尝试过的:
@Component
public class SampleCamelRouter extends RouteBuilder {
@Override
public void configure() throws Exception {
from("file://target/input?delay=4000")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
File filetoUpload = exchange.getIn().getBody(File.class);
String fileName = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.addTextBody("fileName", fileName);
entity.addBinaryBody("file", filetoUpload);
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.build().writeTo(out);
InputStream inputStream = new ByteArrayInputStream(out.toByteArray());
exchange.getIn().setBody(inputStream);
}
})
.setHeader(Exchange.CONTENT_TYPE, constant("multipart/form-data"))
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.to("http://localhost:9090/");
}
}
But when I run this, I get the below exception in the rest server: 但是,当我运行它时,我在其余服务器中收到以下异常:
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
And on the camel side the request fails with a 500 error. 在骆驼方面,请求失败并显示500错误。
Any leads are appreciated. 任何线索表示赞赏。
use headers = "Content-Type= multipart/form-data",
into controller. 在控制器中使用
headers = "Content-Type= multipart/form-data",
。
The problem isn't in your code - it's in your request. 问题不在您的代码中,而是在您的请求中。 You're missing boundary in your multipart request.
您的多部分请求中缺少边界。 As it said in specification here :
由于它在规范说这里 :
The Content-Type field for multipart entities requires one parameter, "boundary", which is used to specify the encapsulation boundary. 多部分实体的Content-Type字段需要一个参数“边界”,该参数用于指定封装边界。 The encapsulation boundary is defined as a line consisting entirely of two hyphen characters ("-", decimal code 45) followed by the boundary parameter value from the Content-Type header field.
封装边界定义为一行,该行完全由两个连字符(“-”,十进制代码45)组成,后跟来自Content-Type标头字段的边界参数值。 This and this posts should also be helpful.
这和这篇文章也应该有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.