gpt4 book ai didi

ftp - apache VFS2 uriStyle - 根绝对路径以双斜杠结尾

转载 作者:行者123 更新时间:2023-12-03 03:37:52 32 4
gpt4 key购买 nike

在使用 vfs2 库的 ftp 服务器上工作时,我注意到,我必须启用 VFS.setUriStyle(true),以便该库将工作目录更改为我正在操作的目标文件的父目录(cwd directoryName) .

但是如果启用了 UriStyle,则所有内容都会相对于根进行解析。如果根不是“//”,这不会成为问题。

类 GenericFileName 将根的绝对路径设置为“/”,这使得方法 getPath() 返回“/”+getUriTrailer(),在根的情况下始终返回“//”。相对于//解析的所有内容都有两个点指向其路径。

这意味着如果我执行以下代码:

public class RemoteFileTest {
public static void main(String[] args) {
// Options for a RemoteFileObject connection
VFS.setUriStyle(true);
FileSystemOptions options = new FileSystemOptions();
// we doing an ftp connection, hence we use the ftpConfigBuilder
// we want to work in passive mode
FtpFileSystemConfigBuilder.getInstance().setPassiveMode(options, true);
FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(options, false);
// DefaultFileSystemConfigBuilder.getInstance().setRootURI(options, "/newRoot/");
// System.out.println(DefaultFileSystemConfigBuilder.getInstance().getRootURI(options));
// ftp://localhost:21/

StaticUserAuthenticator auth = new StaticUserAuthenticator("", "user", "pass");
try {
DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, auth);
} catch (FileSystemException e) {
e.printStackTrace();
return;
}

// A FileSystemManager creates an abstract FileObject linked to are desired RemoteFile.
// That link is just simulated and not yet real.
FileSystemManager manager;
try {
manager = VFS.getManager();
} catch (FileSystemException e) {
e.printStackTrace();
return;
}

try (FileObject remoteFile = manager.resolveFile("ftp://localhost:21/sub_folder/test.txt", options)) {

System.out.println("Is Folder " + remoteFile.isFolder());
System.out.println("Is File " + remoteFile.isFile());

} catch (FileSystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}

}}

我收到与 ftp 服务器的交互:

USER user
PASS ****
TYPE I
CWD //
SYST
PASV
LIST ..sub_folder/
PWD
CWD ..sub_folder/

我希望交互就像这样,但目录前面没有两个点。

亲切的问候巴里

最佳答案

按如下所述修复它:

再次禁用 uriStyle。编写了我自己的 VFS 类,它创建了我自定义编写的管理器。该管理器用我的自定义文件覆盖 FtpFileProvider,这只是将根设置为自定义选定的文件,从而导致所需的行为。

import org.apache.commons.vfs2.FileName;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystem;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder;
import org.apache.commons.vfs2.provider.ftp.FtpFileProvider;

public class AdvancedFtpFileProvider extends FtpFileProvider {
public AdvancedFtpFileProvider() {
super();
// setFileNameParser(AdvancedFtpFileNameParser.getInstance());
}

@Override
protected FileObject findFile(FileName name, FileSystemOptions fileSystemOptions) throws FileSystemException {
// Check in the cache for the file system
//getContext().getFileSystemManager().resolveName... resolves the configured RootUri relative to the selected root (name.getRoot()). This calls cwd to the selectedRoot and operates from there with relatives urls towards the new root!
final FileName rootName = getContext().getFileSystemManager().resolveName(name.getRoot(), DefaultFileSystemConfigBuilder.getInstance().getRootURI(fileSystemOptions));

final FileSystem fs = getFileSystem(rootName, fileSystemOptions);

// Locate the file
// return fs.resolveFile(name.getPath());
return fs.resolveFile(name);
}
}

关于ftp - apache VFS2 uriStyle - 根绝对路径以双斜杠结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46053421/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com