Download a single file via FTP with Spring Integration

10,165

Solution 1

You can use an FtpRemoteFileTemplate...

@SpringBootApplication
public class So44194256Application implements CommandLineRunner {

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

    @Bean
    public DefaultFtpSessionFactory ftpSessionFactory() {
        DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
        sf.setHost("10.0.0.3");
        sf.setUsername("ftptest");
        sf.setPassword("ftptest");
        return sf;
    }

    @Bean
    public FtpRemoteFileTemplate template(DefaultFtpSessionFactory sf) {
        return new FtpRemoteFileTemplate(sf);
    }

    @Autowired
    private FtpRemoteFileTemplate template;

    @Override
    public void run(String... args) throws Exception {
        template.get("foo/bar.txt",
                inputStream -> FileCopyUtils.copy(inputStream, 
                      new FileOutputStream(new File("/tmp/bar.txt"))));
    }

}

Solution 2

To add to @garyrussell's answer:

In FTPS protocol, if you are behind a firewall, you will might encounter Host attempting data connection x.x.x.x is not the same as server y.y.y.y error (as described here). The reason is the FtpSession instance returned from DefaultFtpsSessionFactory by default does remote verification test, i.e. it runs in an "active" mode.

The solution is to disable this verification on the FtpSession instance by setting the "passive mode", when you create the DefaultFtpsSessionFactory.

DefaultFtpsSessionFactory defaultFtpsSessionFactory() {

        DefaultFtpsSessionFactory defaultFtpSessionFactory = new DefaultFtpsSessionFactory(){
            @Override
            public FtpSession getSession() {
                FtpSession ftpSession = super.getSession();
                ftpSession.getClientInstance().setRemoteVerificationEnabled(false);
                return ftpSession;
            }
        };
        defaultFtpSessionFactory.setHost("host");
        defaultFtpSessionFactory.setPort(xx);
        defaultFtpSessionFactory.setUsername("username");
        defaultFtpSessionFactory.setPassword("password");
        defaultFtpSessionFactory.setFileType(2); //binary data transfer

        return defaultFtpSessionFactory;
    }
Share:
10,165
cozyconemotel
Author by

cozyconemotel

I write Ruby and Java for work, but I'm a big fan of the Rust language. “When nothing seems to help, I go and look at a stonecutter hammering away at his rock, perhaps a hundred times without as much as a crack showing in it. Yet at the hundred and first blow it will split in two, and I know it was not that last blow that did it, but all that had gone before.”

Updated on June 13, 2022

Comments

  • cozyconemotel
    cozyconemotel almost 2 years

    I was reading through the Spring Integration Documentation thinking that a file download would be pretty simple to implement. Instead, the article provided me with many different components that seem to over-qualify my needs:

    The FTP Inbound Channel Adapter is a special listener that will connect to the FTP server and will listen for the remote directory events (e.g., new file created) at which point it will initiate a file transfer.

    The streaming inbound channel adapter produces message with payloads of type InputStream, allowing files to be fetched without writing to the local file system.

    Let's say I have a SessionFactory declared as follows:

    @Bean
    public SessionFactory<FTPFile> ftpSessionFactory() {
        DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
        sf.setHost("localhost");
        sf.setPort(20);
        sf.setUsername("foo");
        sf.setPassword("foo");
        return new CachingSessionFactory<>(sf);
    }
    

    How do I go from here to downloading a single file on a given URL?

    • Scary Wombat
      Scary Wombat almost 7 years
      why not use org.apache.commons.net.ftp.FTPClient