java - 列出远程服务器目录中的文件名
问题描述
我想递归地列出远程目录及其子目录中的文件。我知道可以通过调用 ListGateway 的 listFiles 方法来做到这一点:
列表列表 = listGateway.listFiles("/ussama/providers")
@MessagingGateway
public interface ListGateway {
@Gateway(requestChannel = "listSftpChannel")
List<File> listFiles(String dir);
}
@Bean
@ServiceActivator(inputChannel = "listSftpChannel")
public MessageHandler handler() {
SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), "ls", "'/directory'");
return sftpOutboundGateway;
}
@Bean
public IntegrationFlow sftpOutboundListFlow() {
return IntegrationFlows.from("listSftpChannel")
.handle(new SftpOutboundGateway(sftpSessionFactory(), "ls", "payload")
).get();
}
但我想每隔 x 分钟做一次。有没有办法可以轮询远程目录以在每 x 分钟后列出文件。请给java配置。
解决方案
轮询目录的简单 POJO 消息源并根据需要配置轮询器...
@Bean
public IntegrationFlow pollLs(SessionFactory<LsEntry> sessionFactory) {
return IntegrationFlows.from(() -> "foo/bar", e -> e
.poller(Pollers.fixedDelay(5, TimeUnit.SECONDS)))
.handle(Sftp.outboundGateway(sessionFactory, Command.LS, "payload")
.options(Option.RECURSIVE))
.handle(System.out::println)
.get();
}
显然,您将需要一些服务.handle
来接收List<LsEntry>
结果。
顺便说一句,有一个工厂类Sftp
可以方便地创建端点。
推荐阅读
- git - 权限被拒绝(公钥)。GitLab 突然出现错误。我正确配置了私钥和公钥
- python - 用双引号括起来的逗号读取 csv
- c - 二进制表达式的错误操作数无效
- arrays - 如何从数据库中过滤食谱?
- data-structures - 为什么还要有哈希表?
- node.js - 为什么我必须刷新页面才能连接到对等系统?
- python - 将字典理解与包含的字符串拆分操作一起使用
- jupyter-notebook - 如何确保我的 jupyter notebook 可以在任何其他计算机或任何 jupyter Lab 上运行?
- laravel - 从数据透视表中删除数据时,也要从主表中删除
- .net - Razor Pages 不使用它的布局