gpt4 book ai didi

java - 如何使用 Java 列出 SFTP 服务器上的所有文件?

转载 作者:行者123 更新时间:2023-12-01 08:55:41 25 4
gpt4 key购买 nike

我是 SFTP 协议(protocol)的新手。我需要使用 SFTP 协议(protocol)列出服务器中的所有文件和文件夹。我使用 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;
}

我尝试使用该协议(protocol)的“shell”和“exec” channel 。但命令“ls”不起作用。

Java 中最好的库是哪个?

提前致谢。

最佳答案

您必须递归到子目录。

类似于:

if (attrs.isDir())
{
fileList.addAll(listFiles(deviceName, location + "/" + entry.getFilename());
}

另请参阅Display remote directory/all files in Jtree .

关于java - 如何使用 Java 列出 SFTP 服务器上的所有文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42052208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com