singhal
singhal

Reputation: 117

fetching files from SFTP

I want to fetch files from SFTP which are created after a given timestamp(time of last pull) in java. I am using j2ssh as of now. Please let me know if some other API supports such a feature.

Upvotes: 0

Views: 494

Answers (1)

Mike
Mike

Reputation: 3311

Jsch supports the ls command which will bring you back all the attributes of the remote file. You can write a little code to eliminate the files you want to retrieve from there.

Java Doc: http://epaul.github.io/jsch-documentation/javadoc/

This example compares the remote file timestamps to find the oldest file, it wouldn't be much of a stretch to modify it to compare your last run date against the remote file date, then do the download as part of the loop.

Code from Finding file size and last modified of SFTP file using Java

try {
    list = Main.chanSftp.ls("*.xml");
    if (list.isEmpty()) {
        fileFound = false;
    }
    else {
        lsEntry = (ChannelSftp.LsEntry) list.firstElement();
        oldestFile = lsEntry.getFilename();
        attrs = lsEntry.getAttrs();
        currentOldestTime = attrs.getMTime();
        for (Object sftpFile : list) {
            lsEntry = (ChannelSftp.LsEntry) sftpFile;
            nextName = lsEntry.getFilename();
            attrs = lsEntry.getAttrs();
            nextTime = attrs.getMTime();
            if (nextTime < currentOldestTime) {
                oldestFile = nextName;
                currentOldestTime = nextTime;
            }
        }

Upvotes: 0

Related Questions