CraigW
CraigW

Reputation: 23

Overwriting a file using Spring mina SFTP

I have created a client based on the example on the Spring web page

https://docs.spring.io/spring-integration/reference/sftp/inbound.html#configuring-with-java-configuration

I did change the setMaxFetchSize to -1 to transfer all of the files from the remote site and the SftpSimplePatternFileListFilter to '*.html' as I want to transfer web pages.

The problem is that the first run of the code tranfsers the files but subsequent runs display the following message

The remote file 'xxxxx.html' has not been transferred to the existing local file 'sftp-inbound\xxxxxx.html'. Consider removing the local file.

The web pages are generated and transferred to the SFTP server every 5 minutes. I'd like to overwrite the local files if a new version of the file exists on the server. I have looked though the Spring documentation but can't see a method defined to do this. How can the local files be overwritten?

Upvotes: 0

Views: 112

Answers (1)

CraigW
CraigW

Reputation: 23

I am a beginner with Spring and I did find the documentation useful but there are a lot of things not mentioned in it that I needed to find from elsewhere.

I ended up with a mashup the Configuring with Java Configuration and Configuring with Java DSL (link below) as I wanted to preserve the timestamp of the files from the server. The @Poller annotation does not appear to have an option to preserve a timestamp:

https://docs.spring.io/spring-integration/reference/sftp/inbound.html#configuring-with-the-java-dsl

The Configuring with Java DSL seems to be missing the following declaration in the class:

private DefaultSftpSessionFactory sftpSessionFactory;

In the Sftp.inboundAdapter I added the following filter - the default AcceptOnceFileListFilter seems to only transfer the files the first time. If the files are updated on the remote system, they are not downloaded again.

                                .filter(new AcceptAllFileListFilter<>())

As I wanted to transfer all the updated files I set the maxFetchSize to -1 (negative numbers indicate all files)

                                .maxFetchSize(-1)

This still didn't transfer all of the updated files so I also had to add the following to the poller

                                        .maxMessagesPerPoll(-1)))

The final IntegrationFlow is below - note that remote_dir is a String set earlier in the class.

        return IntegrationFlow
                .from(Sftp.inboundAdapter(this.sftpSessionFactory)
                                .preserveTimestamp(true)
                                .maxFetchSize(-1)
                                .deleteRemoteFiles(false)
                                .filter(new AcceptAllFileListFilter<>())
                                .remoteDirectory(remote_dir)
                                .patternFilter("*.html")
                                .localDirectory(new File("html")),
                        e -> e.id("sftpInboundAdapter")
                                .autoStartup(true)
                                .poller(Pollers
                                        .fixedDelay(300000)
                                        .maxMessagesPerPoll(-1)))
                .handle(m -> System.out.println("Transferring updated file " + m.getPayload()))
                .get();

Upvotes: 0

Related Questions