File Transfer from Source Directory to Destination Directory using File Adapter in Spring Integration


We will create Spring integration file adapter example. This example will show you how to transfer file from source directory to destination directory using file adapter in Spring integration or how to move a file from source to destination folder or how to poll a directory at an interval to move file to the destination folder. You may also decide to delete or not after the file has been moved to the destination folder by setting auto delete flag to true.


Eclipse 4.12, Java 8 or 12, Gradle 5.6, Spring Boot 2.1.8

Creating Project

Create a gradle based project in Eclipse with the project’s name as spring-integration-file-transfer.

Updating Build Script

The default generated build.gradle script has to be updated to include the required dependencies.

We have two dependencies for Spring integration and file adapter in the below build script.

buildscript {
	ext {
		springBootVersion = '2.1.8.RELEASE'
    repositories {
    dependencies {

apply plugin: 'java'
apply plugin: 'org.springframework.boot'

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {

Creating Application Properties

We will create an file under src/main/resources which will content key/value pair properties.

For example, we will put input directory name from where file will be moved to another directory.

We will also put destination directory where the file will be moved.

Creating Config Class

We will create a config class that contains several configurations for the applications, such as, creation of Spring beans, reading properties file etc.

package com.jeejava.spring.integration.file.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.integration.config.EnableIntegration;

public class FileConfig {

	private Environment env;
	// ...


Creating Main Class

Main class with @SpringBootApplication is enough to deploy the application into embedded Tomcat server.

We will run the application using CommandLineRunner.

package com.jeejava.spring.integration.file;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringIntegrationFileApp implements CommandLineRunner {

	public static void main(String[] args) {, args);

	public void run(String... args) throws Exception {


Example – Moving File

We will create a simple example where we will transfer a file from a particular directory into a destination directory.

Creating FileReadingMessageSource Bean

We need to create a FileReadingMessageSource bean in order to read the file from a source directory. We will mark this bean as an input channel adapter.

We have to configure poller to poll the directory at a certain interval to check whether a new file is there or not. Another reason is you will get exception if you do not configure poller when you are using FileReadingMessageSource as inbound channel adapter.

java.lang.IllegalArgumentException: No poller has been defined for Annotation-based endpoint, and no default poller is available within the context.

We will configure the source directory into this bean to read the file from the source directory.

Related Posts:

We will create service activator that will sit between input channel adapter and output channel adapter. We will configure output channel adapter for destination directory.

Put the below code snippets into the FileConfig class.

@InboundChannelAdapter(value = "fileInputChannel", poller = @Poller(fixedDelay = "1000"))
public FileReadingMessageSource fileReadingMessageSource() {
	FileReadingMessageSource fileReadingMessageSource = new FileReadingMessageSource();
	fileReadingMessageSource.setDirectory(new File(env.getProperty("")));
	return fileReadingMessageSource;

@ServiceActivator(inputChannel = "fileInputChannel")
public FileWritingMessageHandler fileWritingMessageHandler() {
	FileWritingMessageHandler fileWritingMessageHandler = new FileWritingMessageHandler(
			new File(env.getProperty("")));
	return fileWritingMessageHandler;

In the above code snippets we have created FileWritingMessageHandler to move the file from source to destination directory.

We have configured destination directory, auto creation of directory if directory does not exist.

You may also configure whether you want to delete the file from the source directory once moved to the destination directory.

You need to setExpectRelay() to false because FileWritingMessageHandler uses by default request/reply mechanism. If you do not set it to false then you will see below exception:

org.springframework.messaging.MessageHandlingException: error occurred in message handler [fileWritingMessageHandler]; nested exception is org.springframework.messaging.core.DestinationResolutionException: no output-channel or replyChannel header available

Testing the Application

Run the main class and you will see the file has been moved to the destination directory:

file transfer from source directory to destination directory using file adapter in spring integration

That’s all. Hope you got an idea on moving a file from source to destination directory using Spring integration.

Source Code


Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *