1

同事们,我正在尝试使用 spring 集成从 sftp 下载包含文件的文件夹。

我可以从一个文件夹同步文件20161207

@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
   SftpInboundFileSynchronizer fileSynchronizer =
      new SftpInboundFileSynchronizer(sftpSessionFactory());
   fileSynchronizer.setDeleteRemoteFiles(false);
   fileSynchronizer.setRemoteDirectory("/pub/op/20161207/");
   fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.xml"));
   return fileSynchronizer;
}

但是,如果需要从多个文件夹(等)下载(同步)文件,我该怎么2016120820161209?你有什么例子吗?谢谢你。

更新 我尝试使用 SftpRegexPatternFileListFilter,但没有帮助:

  @Bean
    public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
        SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
        fileSynchronizer.setDeleteRemoteFiles(false);
        fileSynchronizer.setRemoteDirectory("/pub/op/");
        Pattern pattern = Pattern.compile(".*\\.xml$");
        SftpRegexPatternFileListFilter sftpRegexPatternFileListFilter  = new SftpRegexPatternFileListFilter(pattern);
        fileSynchronizer.setFilter(sftpRegexPatternFileListFilter);
        //fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.xml"));
        return fileSynchronizer;
    }

更新 代码根据建议在这里被替换:

@Bean @ServiceActivator(inputChannel = "sftpChannel") 
    public MessageHandler handler() 
    { SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), "MGET", "payload"); 
        sftpOutboundGateway.setLocalDirectory(new File("sftp-inbound")); 
        return sftpOutboundGateway;}

更新 Artem,加里,谢谢你的帮助。这是我的代码:@Bean public DefaultSftpSessionFactory sftpSessionFactory() {

        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
        factory.setHost(server);
        factory.setPort(port);
        factory.setAllowUnknownKeys(true);
        factory.setUser(login);
        factory.setPassword(pass);
        factory.setTimeout(60*1000);
        return factory;
    }


@Bean
    @ServiceActivator(inputChannel = "sftpChannel")
    public MessageHandler handler() {
        SftpOutboundGateway sftpOutboundGateway = new  SftpOutboundGateway(sftpSessionFactory(), "mget", "getPayload() == '/pub/op/20170130/test.xml'");
        sftpOutboundGateway.setLocalDirectory(new File("C:/test/gateway/"));
        return sftpOutboundGateway;
    }

当我启动应用程序时,我收到下一个输出

12:25:58.593 INFO  [main] o.s.c.s.DefaultLifecycleProcessor : Starting beans in phase -2147483648
12:25:58.593 INFO  [main] o.s.i.endpoint.EventDrivenConsumer : Adding {sftp:outbound-gateway:aameFtpConfig.handler.serviceActivator} as a subscriber to the 'sftpChannel' channel
12:25:58.593 INFO  [main] o.s.i.channel.DirectChannel : Channel 'org.springframework.context.annotation.AnnotationConfigApplicationContext@4157f54e.sftpChannel' has 1 subscriber(s).
12:25:58.593 INFO  [main] o.s.i.endpoint.EventDrivenConsumer : started aameFtpConfig.handler.serviceActivator
12:25:58.594 INFO  [main] o.s.c.s.DefaultLifecycleProcessor : Starting beans in phase 0
12:25:58.594 INFO  [main] o.s.i.endpoint.EventDrivenConsumer : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
12:25:58.594 INFO  [main] o.s.i.c.PublishSubscribeChannel : Channel 'org.springframework.context.annotation.AnnotationConfigApplicationContext@4157f54e.errorChannel' has 1 subscriber(s).
12:25:58.594 INFO  [main] o.s.i.endpoint.EventDrivenConsumer : started _org.springframework.integration.errorLogger

但文件没有复制到"C:/test/gateway/". 请告诉我我做错了什么?

4

1 回答 1

1

尝试使用SftpOutboundGatewaywithMGET命令。

该动态远程目录可以作为请求消息。

您可以使用调用入站通道适配器的原始方法来实现轮询逻辑。而这个可以生成要发送的目录名。

您可以考虑在网关之后使用 Idempotent Receiver,以确保不会再次处理相同的文件。

Sftp 入站通道适配器无法为您执行此操作,因为它的目的是检查相同资源中的新数据。

于 2017-01-29T14:37:23.323 回答