简体   繁体   English

Apache Commons VFS:使用FTP

[英]Apache Commons VFS: working with FTP

I'm trying to use Apache Commons VFS with FTP. 我正在尝试使用Apache Commons VFS和FTP。 On my FTP a have the next structure of files and folders: 在我的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). 我需要连接并读取文件夹/ test / in中的所有文件(它一直在变化)。 Code: 码:

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

        FileObject path = fsManager.resolveFile("ftp://user:password@my.ftp.host/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. 但我无法获得文件列表,因为它表示/ test / in不存在。 A made some tests to check file types: System.out.println(path.getType()); 做了一些测试来检查文件类型: System.out.println(path.getType()); with different paths. 有不同的路径。 Results: 结果:

ftp://user:password@my.ftp.host/test - file ftp:// user:password@my.ftp.host/test - 文件

ftp://user:password@my.ftp.host/test/in - imaginary ftp:// user:password@my.ftp.host/test/in - imaginary

ftp://user:password@my.ftp.host/test/in/file1.txt - file ftp:// user:password@my.ftp.host/test/in/file1.txt - file

FileType.IMAGINARY means that file does not exist. FileType.IMAGINARY表示该文件不存在。 Any ideas how to work with ftp folders? 任何想法如何使用ftp文件夹?

只需为ftp设置'被动'模式:

FtpFileSystemConfigBuilder.getInstance().setPassiveMode(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 需要解析的文件夹是/ 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. 深入挖掘表明FileObjectisReadable()exists()返回false意味着无法访问FileObject Looking at the source of AbstractFileObject , it was depending on these checks to determine the directory (Check AbstractFileObject getParent() ). 查看AbstractFileObject的源代码,它依赖于这些检查来确定目录(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 . 问题是AbstractFileObject查看相对于文件系统root的文件,除非它被显式设置为使用User目录作为根目录,因此错过了传递的文件路径。 So the solution was to set the FtpFileSystemConfigBuilder indicating to consider user directory as the root. 所以解决方案是设置FtpFileSystemConfigBuilder指示将用户目录视为根。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM