我试图使用相对路径用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();
}
}
}
还要验证jcraft jsch库是否在类路径中。