Camel

Camel and ftp tutorial

Camel provides simple and effective integration with ftp, sftp and ssh protocols. Let's see some practical examples.

How to use Camel to upload files using sftp:

package com.sample;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;

public class FTPExample {
    
public static void main(String[] args) throws Exception {
		 Main main = new Main();
	        main.enableHangupSupport();
	        main.addRouteBuilder(new MyRouteFTP());
	        main.run(args);
	}
}


 class MyRouteFTP extends RouteBuilder {
	@Override
	public void configure() throws Exception {
		System.out.println("My Routing Started");
		from("file:/var/data/in?noop=true").to(
				"sftp://user@myhost//home/data?password=secret");
		System.out.println("My Routing complete");
	}
}

In the above code, we are transferring the files from /var/data/in to the host "myhost" via sftp.

Please note that in order to run a Camel FTP component using sftp you will need the following dependencies:

<dependency>
	<groupId>com.jcraft</groupId>
	<artifactId>jsch</artifactId>
	<version>0.1.53</version>
</dependency>

<dependency>
	<groupId>org.apache.camel</groupId>
	<artifactId>camel-ftp</artifactId>
	<version>2.15.2</version>
</dependency>

How to use Camel to download files using ftp:

On the other hand, downloading the file via ftp means that the ftp component will be used in the "to" direction of the route:

class MyRouteFTP extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        System.out.println("My Routing Started");
        from("ftp://user///opt/data/?password=secret").
             to("file:/var/data/out");
        System.out.println("My Routing complete");
    }
}

Using the ftp protocol requires that you use the common-net API instead of the jcraft API:

<dependency>
	<groupId>commons-net</groupId>
	<artifactId>commons-net</artifactId>
	<version>3.3</version>
</dependency>

<dependency>
	<groupId>org.apache.camel</groupId>
	<artifactId>camel-ftp</artifactId>
	<version>2.15.2</version>
</dependency>

Follow us on Twitter