Search code examples
javaftpapache-commons-vfs

Apache Commons VFS: working with 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?


Solution

  • Just set 'passive' mode for ftp:

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