3

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

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

类 GenericFileName 将根的 absolutePath 设置为“/”,这使得方法 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/

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

亲切的问候巴里

4

2 回答 2

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);
    }
}
于 2017-09-05T15:48:01.133 回答
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 );

我希望这可以帮助某人,如果没有,那么为下次这难倒我提供参考。

于 2019-07-17T14:41:11.727 回答