FTP2Page edited by Claus IbsenFTP/SFTP/FTPS Component - Camel 2.0 onwardsThis component provides access to remote file systems over the FTP and SFTP protocols.
URI formatftp://[usern...@]hostname[:port]/directoryname[?options] sftp://[usern...@]hostname[:port]/directoryname[?options] ftps://[usern...@]hostname[:port]/directoryname[?options]
Using Local Work DirectoryCamel supports consuming from remote FTP servers and downloading the files directly into a local work directory. This avoids reading the entire remote file content into memory as it is streamed directly into the local file using FileOutputStream. Camel will store to a local file with the same name as the remote file, though with .inprogress as extension while the file is being downloaded. Afterwards, the file is renamed to remove the .inprogress suffix. And finally, when the Exchange is complete the local file is deleted. So if you want to download files from a remote FTP server and store it as files then you need to route to a file endpoint such as:
from("ftp://some...@someserver.com?password=secret&localWorkDirectory=/tmp").to("file://inbox");
SamplesIn the sample below we set up Camel to download all the reports from the FTP server once every hour (60 min) as BINARY content and store it as files on the local file system. protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() throws Exception { // we use a delay of 60 minutes (eg. once pr. hour we poll the FTP server long delay = 60 * 60 * 1000L; // from the given FTP server we poll (= download) all the files // from the public/reports folder as BINARY types and store this as files // in a local directory. Camel will use the filenames from the FTPServer // notice that the FTPConsumer properties must be prefixed with "consumer." in the URL // the delay parameter is from the FileConsumer component so we should use consumer.delay as // the URI parameter name. The FTP Component is an extension of the File Component. from("ftp://sc...@localhost/public/reports?password=tiger&binary=true&consumer.delay=" + delay). to("file://target/test-reports"); } }; } And the route using Spring DSL: <route> <from uri="ftp://sc...@localhost/public/reports?password=tiger&binary=true&delay=60000"/> <to uri="file://target/test-reports"/> </route> Consuming a remote FTP server triggered by a routeThe FTP consumer is built as a scheduled consumer to be used in the from route. However, if you want to start consuming from an FTP server triggered within a route. Notice we use the Content Enricher EIP with the pollEnrich DSL to consume the FTP file: from("seda:start") // set the filename in FILE_NAME header so Camel know the name of the remote file to poll .setHeader(Exchange.FILE_NAME, header("myfile")) .pollEnrich("ftp://ad...@localhost:21/getme?password=admin&binary=false") .to("mock:result"); Consuming a remote FTPS server (implicit SSL) and client authenticationfrom("ftps://ad...@localhost:2222/public/camel?password=admin&securityProtocol=SSL&isImplicit=true&ftpClient.keyStore.file=./src/test/resources/server.jks&ftpClient.keyStore.password=password&ftpClient.keyStore.keyPassword=password") .to("bean:foo"); Consuming a remote FTPS server (explicit TLS) and a custom trust store configurationfrom("ftps://ad...@localhost:2222/public/camel?password=admin&ftpClient.trustStore.file=./src/test/resources/server.jks&ftpClient.trustStore.password=password") .to("bean:foo"); Filter using org.apache.camel.component.file.GenericFileFilterCamel supports pluggable filtering strategies. This strategy it to use the build in org.apache.camel.component.file.GenericFileFilter in Java. You can then configure the endpoint with such a filter to skip certain filters before being processed. In the sample we have build our own filter that only accepts files starting with report in the filename. public class MyFileFilter implements GenericFileFilter { public boolean accept(GenericFile file) { // we only want report files return file.getFileName().startsWith("report"); } } And then we can configure our route using the filter attribute to reference our filter (using # notation) that we have defines in the spring XML file: <!-- define our sorter as a plain spring bean --> <bean id="myFilter" class="com.mycompany.MyFileFilter"/> <route> <from uri="ftp://someu...@someftpserver.com?password=secret&filter=#myFilter"/> <to uri="bean:processInbox"/> </route> Filtering using ANT path matcherThe ANT path matcher is a filter that is shipped out-of-the-box in the camel-spring jar. So you need to depend on camel-spring if you are using Maven. The file paths are matched with the following rules:
The sample below demonstrates how to use it: <camelContext xmlns="http://camel.apache.org/schema/spring"> <template id="camelTemplate"/> <!-- use myFilter as filter to allow setting ANT paths for which files to scan for --> <endpoint id="myFTPEndpoint" uri="ftp://ad...@localhost:20123/antpath?password=admin&recursive=true&delay=10000&initialDelay=2000&filter=#myAntFilter"/> <route> <from ref="myFTPEndpoint"/> <to uri="mock:result"/> </route> </camelContext> <!-- we use the AntPathMatcherRemoteFileFilter to use ant paths for includes and exlucde --> <bean id="myAntFilter" class="org.apache.camel.component.file.AntPathMatcherGenericFileFilter"> <!-- include and file in the subfolder that has day in the name --> <property name="includes" value="**/subfolder/**/*day*"/> <!-- exclude all files with bad in name or .xml files. Use comma to seperate multiple excludes --> <property name="excludes" value="**/*bad*,**/*.xml"/> </bean> Debug loggingThis component has log level TRACE that can be helpful if you have problems. See Also
Change Notification Preferences
View Online
|
View Change
|
Add Comment
|
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence
- [CONF] Apache Camel > FTP2 confluence