同事们,我正在尝试使用 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;
}
但是,如果需要从多个文件夹(20161208
、20161209
等)下载(同步)文件,我该怎么办?
你有什么例子吗?
谢谢。
更新 我尝试使用 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;}
更新 阿乔姆,加里,谢谢你的帮助。 这是我的代码: @ 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/”
。
请告诉我我做错了什么?
最佳答案
尝试将 SftpOutboundGateway
与 MGET
命令结合使用。
该动态远程目录可以作为请求消息。
您可以通过调用入站 channel 适配器的原始方法来实现轮询逻辑。并且这个可以生成要发送的目录名。
您可以考虑在网关之后使用幂等接收器,以确保不会再次处理相同的文件。
Sftp 入站 channel 适配器无法为您执行此操作,因为它的目的是检查同一资源中是否有新数据。
关于java - 如何使用 Spring Integration 在 Ftp 和本地文件夹之间同步多个文件夹中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41919162/