微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从 Sftp 过滤并获取远程文件名列表

如何解决从 Sftp 过滤并获取远程文件名列表

我需要从修改时间超过给定时间的 sftp 服务器获取远程文件名列表,然后从 sftp 中删除所有这些文件,我正在使用 SftpOutboundGatewayNLST 命令列出文件

但是它从给定的远程目录路径获取所有文件,我尝试了 .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();

如何过滤和获取文件名。我不想使用 mget 命令

更新

一个句柄方法中的 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?