Apache VFS 相对路径
Apache VFS relative path
我尝试使用相对路径通过 Apache VFS 获取文件夹的父级,但我得到 "Invalid relative path"
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 库是否在 class 路径中。
我尝试使用相对路径通过 Apache VFS 获取文件夹的父级,但我得到 "Invalid relative path"
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 库是否在 class 路径中。