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

问题描述 投票:0回答:4

是否有一种方法可以使用Java JSch确定Unix SFTP服务器上最新文件的名称?

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

Some Report dd/MM/yyyy hh:ss

我尝试了this post中提到的代码,但未获取最新文件。而且代码似乎永远不会停止执行。

任何帮助将不胜感激。

java sftp jsch
4个回答
1
投票

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

  • nextTimecurrentOldestTime的比较从if (nextTime < currentOldestTime)更改为if (nextTime > currentOldestTime)。现在,它将获取最新文件。

0
投票

根据您引荐的帖子this post

有一条代码行说只过滤xml文件,您是否更改了它以检查所有文件格式

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

还有在执行线程上调用睡眠方法1分钟

Thread.sleep(60000);

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

这可能有帮助


0
投票

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

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 !!");
    }

}

0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.