同事们,我正在尝试使用 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,加里,谢谢你的帮助。这是我的代码:@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/"
. 请告诉我我做错了什么?