Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine latest file from SFTP server using Java JSch

Tags:

java

sftp

jsch

Is there a way to determine the name of latest file on Unix SFTP server using Java JSch?

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.

like image 864
Anuja Avatar asked Sep 05 '26 06:09

Anuja


2 Answers

I based my solution on code posted in Finding file size and last modified of SFTP oldest file using Java, with the following modification:

  • Change the comparision of nextTime and currentOldestTime from if (nextTime < currentOldestTime) to if (nextTime > currentOldestTime). This now picks up the latest file.
like image 59
Anuja Avatar answered Sep 06 '26 19:09

Anuja


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

}
like image 25
ASR Avatar answered Sep 06 '26 20:09

ASR