2015-03-25 75 views
0

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

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

我尝试“/ ..”,“/../”为好吧,都有异常。父目录的正确路径是什么?

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

回答

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:[email protected]/",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(); 
     } 
    } 
}