简体   繁体   English

使用 Spring Integration 通过 FTP 下载单个文件

[英]Download a single file via FTP with Spring Integration

I was reading through the Spring Integration Documentation thinking that a file download would be pretty simple to implement.我正在阅读Spring 集成文档,认为文件下载实现起来非常简单。 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 (eg, new file created) at which point it will initiate a file transfer. FTP 入站通道适配器是一个特殊的侦听器,它将连接到 FTP 服务器并侦听远程目录事件(例如,创建的新文件),此时它将启动文件传输。

The streaming inbound channel adapter produces message with payloads of type InputStream, allowing files to be fetched without writing to the local file system.流入站通道适配器生成具有 InputStream 类型有效负载的消息,允许在不写入本地文件系统的情况下获取文件。

Let's say I have a SessionFactory declared as follows:假设我有一个SessionFactory声明如下:

@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?如何从这里下载给定 URL 上的单个文件?

You can use an FtpRemoteFileTemplate ...您可以使用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"))));
    }

}

following code block might be helpful以下代码块可能会有所帮助

@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true) {
        {
            setHost("localhost");
            setPort(20);
            setUser("foo");
            setPassword("foo");
            setAllowUnknownKeys(true);
        }
    };
    return new CachingSessionFactory<LsEntry>(factory);
}

@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
    SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory()) {
        {
            setDeleteRemoteFiles(true);
            setRemoteDirectory("/remote");
            setFilter(new SftpSimplePatternFileListFilter("*.txt"));
        }
    };
    return fileSynchronizer;
}

@Bean
@InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "600"))
public MessageSource<File> sftpMessageSource() {
    SftpInboundFileSynchronizingMessageSource messageSource = new SftpInboundFileSynchronizingMessageSource(
            sftpInboundFileSynchronizer()) {
        {
            setLocalDirectory(new File("/temp"));
            setAutoCreateLocalDirectory(true);
            setLocalFilter(new AcceptOnceFileListFilter<File>());
        }
    };
    return messageSource;
}

obtained from https://github.com/edtoktay/spring-integratonhttps://github.com/edtoktay/spring-integraton获得

To add to @garyrussell's answer:添加到@garyrussell 的回答中:

In FTPS protocol, if you are behind a firewall, you will might encounter Host attempting data connection xxxx is not the same as server yyyy error (as described here ).FTPS协议,如果你是在防火墙后面,你将可能会遇到Host attempting data connection xxxx is not the same as server yyyy错误(如描述在这里)。 The reason is the FtpSession instance returned from DefaultFtpsSessionFactory by default does remote verification test, ie it runs in an "active" mode.原因是默认情况下从DefaultFtpsSessionFactory返回的 FtpSession 实例会进行远程验证测试,即它以“主动”模式运行。

The solution is to disable this verification on the FtpSession instance by setting the "passive mode", when you create the DefaultFtpsSessionFactory .解决方案是在创建DefaultFtpsSessionFactory时通过设置“被动模式”来禁用对 FtpSession 实例的此验证。

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Spring-Integration下载FTP文件? - FTP file download with Spring-Integration? 如何通过Spring集成通过具有TLS身份验证的ftp下载文件 - How to download file through ftp with tls authentication through spring integration 使用Spring Integration计划通过FTP进行远程文件下载并处理内存中的文件 - Schedule remote file download over FTP and process file in memory with Spring Integration 如何使用多线程通过FTP下载文件? - How to download file via FTP using multithreading? Spring Integration-FTP下载后立即发送消息 - Spring Integration - Send message immediately after FTP download 使用Spring Integration发送成功的FTP文件上传消息 - Sending Successful FTP File Upload Message with Spring Integration 在此Spring应用程序的Linux上执行问题,该问题通过Spring Integration FTP在FTP服务器上上传文件:MessagingException - Problems performing on Linux this Spring application that upload a file on an FTP server by Spring Integration FTP: MessagingException 如何通过ftp url下载/阅读html文件? - How to download/read html file via ftp url? 如何通过 FTP 下载文本文件并将内容读取为字符串 - How to download a text file via FTP and read the contents to a string 春季与ftp跳过文件集成 - Spring integration with ftp skip files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM