Retrieving files from FTP server using Spring Integration

J Riyana
Nerd For Tech
Published in
3 min readApr 15, 2021

--

In this article, I am going to show you how to retrieve files from an FTP server to a local directory using Spring Integration.

For this, I am using the Apache FTP server. You can download the latest version from this link. ( https://mina.apache.org/ftpserver-project/downloads.html ) Download it and unzip the file.

As I mentioned in the heading we need to include the following maven dependency in our pom.xml. Here we use the spring-integration-ftp dependency to allow spring-boot to connect with the FTP server.

maven dependencies

Now let's implement the code.

Following is the code for the FTP configuration. I created an FTPConfiguration.java file.

FTPConfiguration.java

Now let’s see what we did here.

sf()

This method instantiates a DefaultFtpSessionFactory to access the FTP server and sets the localhost as the host, 2121 as the port, anonymous as the Username, and a null string as the password.

getFile()

This method defines and constructs an FTPOutboundGateway bypassing the sf() method, mget command (mget is the spring FTP adapter command that use to retrieve multiple files from the FTP server) and the payload argument. Here the ServiceActivator indicates that the gateway is a spring-managed bean. The output of this method is a set of files. I named it “fileResults”. And it saves the retrieved files in the downloads folder.

Here, FTPOutboundGateway is used to perform remote file operations and Payload is a set of data that can perform functions like get, ls, put and etc.

fileResults()

This method creates a DirectChannel and adds a wireTap bean to it as an interceptor.

Here the wireTap method creates a copy of the message to a secondary target while sending the original message to the main channel.

logger()

This method creates a logging handler object and sends the message to the logging channel. Here the wiretap channel is wired to the logging handler.

GateFile interface

Here I declared a MessagingGateway to this interface and FTP mget as the request channel and fileResults as the reply channel. Then the result is sent to the main method a list of string objects

Next, we have to create a runner method to run this as a spring application.

Following is the code which should include in the springApplication.java

springApplication.java

Here the runner() method takes the GateFile interface as the argument and fetches the output.

That's all about the coding part. Now let's see how to run this application.

1st step - Start the FTP server.

Go to the bin folder which is inside the apache FTP server folder. Open the command prompt and execute the following command.

This apache FTP server stores files inside the home folder. (C:\apache-ftpserver-1.1.1\apache-ftpserver-1.1.1\res\home)

2nd step - run the spring-boot application.

Output

It prints the path of each file stored in the download folder.

--

--

J Riyana
Nerd For Tech

I am an undergraduate in the Faculty of Information Technology University of Moratuwa, Sri Lanka.