简体   繁体   English

使用 Java JSch 从 SFTP 服务器确定最新文件

[英]Determine latest file from SFTP server using Java JSch

Is there a way to determine the name of latest file on Unix SFTP server using Java JSch?有没有办法使用 Java JSch 确定 Unix SFTP 服务器上最新文件的名称?

I want to copy the latest file from server to local machine.我想将最新的文件从服务器复制到本地机器。 I already have a working code for that.我已经有一个工作代码。 But I'm not able to identify the latest file.但我无法识别最新的文件。 The folder contains many files in below format:该文件夹包含以下格式的许多文件:

Some Report dd/MM/yyyy hh:ss

I tried the code mentioned in this post but it isn't picking up the latest file.我尝试了这篇文章中提到的代码,但它没有选择最新的文件。 Also the code never seems to stop executing.此外,代码似乎永远不会停止执行。

Any help would be appreciated.任何帮助将不胜感激。

I based my solution on code posted in Finding file size and last modified of SFTP oldest file using Java , with the following modification:我的解决方案基于使用 Java 查找文件大小和上次修改的 SFTP 最旧文件中发布的代码,并进行了以下修改:

  • Change the comparision of nextTime and currentOldestTime from if (nextTime < currentOldestTime) to if (nextTime > currentOldestTime) .nextTimecurrentOldestTime的比较从if (nextTime < currentOldestTime)更改为if (nextTime > currentOldestTime) This now picks up the latest file.现在选择最新的文件。

The below is the working program copying the leatest file from remote server to local using jsch:下面是使用 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 !!");
    }

}

According to the post you referred this post根据你提到这个帖子的帖子

There is code line which says to filter only xml file have you changed it to check all file format有代码行说只过滤 xml 文件你有没有改变它来检查所有文件格式

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

Also there is sleep method called on executing thread for 1 minute还有在执行线程时调用了 sleep 方法 1 分钟

Thread.sleep(60000);

so expect the code to run for atleast a minute所以希望代码至少运行一分钟

This might help这可能有帮助

ChannelSftp.LsEntry lastModifiedEntry = Collections.max(list, (Comparator. comparingInt(character1 -> character1.getAttrs().getMTime()) .thenComparingInt(character2 -> character2.getAttrs().getMTime()))); ChannelSftp.LsEntry lastModifiedEntry = Collections.max(list, (Comparator.comparatorInt(character1 -> character1.getAttrs().getMTim​​e()) .thenComparingInt(character2 -> character2.getAttrs().getMTim​​e())));

you can get the last modified Entry in the folder.您可以获取文件夹中最后修改的条目。

It could be done with Collections or stream since channelSftp.ls returns Vector of LsEntry and Vector.class is fully competible with Java-Collections:它可以通过集合或流来完成,因为 channelSftp.ls 返回向量的 LsEntry 并且 Vector.class 与 Java 集合完全兼容:

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

Or或者

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM