Spring Integration: SFTP Download using Key-Based Authentication

This example will demonstrate how to use Spring Integration for downloading files from a remote SFTP server. Two possible authentications could be used, i.e. public key or password.

Technologies used:

  1. Spring Boot 1.5.2.RELEASE
  2. Spring Integration 4.3.8.RELEASE (managed by Spring Boot)
  3. Spring 4.3.7.RELEASE (managed by Spring Boot)

Quick overview:

  1. Create SFTP Session Factory, i.e. DefaultSftpSessionFactory
  2. Create and set up InboundChannelAdapter to regularly check a remote SFTP server for new files
  3. Create MessageHandler to process incoming files

1. Project Structure

A final project directory structure.
spring-integration sftp download example

2. SftpConfig using Java Configuration

We have to configure SFTP Session Factory (DefaultSftpSessionFactory) with all required parameters, i.e. host, IP port, username and password (or private key with a passphrase). The same configuration was already used in the SFTP Upload Example.

After that, we have to create a MessageSource<File> bean and define it as a @InboundChannelAdapter. This component is responsible for regularly checking a remote SFTP server whether a new file exists. A regular period defines annotation @Poller inside definition of InboundChannelAdapter (Poller is defined by the cron expression).

Then, we need to create an instance of a SftpInboundFileSynchronizer (will be used by @InboundChannelAdapter), which defines a strategy of a synchronization mechanism, i.e. we are able to set remote filename filters (sftpRemoteDirectoryDownloadFilter), a remote directory path (sftpRemoteDirectoryDownload) or whether a remote file should be deleted after a successful transfer.

The last important bean is related to a general MessageHandler bean used as a @ServiceActivator. The MessageHandler processes incoming files.

public class SftpConfig {

    private String sftpHost;

    private int sftpPort;

    private String sftpUser;

    private Resource sftpPrivateKey;

    private String sftpPrivateKeyPassphrase;

    private String sftpPasword;

    private String sftpRemoteDirectoryDownload;

    private String sftpLocalDirectoryDownload;

    private String sftpRemoteDirectoryDownloadFilter;

    public SessionFactory<LsEntry> sftpSessionFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
        if (sftpPrivateKey != null) {
        } else {
        return new CachingSessionFactory<LsEntry>(factory);

    public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
        SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
                .setFilter(new SftpSimplePatternFileListFilter(sftpRemoteDirectoryDownloadFilter));
        return fileSynchronizer;

    @InboundChannelAdapter(channel = "fromSftpChannel", poller = @Poller(cron = "0/5 * * * * *"))
    public MessageSource<File> sftpMessageSource() {
        SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
        source.setLocalDirectory(new File(sftpLocalDirectoryDownload));
        source.setLocalFilter(new AcceptOnceFileListFilter<File>());
        return source;

    @ServiceActivator(inputChannel = "fromSftpChannel")
    public MessageHandler resultFileHandler() {
        return new MessageHandler() {
            public void handleMessage(Message<?> message) throws MessagingException {

3. Setup Spring Boot with Spring Integration

I have used Spring Boot in my example, so annotation @SpringBootApplication is obvious. The more interesting annotation is @IntegrationComponentScan and @EnableIntegration which will enable all other configurations used in the previous configuration file.

public class SpringSftpDownloadDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringSftpDownloadDemoApplication.class, args);

4. Example of Usage

Here you can see a basic use case. I have created an integration test using a real SFTP server with enabled public key authentication (i.e. without password).
This test starts an asynchronous thread to check an existence of a downloaded file.

@TestPropertySource(properties = { "sftp.port = 10022", "sftp.remote.directory.download.filter=*.xxx"})
public class SpringSftpDownloadDemoApplicationTests {

    private static EmbeddedSftpServer server;

    private static Path sftpFolder;

    private String localDirectoryDownload;

    public static void startServer() throws Exception {
        server = new EmbeddedSftpServer();
        sftpFolder = Files.createTempDirectory("SFTP_DOWNLOAD_TEST");
        // Starting SFTP
        if (!server.isRunning()) {

    public void clean() throws IOException {

    public void testDownload() throws IOException, InterruptedException, ExecutionException, TimeoutException {
        // Prepare phase
        Path tempFile = Files.createTempFile(sftpFolder, "TEST_DOWNLOAD_", ".xxx");

        // Run async task to wait for expected files to be downloaded to a file
        // system from a remote SFTP server
        Future<Boolean> future = Executors.newSingleThreadExecutor().submit(new Callable<Boolean>() {
            public Boolean call() throws Exception {
                Path expectedFile = Paths.get(localDirectoryDownload).resolve(tempFile.getFileName());
                while (!Files.exists(expectedFile)) {
                return true;

        // Validation phase
        assertTrue(future.get(10, TimeUnit.SECONDS));

    public static void stopServer() {
        if (server.isRunning()) {

The source code of this project could be found on my public Github profile.

Share this: