Include Page | ||||
---|---|---|---|---|
|
FTP/SFTP Component
This component provides access to remote file systems over the FTP and SFTP protocols.
This component is an extension of the File component.
URI format
Code Block |
---|
ftp://[username@]hostname[:port]/filename[?options]
sftp://[username@]hostname[:port]/filename[?options]
|
Where filename represents the underlying file name or directory. Can contain nested folders.
The username is currently only possible to provide in the hostname parameter.
If no port number is provided. Camel will provide default values according to the protocol. (ftp = 21, sftp = 22)
Examples
ftp://someone@someftpserver.com/public/upload/images/holiday2008?password=secret&binary=true
ftp://someoneelse@someotherftpserver.co.uk:12049/reports/2008/budget.txt?password=secret&binary=false&directory=false
ftp://publicftpserver.com/download
Options
Name | Default Value | Description |
---|---|---|
directory | true | indicates whether or not the given file name should be interpreted by default as a directory or file (as it sometimes hard to be sure with some FTP servers) |
password | null | specifies the password to use to login to the remote file system |
binary | false | specifies the file transfer mode BINARY or ASCII. Default is ASCII. |
consumer.setNames | false | Used by FTPConsumer. If set to true Camel will set the special filename header FileComponent.HEADER_FILE_NAME value to the filename from the FTP Server. |
consumer.delay | 500 | Delay in millis between each poll |
consumer.initialDelay | 1000 | Millis before polling starts |
consumer.userFixedDelay | false | true to use fixed delay between pools, otherwise fixed rate is used. See ScheduledExecutorService in JDK for details. |
consumer.exclusiveReadLock | true | New option in Camel 1.5: Used by FTPConsumer. If set to true Camel will only poll the ftp files if it has exclusive read to the file (= the file is not in progress of being written). Camel will wait until it is granted, testing once every second. The test is implemented by Camel will try to rename the file. Setting to false Camel will poll the file even if its in progress of being written (= this is the behavior of Camel 1.4). |
consumer.deleteFile | false | New option in Camel 1.5: Used by FTPConsumer. Flag to set if the consumed file should be deleted after it has been downloaded. |
consumer.regexPattern | null | Used by FTPConsumer. Regular expression to use for matching files when consuming. |
consumer.moveNamePrefix | null | New option in Camel 1.5: Used by FTPConsumer. The prefix String perpended to the filename when moving it. For example to move processed files into the done directory, set this value to 'done/' |
consumer.moveNamePostfix | null | New option in Camel 1.5: Used by FTPConsumer. The postfix String appended to the filename when moving it. For example to rename processed files from foo to foo.old set this value to '.old' |
consumer.excludedNamePrefix | null | Camel 1.5: Is used to exclude files if filename is starting with the given prefix. |
consumer.excludedNamePostfix | null | Camel 1.5: Is used to exclude files if filename is ending with the given postfix. |
New default behavior for FTP/SFTP-Consumers in Camel 1.5
By default the file is only consumer if the file is not in the progress of being written.
The consumer will always skip any file which name starts with a dot, such as ".", ".camel", ".m2" or ".groovy"
).
Message Headers
The following message headers is provided in the message.
Header | Description |
---|---|
file.remote.host | The hostname of the remote server |
file.remote.name | The fullname of the file consumed from the remote server |
Consumer properties
When using FTPConsumer (downloading files from a FTP Server) the consumer specific properties from the File component should be prefixed with "consumer.". For example the delay option from File Component should be specified as "consumer.delay=30000" in the URI. See the samples or some of the unit tests of this component.
Known issues
When consuming files (downloading) you must use type conversation to either String or to InputStream for ASCII and BINARY file types.
In Camel 1.4 this is fixed, as there are build in type converters for the ASCII and BINARY file types, meaning that you do not need the convertBodyTo expression.
In Camel 1.4 or below Camel FTPConsumer will poll files regardless if the file is currently being written. See the consumer.exclusiveRead option.
In Camel 1.5 the file consumer will avoid polling files that is currently in the progress of being written (see option consumer.exclusiveRead). However this requires Camel being able to rename the file for its testing. If the Camel user hasn't this rights on the file system, you can set this option to false to revert the change to the default behavior of Camel 1.4 or older.
Also in Camel 1.3 since setNames is default false then you must explicitly set the filename using the setHeader expression when consuming from FTP directly to File.
The code below illustrates this:
Code Block |
---|
private String ftpUrl = "ftp://camelrider@localhost:21/public/downloads?password=admin&binary=false";
private String fileUrl = "file:myfolder/?append=false&noop=true";
return new RouteBuilder() {
public void configure() throws Exception {
from(ftpUrl).setHeader(FileComponent.HEADER_FILE_NAME, constant("downloaded.txt")).convertBodyTo(String.class).to(fileUrl);
}
};
|
Or you can set the option to true as illustrated below:
Code Block |
---|
private String ftpUrl = "ftp://camelrider@localhost:21/public/downloads?password=admin&binary=false&consumer.setNames=true";
private String fileUrl = "file:myfolder/?append=false&noop=true";
return new RouteBuilder() {
public void configure() throws Exception {
from(ftpUrl).convertBodyTo(String.class).to(fileUrl);
}
};
|
Sample
In the sample below we setup 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.
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpToBinarySampleTest.java} |
Debug logging
This component has log level TRACE that can be helpful if you have problems.
Include Page | ||
---|---|---|
|