我需要使用apache骆驼读取文件并将其作为流发送到另一个端点。
<option [value]="0" selected="1==1">No Default Layout</option>
答案 0 :(得分:0)
这里有一些从(file).to(rest-service)路由的简单工作示例
//receiver
from("jetty://http://0.0.0.0:5514/path/getFile")
.process(exchange -> {
if (exchange.getIn().getAttachments() != null) {
for (String key : exchange.getIn().getAttachments().keySet()) {
DataHandler dataHandler = exchange.getIn().getAttachments().get(key);
System.out.println(String.format("Receive attachment:%s size:%s", dataHandler.getName(), dataHandler.getInputStream().available()));
}
}
});
//sender
from("file:/Users/user1/test/?delete=true&delay=5000")
.process(exchange -> {
GenericFile<File> body = exchange.getIn().getBody(GenericFile.class);
exchange.getIn().setHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
exchange.getIn().setHeader("CamelHttpMethod", "POST");
exchange.getIn().setBody(MultipartEntityBuilder.create()
.addPart(body.getFileName(), new FileBody(body.getFile(), ContentType.MULTIPART_FORM_DATA, body.getFileName()))
.build()
);
})
.to("http4://0.0.0.0:5514/path/getFile?synchronous=true")//camel-http4 component for sending to our rest service
;
这里example可以运行并查看其工作原理。