Sshj library for download multifiles in remote server

921 Views Asked by At

I use Java 8 and my condition is to download multiple file in a remote server using sftp protocol, is not necessary to filter file for his name but necessary to download all file in a specific remote folder.

i see the library com.hierynomus » sshj for this scope, but looking on the net i haven't found what i need, only for download a single file.

What i think is i could use this method,

String localDir = "/home";
String remoteFile = "/home/folder/*"
SSHClient sshClient = setupSshj();
SFTPClient sftpClient = sshClient.newSFTPClient();
sftpClient.get(remoteFile, localDir);

but i'm not sure if the asterisk in the "remoteFile" will be useful for my purpose...

Unfortunately for now i can't try this on remote server ...

Someone can help me?

Thank's everyone

1

There are 1 best solutions below

1
Jokkeri On

You need to LIST all the files you want to download:

List<RemoteResourceInfo> entries = sftpClient.ls("/home/folder")

After that you will loop the entries to download them one by one:

for (RemoteResourceInfo remoteFile : entries) {
    if(remoteFile.isRegularFile()){
        sftpClient.get(remoteFile.getPath(), localDir);
    }
}

E: Also you should check if the list entry is really a file, edited the code accordingly. Though I am not sure whether using !remoteFile.isDirectory() would be better.