Apache Commons VFS:使用 FTP
我正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需为 ftp 设置“被动”模式:
Just set 'passive' mode for ftp:
我遇到了类似的问题,单独设置被动模式并不能解决它。
需要解析的文件夹是 /FTP_HOME/data/xxxx
我正在使用 vfs2
DefaultFileMonitor
监视该文件夹,并监听FileChangeEvent
但无济于事。深入挖掘发现
FileObject
的isReadable()
和exists()
返回false
意味着 < code>FileObject 不可访问。查看
AbstractFileObject
的源代码,它依赖于这些检查来确定目录(检查AbstractFileObject
getParent()
)。问题是,
AbstractFileObject
查看相对于文件系统根目录的文件,除非明确将其设置为使用用户目录作为根目录,从而错过了传递的文件路径。因此解决方案是设置FtpFileSystemConfigBuilder
指示将用户目录视为根目录。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 onFileChangeEvent
for no avail.Digging a little deeper showed that
FileObject
'sisReadable()
andexists()
returnedfalse
meaning that theFileObject
is not accessible.Looking at the source of
AbstractFileObject
, it was depending on these checks to determine the directory (CheckAbstractFileObject
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 theFtpFileSystemConfigBuilder
indicating to consider user directory as the root.