Search code examples
sftppollingjsch

SFTP polling using java


My scenario as follows: One java program is updating some random files to a SFTP location.

My requirement is as soon as a file is uploaded by the previous java program, using java I need to download the file. The files can be of size 100MB. I am searching for some java API which is helpful in this way. Here I even don't know the name of files. But I can keep a regular expression for this. A same file can be uploaded by previous program periodically. Since file size is high I need to wait until the complete file to be uploaded.

I used Jsch to download files, but I am not getting how to poll using jsch.


Solution

  • Polling

    All you can do is to keep listing remote directory periodically, until you find a new file. There's no better way with SFTP. For that you obviously use ChannelSftp.ls().

    Regarding selecting files matching certain pattern, see:
    JSch ChannelSftp.ls - pass match patterns in java

    Waiting until the upload is complete

    Again, there's no support for this in widespread implementations of SFTP.

    For details, see my answer at:
    SFTP file lock mechanism.