如何使用 Java 列出来自 SFTP 服务器的所有文件?
How can I list all the files from an SFTP server using Java?
我是 SFTP 协议的新手。我需要使用 SFTP 协议列出来自服务器的所有文件和文件夹。我使用 JSch 库实现了这个:
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;
}
我尝试使用此协议的 'shell' 和 'exec' 频道。但是命令 'ls' 不起作用。
在 Java 中,哪个图书馆最好?
提前致谢。
您必须递归到子目录。
类似于:
if (attrs.isDir())
{
fileList.addAll(listFiles(deviceName, location + "/" + entry.getFilename());
}
另见 Display remote directory/all files in Jtree。
我是 SFTP 协议的新手。我需要使用 SFTP 协议列出来自服务器的所有文件和文件夹。我使用 JSch 库实现了这个:
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;
}
我尝试使用此协议的 'shell' 和 'exec' 频道。但是命令 'ls' 不起作用。
在 Java 中,哪个图书馆最好?
提前致谢。
您必须递归到子目录。
类似于:
if (attrs.isDir())
{
fileList.addAll(listFiles(deviceName, location + "/" + entry.getFilename());
}
另见 Display remote directory/all files in Jtree。