如何解决从 Sftp 过滤并获取远程文件名列表
我需要从修改时间超过给定时间的 sftp 服务器获取远程文件名列表,然后从 sftp 中删除所有这些文件,我正在使用 SftpOutboundGateway
的 NLST
命令列出文件名
但是它从给定的远程目录路径获取所有文件,我尝试了 .filter(lastModifiedFileListFilter)
没有用,然后我也尝试了 .filterFunction(logicGiven)
也没有用
我想知道 NLST
命令是否支持过滤
这是代码示例:
过滤器实例:
private FileListFilter lastModifiedFilter(){
LastModifiedFileListFilter fileListFilter = new LastModifiedFileListFilter();
fileListFilter.setAge(120,TimeUnit.SECONDS);
return fileListFilter;
}
使用 filter() 方法:
@Bean
public IntegrationFlow deleteFiles(){
return IntegrationFlows.from("integration.channel.bulk-delete")
.handle(Sftp.outboundGateway(sftpSessionFactory(),AbstractRemoteFileOutboundGateway.Command.NLST,"headers[path]")
.filter(lastModifiedFilter()))
.get();
使用 filterFunction() 方法:
@Bean
public IntegrationFlow deleteFiles(){
return IntegrationFlows.from("integration.channel.bulk-delete")
.handle(Sftp.outboundGateway(sftpSessionFactory(),"headers[path]")
.filterFunction(file->{
Instant decidedTime = Instant.Now().minus(120,ChronoUnit.SECONDS);
return Instant.ofEpochSecond(file.getAttrs().getMTime()).isBefore(decidedTime);
}))
.log()
.get();
更新
下一个句柄方法中的 RM 命令不会删除,我还在 doRm()
方法中放置了一个断点,尽管我的 for 循环中有文件,但它并没有停在那里
@Bean
public IntegrationFlow deleteFiles(){
return IntegrationFlows.from("integration.channel.bulk-delete")
.handle(Sftp.outboundGateway(sftpSessionFactory(),AbstractRemoteFileOutboundGateway.Command.LS,"headers[path]")
.options(AbstractRemoteFileOutboundGateway.Option.NAME_ONLY)
.filterFunction(file->{
Instant decidedTime = Instant.Now().minus(120,ChronoUnit.SECONDS);
return Instant.ofEpochSecond(file.getAttrs().getMTime()).isBefore(decidedTime);
}))
.log()
.handle(fileList->{
List<String> files = (List<String>) fileList.getPayload();
for (String remoteFile: files) {
Sftp.outboundGateway(sftpSessionFactory(),AbstractRemoteFileOutboundGateway.Command.RM,"headers[file_remoteDirectory]+"+remoteFile);
}
})
.get();
解决方法
NLST 仅获取文件名,因此没有要过滤的时间戳。
NLST 目前根本不应用任何过滤器。
使用 LS(可能与递归 -R
选项一起使用)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。