Ftp ApacheVFS2 uriStyle-根绝对路径以双斜杠结尾

Ftp ApacheVFS2 uriStyle-根绝对路径以双斜杠结尾,ftp,apache-commons-vfs,cwd,Ftp,Apache Commons Vfs,Cwd,在使用vfs2库处理ftp服务器时,我注意到我必须启用VFS.setUriStyle(true),以便库将工作目录更改为我正在操作的目标文件的父目录(cwd directoryName) 但是,如果启用了UriStyle,则所有问题都将相对于根进行解析。如果根不是“/”,这就不是问题了 类GenericFileName将根的绝对路径设置为“/”,这使得方法getPath()返回“/”+GetUriTraile(),在根的情况下,它总是返回“/”。与//相关的每件事物都有两个点沿着它们的路径前进

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

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

类GenericFileName将根的绝对路径设置为“/”,这使得方法getPath()返回“/”+GetUriTraile(),在根的情况下,它总是返回“/”。与//相关的每件事物都有两个点沿着它们的路径前进

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

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/
我希望交互就像这样,但是目录前面没有两个点

问候
Barry

按如下所述对其进行了修复:

再次禁用URI样式。 编写了我自己的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://user:pass@host//home/user/file.txt
成为。。。(注意“home”后面的单斜杠)

我这样做是为了解决这个问题

// Setup some options, add as many as you need    
FileSystemOptions opts = new FileSystemOptions( );

// This line tells VFS to treat the URI as the absolute path and not relative
FtpsFileSystemConfigBuilder.getInstance( ).setUserDirIsRoot( opts, false );

// Retrieve the file from the remote FTP server    
FileObject realFileObject = fileSystemManager.resolveFile( fileSystemUri, opts );
我希望这能帮助一些人,如果没有那么提供了一个参考,为下一次这难倒我

// Setup some options, add as many as you need    
FileSystemOptions opts = new FileSystemOptions( );

// This line tells VFS to treat the URI as the absolute path and not relative
FtpsFileSystemConfigBuilder.getInstance( ).setUserDirIsRoot( opts, false );

// Retrieve the file from the remote FTP server    
FileObject realFileObject = fileSystemManager.resolveFile( fileSystemUri, opts );