Anuja
Anuja

Reputation: 115

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?

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.

Upvotes: 3

Views: 7661

Answers (5)

selins sofa
selins sofa

Reputation: 91

It could be done with Collections or stream since channelSftp.ls returns Vector of LsEntry and Vector.class is fully competible with Java-Collections:

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();

Upvotes: 0

peridin
peridin

Reputation: 28

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

you can get the last modified Entry in the folder.

Upvotes: 0

ASR
ASR

Reputation: 3559

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

}

Upvotes: 1

Anuja
Anuja

Reputation: 115

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.

Upvotes: 2

K patel
K patel

Reputation: 64

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

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

Also there is sleep method called on executing thread for 1 minute

Thread.sleep(60000);

so expect the code to run for atleast a minute

This might help

Upvotes: 0

Related Questions