【问题标题】:Download a single file via FTP with Spring Integration使用 Spring Integration 通过 FTP 下载单个文件
【发布时间】:2017-05-26 05:31:01
【问题描述】:

我正在阅读Spring Integration Documentation,认为文件下载很容易实现。相反,这篇文章为我提供了许多不同的组件,这些组件似乎超出了我的需求:

FTP 入站通道适配器是一个特殊的侦听器,它将连接到 FTP 服务器并侦听远程目录事件(例如,创建的新文件),此时它将启动文件传输。

流式入站通道适配器生成带有 InputStream 类型有效负载的消息,允许在不写入本地文件系统的情况下获取文件。

假设我有一个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);
}

如何从这里下载给定 URL 上的单个文件?

【问题讨论】:

  • 为什么不使用 org.apache.commons.net.ftp.FTPClient

标签: java spring ftp spring-integration


【解决方案1】:

您可以使用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"))));
    }

}

【讨论】:

    【解决方案2】:

    添加到@garyrussell 的答案:

    FTPS协议中,如果你在防火墙后面,你可能会遇到 Host attempting data connection x.x.x.x is not the same as server y.y.y.y 错误(如 here 所述)。原因是 DefaultFtpsSessionFactory 返回的 FtpSession 实例默认进行远程验证测试,即它以“活动”模式运行。

    解决方案是在创建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;
        }
    

    【讨论】:

      【解决方案3】:

      以下代码块可能会有所帮助

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-23
        • 2012-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-04
        相关资源
        最近更新 更多