【问题标题】:How to synchronise files from more than one folder between Ftp and local folder using Spring Integration?如何使用 Spring Integration 在 Ftp 和本地文件夹之间同步多个文件夹中的文件?
【发布时间】:2017-01-29 08:54:11
【问题描述】:

同事们,我正在尝试使用 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;}

更新 阿尔乔姆,加里,谢谢你的帮助。 这是我的代码: @豆 公共 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/"。 请告诉我我做错了什么?

【问题讨论】:

    标签: java spring spring-integration spring-integration-sftp


    【解决方案1】:

    尝试将SftpOutboundGatewayMGET 命令一起使用。

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

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

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

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

    【讨论】:

    • 从 4.2 版开始,您可以使用 synchronizer.setRemoteDirectoryExpression 使用 SpEL 表达式来生成将在每次轮询时检查的目录 - 您可以通过这种方式轮换多个目录 - 但没有办法在一次投票中扫描多个目录;他们都将被拉到同一个本地目录。正如@Artem 所说,您可以使用 MGET(带递归)从多个目录中获取。
    • 我是 Spring 新手...我需要添加 bean,例如:` @Bean @ServiceActivator(inputChannel = "sftpChannel") public MessageHandler handler() { SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory (), "MGET", "有效载荷"); sftpOutboundGateway.setLocalDirectory(new File("sftp-inbound"));返回 sftpOutboundGateway; }` 在我的配置中?
    • 是的,否则无法使SftpOutboundGateway工作
    • 不要将代码/配置放在 cmets 中;如您所见,它的渲染效果很差并且难以阅读。相反,请编辑您的问题并添加评论以表明您已这样做(我们不会收到编辑通知)。
    猜你喜欢
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多