简体   繁体   English

使用apache camel从http位置下载

[英]Download from a http location using apache camel

I need to download a file from an http location to my local system using apache camel. 我需要使用apache camel将文件从http位置下载到本地系统。 When I gave the below code 当我给下面的代码

    from("http://url/filename.xml")                         
    .to("file://C:location")  

it has worked for ftp but not working when the url is "http". 它适用于ftp,但当url为“ http”时不起作用。 That is, it is not downloading the file from the http location to the local address provided in the "to()". 也就是说,它没有将文件从http位置下载到“ to()”中提供的本地地址。

the http component cannot be used as a consumer ie. http组件不能用作使用者,即。 you cannot have a route as from("http://...") 您不能有来自((http:// ...“)的路线

you need to use a consumer component that will start the route. 您需要使用将启动路线的使用者组件。 You could try something like this 你可以尝试这样的事情

from("timer:foo?fixedRate=true&period=5000")
.to("http://url/filename.xml")                         
.to("file://C:location") 

This should work. 这应该工作。

 from("direct:abc")
            .setHeader("Accept", simple("application/xml"))//Change it according to the file content
            .setHeader(Exchange.HTTP_METHOD, constant("GET"))
            .to("http://url/filename.xml")
            .to("file:///tmp/?fileName=yourFileName.xml");

You cannot use from("Some url") . 您不能使用from("Some url") Above route is triggered whenever there is a message on direct:abc endpoint. 只要direct:abc端点上有消息,就会触发以上路由。 You can change the yourFileName.xml to whatever filename you want it to be stored as. 您可以将yourFileName.xml更改为您希望将其存储为的任何文件名。

Instead of a trigger from route, you can as well use a timer or any other means of self triggering. 除了路由触发之外,您还可以使用计时器或任何其他自触发方式。

The reason you cannot consume from a rest enpoint like this 你不能从休息中消费的原因像这样

from("http://url/filename.xml")  

is you cannot consume from http endpoint. 是您不能从http端点消费。 So there needs to be a trigger. 因此,需要有一个触发器。 Infact the exception message is pretty clear when you do like this. 实际上,当您这样做时,异常消息非常清晰。 It says 它说

org.apache.camel.spring.boot.CamelSpringBootInitializationException: org.apache.camel.FailedToCreateRouteException: Failed to create route route1: Route(route1)[[From[http://url/filename.xml]] -> [To[... because of Cannot consume from http endpoint

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM