【问题标题】:How can I list all the files from an SFTP server using Java?如何使用 Java 列出来自 SFTP 服务器的所有文件?
【发布时间】:2017-02-05 12:45:11
【问题描述】:

我是 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 中哪个是最好的库?

提前致谢。

【问题讨论】:

    标签: java sftp jsch


    【解决方案1】:

    你必须递归到子目录。

    类似:

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

    另见Display remote directory/all files in Jtree

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 1970-01-01
      • 2023-03-10
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多