Apache Commons VFS:使用 FTP

nin*_*nja 3 java ftp apache-commons-vfs

我正在尝试通过 FTP 使用 Apache Commons VFS。在我的 FTP 上有下一个文件和文件夹结构:

/
/test
/test/in
/test/in/file1.txt
/test/in/file2.txt
Run Code Online (Sandbox Code Playgroud)

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

        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());
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

FileType.IMAGINARY 表示该文件不存在。任何想法如何使用 ftp 文件夹?

nin*_*nja 5

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

FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);
Run Code Online (Sandbox Code Playgroud)