gpt4 book ai didi

java - 列出远程服务器目录中的文件名

转载 作者:行者123 更新时间:2023-12-02 10:21:26 24 4
gpt4 key购买 nike

我想递归地列出远程目录及其子目录中的文件。我知道可以通过调用 ListGateway 的 listFiles 方法来做到这一点:

List list = 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具有创建端点的便捷方法。

关于java - 列出远程服务器目录中的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54324275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com