2

我尝试使用 Apache VFS 使用相对路径获取文件夹的父级,但我得到“无效的相对路径”

public static void main(String[] args) throws Exception {
FileSystemManager fileSystemManager = VFS.getManager();
FileObject fileObject = fileSystemManager
.resolveFile("sftp://myuser:mypassword@myhost/"); // works!!
FileObject root = fileObject.resolveFile("../"); // fails!!
FileObject fileObjects[] = root.getChildren();
...

我也试过 "/.." , "/../" ,都出现异常。父目录的正确方法是什么?

PS #getParent 将不起作用,它仅适用于文件,不适用于目录。

4

2 回答 2

1

搞定了。

public class Test {

    public static void main(String[] args) throws Exception {
        FileSystemOptions opts = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false);
        FileSystemManager fileSystemManager = VFS.getManager();
        FileObject fileObject = fileSystemManager
                .resolveFile("sftp://user:password@host/",opts);

        FileObject temp = fileObject.resolveFile("/foo/faa/frog/");
        FileObject fileObjects[] = temp.getChildren();

        try {
            for (FileObject j : fileObjects) {

                System.out.println(j.getName().getBaseName());
                j.close();
            }
        } finally {
            fileObject.close();
            temp.close();
        }
    }
}
于 2015-03-26T07:50:21.713 回答
0

还要验证 jcraft jsch 库是否在类路径中。

于 2021-02-09T14:59:55.200 回答