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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:06:49  来源:igfitidea点击:

Apache Commons VFS: working with FTP

javaftpapache-commons-vfs

提问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 DefaultFileMonitorand was listening in on FileChangeEventfor 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 falsemeaning that the FileObjectis not accessible. Looking at the source of AbstractFileObject, it was depending on these checks to determine the directory (Check AbstractFileObjectgetParent()).

深入挖掘表明FileObject's isReadable()exists()返回false意味着FileObject不可访问。查看 的来源AbstractFileObject,它依赖于这些检查来确定目录(检查AbstractFileObjectgetParent())。

Issue was that AbstractFileObjectlook 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 FtpFileSystemConfigBuilderindicating to consider user directory as the root.

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

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