使用 Java JSch 确定来自 SFTP 服务器的最新文件

Determine latest file from SFTP server using Java JSch

有没有办法使用 Java JSch 确定 Unix SFTP 服务器上最新文件的名称?

我想将最新的文件从服务器复制到本地机器。我已经有了一个工作代码。但我无法识别最新文件。该文件夹包含以下格式的许多文件:

Some Report dd/MM/yyyy hh:ss

我尝试了 this post 中提到的代码,但它没有获取最新的文件。此外,代码似乎永远不会停止执行。

如有任何帮助,我们将不胜感激。

根据您提到的 post this post

有一行代码说只过滤 xml 文件你有没有改变它来检查所有文件格式

list = Main.chanSftp.ls("*.xml");

还有执行线程1分钟调用sleep方法

Thread.sleep(60000);

所以希望代码 运行 至少一分钟

这可能有帮助

我的解决方案基于 Finding file size and last modified of SFTP oldest file using Java 中发布的代码,并进行了以下修改:

  • nextTimecurrentOldestTime 的比较从 if (nextTime < currentOldestTime) 更改为 if (nextTime > currentOldestTime)。这现在会选择最新的文件。

下面是使用jsch将最新文件从远程服务器复制到本地的工作程序:

package com.poc.client;

import java.util.Vector;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;

public class CopyFileRemoteToLocal {

    public static void main(String[] args) {
        String hostname = "hostName";
        String username = "userName";
        String password = "password";
        String copyFrom = "serverFilePath";
        String copyTo = "LocalFilePath"; 
        JSch jsch = new JSch();
        Session session = null;
        System.out.println("Trying to connect.....");
        try {
            session = jsch.getSession(username, hostname, 22);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect(); 
            Channel channel = session.openChannel("sftp");
            channel.connect();
            ChannelSftp sftpChannel = (ChannelSftp) channel; 
            Vector<LsEntry> vector = (Vector<LsEntry>) sftpChannel.ls(copyFrom);
            ChannelSftp.LsEntry list = vector.get(0);
            String oldestFile =list.getFilename();
            SftpATTRS attrs=list.getAttrs();
            int currentOldestTime =attrs.getMTime();
            String nextName=null;
            LsEntry lsEntry=null;
            int nextTime;
            for (Object sftpFile : vector) {
                lsEntry = (ChannelSftp.LsEntry) sftpFile;
                nextName = lsEntry.getFilename();
                attrs = lsEntry.getAttrs();
                nextTime = attrs.getMTime();
                if (nextTime > currentOldestTime) {
                    oldestFile = nextName;
                    currentOldestTime = nextTime;
                }
            }

            System.out.println("File name is ...."+oldestFile);
            sftpChannel.get(copyFrom+oldestFile, copyTo);
            sftpChannel.exit();
            session.disconnect();
        } catch (JSchException e) {
            e.printStackTrace();  
        } catch (SftpException e) {
            e.printStackTrace();
        }

        System.out.println("Done !!");
    }

}

ChannelSftp.LsEntry lastModifiedEntry = Collections.max(列表, (比较器。 comparingInt(character1 -> character1.getAttrs().getMTim​​e()) .thenComparingInt(character2 -> character2.getAttrs().getMTim​​e())));

您可以获得文件夹中最后修改的条目。

它可以用 Collections 或 stream 完成,因为 channelSftp.ls returns Vector of LsEntry 和 Vector.class 与 Java-Collections:

完全兼容
Vector<LsEntry> list = channelSftp.ls(filePath + "*.txt");
ChannelSftp.LsEntry lastModifiedEntry = Collections.max(list,
    (Comparator.comparingInt(entry-> entry.getAttrs().getMTime()))
);

或者

LsEntry lastModifiedEntry = list.stream().max(
    Comparator.comparingInt(entry -> entry.getAttrs().getMTime())
).get();