我尝试使用相对路径通过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();
...


我也尝试过“ /..”、“/../”,所有都异常。什么是上级目录的正确方法?

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

最佳答案

搞定了。

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();
        }
    }
}

关于java - Apache VFS相对路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29254796/

10-10 11:36