Reputation: 777
I'm new to SFTP protocol. I need to list all the files and folders from a server using SFTP protocol. I implemented this using the JSch library:
public ArrayList<JSONObject> listFiles(String deviceName, String location) throws Exception
{
this.sftpLogin();
Vector fileListVector;
if (Strings.isNullOrEmpty(location))
{
fileListVector = channelSftp.ls("/");
} else
{
fileListVector = channelSftp.ls("/"+location);
}
ArrayList<JSONObject> fileList = new ArrayList<>();
for (Object aFileListVector : fileListVector)
{
ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) aFileListVector;
if (entry.getFilename().equalsIgnoreCase(".") || entry.getFilename().equalsIgnoreCase(".."))
{
continue;
}
SftpATTRS attrs = entry.getAttrs();
fileList.add(ImportProtocolUtils.getFileJSONObject(attrs.isDir(), location, entry.getFilename()));
}
return fileList;
}
I tried 'shell' and 'exec' channel using this the protocol. But command 'ls' is not working.
Which is the best library for this in Java?
Thanks in advance.
Upvotes: 0
Views: 2796
Reputation: 202652
You have to recurse into the subdirectories.
Something like:
if (attrs.isDir())
{
fileList.addAll(listFiles(deviceName, location + "/" + entry.getFilename());
}
See also Display remote directory/all files in Jtree.
Upvotes: 1