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

时间:2017-09-05 11:06:00

标签: ftp apache-commons-vfs cwd

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

但是如果启用了UriStyle,那么所有内容都会相对地解析为root。如果根不是" //"

,这将不会是一个问题

类GenericFileName将根的absolutePath设置为" /",这使得方法getPath()返回" /" + getUriTrailer(),在这种情况下root总是返回" //"。相对于//解决的所有内容都有两个点前进到它们的路径。

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

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/

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

亲切的问候 百里

2 个答案:

答案 0 :(得分:1)

修正如下所述:

再次禁用uriStyle。 写了我自己的VFS类,它创建了我的自定义编写的Manager。 该管理器用我的自定义覆盖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);
    }
}

答案 1 :(得分:0)

因为我在以下问题上遇到了同样的问题,所以来回答这个问题

ftp://user:pass@host//home/user/file.txt

成为...(请注意“ home”后的单斜杠)

ftp://user:pass@host/home/user/file.txt

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

// 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 );

我希望这可以对某人有所帮助,如果没有帮助的话,请在下一次遇到麻烦时提供参考。