0

Using An SFTP Server, Apache Camel and Spring Boot

Last Updated on

Applications can communicate with each other in various means. One is by uploading and downloading files to and from an FTP server. But for development, we often test our applications on our local machines. Therefore, it makes sense to have an FTP server running locally.

This post demonstrates how to upload and download (and read) files to and from an SFTP server using Spring Boot and Apache Camel.

First, Set Up Local Directory And An FTP Server

The fastest way to set up an SFTP server is by using an SFTP Docker image. Make sure the Docker container is running, and the FTP server is accessible before moving on.

Next, we set up a directory structure in our local machine for files to upload to the SFTP server. Then, we place files in the “in” directory, and our application moves to either of the other subdirectories accordingly.

We do not need to set up the directory structure in the FTP server. Apache Camel will do that for us as our application downloads the file.

Spring Boot With Apache Camel

The fastest way to create the initial Maven project with Spring Boot and Apache Camel dependencies is through Spring Initilzr. Then, we update or add codes to the project.

pom.xml And Apache Camel Dependency

The project is using Spring Boot 2.2.5.RELEASE and the Apache Camel version that comes with it is 3.0.1.

Therefore, we need to add a dependency to camel-ftp with the same version for consistency.

With camel-ftp, we can upload and download from an SFTP server using Apache Camel.

We also need httpclient for building the FTP URL to our SFTP server.

The complete pom.xml would be as follow.

Run Application As A Service

Uploading and downloading files to and from an SFTP server requires the application to be running all the time. The application allows the Apache Camel to regularly poll the SFTP Server and the local directory for new files to process.

Codes To Download Files

The following codes download files from the SFTP server. The application polls the “/foo-home/in” directory for files with “_TURRETA_” in their file names.

The SFTPService class allows us to display the content of every file the application reads.

Codes To Upload Files

The following codes poll the local directory “C:/Users/karldev/Desktop/New folder/local-windows-dir/in” for files that have “_TURRETA_” in their file names. Then, they upload them to the SFTP server in the “/foo-home/in” directory.

Sample Console Output

In summary, the application copy files from a local directory to a remote directory in the SFTP server. Then, it reads them and displays their contents.

Karl San Gabriel

Karl San Gabriel

Professional Software Developer