java Apache Commons VFS:使用 FTP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6046220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache Commons VFS: working with FTP
提问by ninja
I'm trying to use Apache Commons VFS with FTP. On my FTP a have the next structure of files and folders:
我正在尝试通过 FTP 使用 Apache Commons VFS。在我的 FTP 上,文件和文件夹的结构如下:
/
/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:
我需要连接并读取文件夹 /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());
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:
但是我无法获得文件列表,因为它说 /test/in 不存在。A 做了一些测试来检查文件类型:System.out.println(path.getType());
使用不同的路径。结果:
ftp://user:[email protected]/test- file
ftp://user:[email protected]/test- 文件
ftp://user:[email protected]/test/in- imaginary
ftp://user:[email protected]/test/in- 虚构
ftp://user:[email protected]/test/in/file1.txt- file
ftp://user:[email protected]/test/in/file1.txt- 文件
FileType.IMAGINARY means that file does not exist. Any ideas how to work with ftp folders?
FileType.IMAGINARY 表示该文件不存在。任何想法如何使用 ftp 文件夹?
回答by ninja
Just set 'passive' mode for ftp:
只需为 ftp 设置“被动”模式:
FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);
回答by Udara Bentota
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
需要解析的文件夹是/FTP_HOME/data/xxxx
I was monitoring the folder using the vfs2 DefaultFileMonitor
and was listening in on FileChangeEvent
for no avail.
我用的是VFS2监视的文件夹DefaultFileMonitor
,并在被监听FileChangeEvent
了也没有用。
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()
).
深入挖掘表明FileObject
's isReadable()
并exists()
返回false
意味着FileObject
不可访问。查看 的来源AbstractFileObject
,它依赖于这些检查来确定目录(检查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.
问题是 AbstractFileObject
查看相对于文件系统根目录的文件,除非它明确设置为使用用户目录作为根目录,因此错过了传递的文件路径。所以解决方案是设置FtpFileSystemConfigBuilder
指示以将用户目录视为根目录。
FtpFileSystemConfigBuilder.getInstance( ).setUserDirIsRoot(opts,true);