Apache Commons VFS:使用 FTP

发布于 2024-11-08 05:39:16 字数 1616 浏览 3 评论 0原文

我正在尝试将 Apache Commons VFS 与 FTP 结合使用。在我的 FTP 上,有以下文件和文件夹结构:

/
/test
/test/in
/test/in/file1.txt
/test/in/file2.txt

我需要连接并读取文件夹 /test/in 中的所有文件(它一直在变化)。代码:

        FileSystemManager fsManager = null;
        FileSystem fs = null;
        FileSystemOptions opts = new FileSystemOptions();
        fsManager = VFS.getManager();

        FileObject path = fsManager.resolveFile("ftp://user:[email protected]/test/in/", opts);

        fs = path.getFileSystem();

        //prints Connection successfully established to /test/in
        System.out.println("Connection successfully established to " + path.getName().getPath());

但是我无法获取文件列表,因为它说 /test/in 不存在。 A 进行了一些测试来检查文件类型:System.out.println(path.getType()); 具有不同的路径。结果:

ftp://user: [电子邮件受保护]/test - 文件

ftp://用户:[电子邮件受保护]/test/in - 虚构的

ftp://user:[电子邮件受保护]/ test/in/file1.txt - 文件

FileType.IMAGINARY 表示该文件不存在。 关于如何使用 ftp 文件夹有什么想法吗?

I'm trying to use Apache Commons VFS with FTP. On my FTP a have the next structure of files and folders:

/
/test
/test/in
/test/in/file1.txt
/test/in/file2.txt

I need to connect and read all files from folder /test/in (it changes all the time). Code:

        FileSystemManager fsManager = null;
        FileSystem fs = null;
        FileSystemOptions opts = new FileSystemOptions();
        fsManager = VFS.getManager();

        FileObject path = fsManager.resolveFile("ftp://user:[email protected]/test/in/", opts);

        fs = path.getFileSystem();

        //prints Connection successfully established to /test/in
        System.out.println("Connection successfully established to " + path.getName().getPath());

But I couldn't got file list, because it says that /test/in does not exist. A made some tests to check file types:System.out.println(path.getType()); with different paths. Results:

ftp://user:[email protected]/test - file

ftp://user:[email protected]/test/in - imaginary

ftp://user:[email protected]/test/in/file1.txt - file

FileType.IMAGINARY means that file does not exist.
Any ideas how to work with ftp folders?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏九 2024-11-15 05:39:16

只需为 ftp 设置“被动”模式:

FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);

Just set 'passive' mode for ftp:

FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);
挽容 2024-11-15 05:39:16

我遇到了类似的问题,单独设置被动模式并不能解决它。

需要解析的文件夹是 /FTP_HOME/data/xxxx

我正在使用 vfs2 DefaultFileMonitor 监视该文件夹,并监听 FileChangeEvent 但无济于事。

FileObject listendir =  fsManager.resolveFile("ftp://"+username+":"+password+"@"+server+"/data/" + "xxxx/",opts);

深入挖掘发现 FileObjectisReadable()exists() 返回 false 意味着 < code>FileObject 不可访问。
查看 AbstractFileObject 的源代码,它依赖于这些检查来确定目录(检查 AbstractFileObject getParent() )。

问题是,AbstractFileObject 查看相对于文件系统根目录的文件,除非明确将其设置为使用用户目录作为根目录,从而错过了传递的文件路径。因此解决方案是设置 FtpFileSystemConfigBuilder 指示将用户目录视为根目录。

FtpFileSystemConfigBuilder.getInstance( ).setUserDirIsRoot(opts,true);

I had a similar kind of an issue and setting the passive mode alone did not solve it.

The folder which needed to be resolved was /FTP_HOME/data/xxxx

I was monitoring the folder using the vfs2 DefaultFileMonitor and was listening in on FileChangeEvent for no avail.

FileObject listendir =  fsManager.resolveFile("ftp://"+username+":"+password+"@"+server+"/data/" + "xxxx/",opts);

Digging a little deeper showed that FileObject 's isReadable() and exists() returned false meaning that the FileObject is not accessible.
Looking at the source of AbstractFileObject, it was depending on these checks to determine the directory (Check AbstractFileObject getParent() ).

Issue was that AbstractFileObject look at the file relative to the file systems root unless it is explicitly set to use the User directory as the root, hence missing out on the file path which was passed . So the solution was to set the FtpFileSystemConfigBuilder indicating to consider user directory as the root.

FtpFileSystemConfigBuilder.getInstance( ).setUserDirIsRoot(opts,true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文