简体   繁体   English

Apache VFS 相对路径

[英]Apache VFS relative path

I try to get folder's parent with Apache VFS using relative path, but I get "Invalid relative path"我尝试使用相对路径通过 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();
...

I tried "/.." , "/../" as well, all got exception.我也试过 "/.." , "/../" ,都得到了例外。 What is the right way to the parent directory?访问父目录的正确方法是什么?

PS #getParent will not work, It's for files only, not directories. PS #getParent 不起作用,它仅适用于文件,不适用于目录。

Nailed it.搞定了。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM