File Operations in Camel (ftp,file components)

In this tutorial, we are going to learn how to do file operations with Apache Camel. We will use two camel components for this purpose – File and Ftp. Generally, File component is used for local file input/output operations and Ftp for remote file operations as an ftp client for Ftp servers.

Example Scenario:

Case 1 : File component

We have an input folder which has a sub-folder and files with different extensions. We want to copy all the text files from it and its sub folders to another folder “output” and  rename them and change their contents.

Case 2: Ftp component

We want to copy a file from our local to a remote ftp server with few configurations for ftp connection.

Project artifacts:

1. Technologies used –

a. Eclipse IDE for Java EE developer (Mars)
b. Preinstalled Maven with Eclipse Mars
c. Camel 2.16.0
d. Java 1.8

2. Eclipse project folder structure:


3. Jars Used

If you are not using Maven, here is the list of jars you would need for this project:


4. Maven Dependencies

file:pom.xml . Paste the below content to pom.xml. It has camel and spring dependencies. It can pass two arguments – “java” for running camel in java DSL route and “xml” for running as Spring routes.

<project xmlns="" xmlns:xsi=""
								<!--<argument>java</argument>  -->

5. Main Class

file: . Main class is our starting point and is called by maven. It can run Java DSL or Spring DSL based on argument passed. Also, we have to comment the line based on the component we don’t want to run.

package com.javanbeyond;

import org.apache.camel.CamelContext;

public class MainClass {

  public static void main(String[] argsthrows Exception {
    if (args.length < 1) {
      System.err.println("Please pass java or xml as argument");
    // Pass route id to execute the particular route
    if (args[0].equalsIgnoreCase("java")) {
      // executeJavaDSL("executeFileComponent");
    else if (args[0].equalsIgnoreCase("xml")) {
      // executeSpringDSL("executeFileComponent");

    else {
      System.out.println("Argument not supported.");
    // Wait 10 seconds for camel to finish file operation


  public static void executeSpringDSL(String routeIdthrows Exception {
    new ClassPathXmlApplicationContext("camel-context.xml")
        .getBean("camel", CamelContext.class).startRoute(routeId);


  public static void executeJavaDSL(String routeIdthrows Exception {
    new CamelJavaDSL().getCamelContext().startRoute(routeId);


6a. Java DSL class

file: . The class where Camel routes are defined  for different components. In our case, file and ftp.

Case 1: “executeFileComponent

I have used camel file expressions to rename the output files and camel simple expressions to change the content of each file.

Case 2: “executeFtpComponent”

I have used filegenie server as a remote ftp server. You need to register in their site before you can use it for ftp connections. At the time of writing, storage less than 20 MB can be used freely. Use your userid and password provided by them through email.

package com.javanbeyond;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.util.jndi.JndiContext;

public class CamelJavaDSL extends RouteBuilder {
  private static CamelContext context;

  public CamelJavaDSL() throws Exception {
    JndiContext jndiContext = new JndiContext();
    // Will be used in ftp component
    jndiContext.bind("ftpConfig", getFtpConfig());
    context = new DefaultCamelContext(jndiContext);

  public void configure() throws Exception {
    // noop - don't delete file in the source.
    // recursive - look into subfolders.
    // include - pick the files whose names matches the regex.
    // autoCreate - create folders in output if not available.
    // fileName - rename the output files based on the pattern mentioned by
    // file expression.
    // keepLastModified - preserves the Last Modified date of source files
    // in the destination folder.
    // setBody method replaces the message body with the content provided.
      .setBody(simple("${body} Javanbeyond"))
        // We can configure additional options on the ftpClient
        // from the URI directly by using the "ftpClient." prefix
        // Additionally, FTPClientConfig class can be referred
        // by its name in Jndi Context binding


  public CamelContext getCamelContext() {
    return context;

  public FTPClientConfig getFtpConfig() {
    // Configuration for ftp component
    FTPClientConfig ftpConfig = new FTPClientConfig();
    return ftpConfig;


6b. Camel Spring XML DSL

file:camel-context.xml. Spring XML equivalent to Java DSL .

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

	<camelContext id="camel" xmlns="">
	<!--         noop - keeps file in the source
				 recursive - looks into subfolders
				 include - picks the files whose names matches the regex
				 autoCreate - create folders in output if the available
				 fileName - rename the output files based on the pattern mentioned by file expression
				 keepLastModified - preserves the Last Modified date of source files in the destination folder.
				 setBody method replaces the message body with the content provided
		<route id="executeFileComponent" autoStartup="false">
			<from uri="file:input?noop=true&amp;recursive=true&amp;include=.*\.txt" />
			<!-- Using simple expression to append string in message body -->
				<simple>${body} Javanbeyond</simple>
				uri="file:output?autoCreate=true&amp;fileName=${file:name.noext}_cp.${file:name.ext}&amp;keepLastModified=true" />

		<!--  We can configure additional options on the ftpClient
						 from the URI directly by using the "ftpClient." prefix.
						 Additionally, FTPClientConfig class can be referred
						 by its id in Spring context -->
		<route id="executeFtpComponent" autoStartup="false">
			<from uri="file:input/sub-folder?noop=true" />
				uri=";password=XXXXXX&amp;ftpClient.dataTimeout=30000&amp;ftpClientConfig=#ftpConfig" />
	<bean id="ftpConfig" class="">
		<property name="serverLanguageCode" value="en" />



Example Execution:

Case 1: File component

  1. Make changes to Main class by calling either executeJavaDSL or executeSpringDSL with “executeFileComponent” as argument and commenting other methods.
  2. Run the example by running maven clean and install and passing either “xml” or “java” to the Main Class.
  3. Wait for the program to end. Refresh the project.
  4. An output folder containing text files should get created with file names changed and “javanbeyond” added to the content.



Case 2: Ftp component

  1. Make changes to Main class by calling either executeJavaDSL or executeSpringDSL with “executeFtpComponent” as argument and commenting other methods.
  2. If needed, configure ftp server name and username and password in ftp URIs.
  3. Run the example by running maven clean and install and passing either “xml” or “java” to the Main Class.
  4. You should see the input1.txt file inside ftp server. Using Filezilla, following is the view:




Back to Top