javaspringspring-integrationspring-integration-sftpspring-integration-file

How to upload file via SFTP inside an IntegrationFlow chain using Spring Integration?


What I'm trying to do is to check my local source directory for new file(s), do a simple transformation then send file(s) to a remote server via SFTP Using Spring Integration.

I'm using file adapter to poll my source directory for new files. Now, I want to upload the file to remote server right after transforming it. Is there a message handler that do exactly that so I can trigger the upload inside my IntegrationFlow chain or should I write the file and SFTP upload it on a separate process (Create a scheduled job just for upload process)?

    @Bean
    public IntegrationFlow integrationFlow() {
        return IntegrationFlows.from(fileReader(), spec -> spec.poller(Pollers.fixedDelay(1000)))
                .transform(transformer, "transform")
                .handle( message handler )
                .get();
    }

    @Bean
    public FileReadingMessageSource fileReader() {
        FileReadingMessageSource source = new FileReadingMessageSource();
        source.setDirectory(new File("src/main/resources/file/outbox"));
        return source;
    }

Updated Code:

@Bean
    public IntegrationFlow integrationFlow() {
        return IntegrationFlows.from(fileReader(), spec -> spec.poller(Pollers.fixedDelay(1000)))
                .transform(transformer, "transform")
                .handle(Sftp.outboundAdapter(sftpSessionFactory)
                        .remoteDirectory(sftpRemoteDirectory)
                )
                .get();
    }

Solution

  • You are on the right track. There is indeed a channel adapter for SFTP to upload payload of the message as a file into a remote directory:

    handle(Sftp.outboundAdapter(sessionFactory(), FileExistsMode.FAIL)
    

    See more info in the SFTP chapter of the Spring Integration documentation:

    https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#sftp-outbound