Collegues,我正在尝试使用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;}
更新 Artem,Gary,谢谢你的帮助。 这是我的代码: @豆 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/"
。
请告诉我我做错了什么?
答案 0 :(得分:1)
尝试使用SftpOutboundGateway
MGET
命令。
该动态远程目录可以作为请求消息。
您可以使用原始方法调用入站通道适配器来实现轮询逻辑。这个可以生成要发送的目录名称。
您可以考虑在网关后使用Idempotent Receiver,以确保不再处理相同的文件。
Sftp入站通道适配器无法为您执行此操作,因为它的目的是检查相同资源以获取新数据。