VFS - Frequently asked questions

Is there a tutorial for VFS?

todo: Add link(s) here to tutorials and example code.

VfsCookbook - a cookbook has been started

How can I enter ftp passive mode?

FileSystemOptions opts = new FileSystemOptions();
FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);
FileObject fo = VFS.getManager().resolveFile("ftp://host/path_to/file.txt", opts);

How do I use the org.apache.commons.vfs.FileSelector or org.apache.commons.vfs.FileFilter?

The FileFilter can be used to mimic the java.io.FileFilter interface.

FileFilter ff = new FileFilter()
{
    public boolean accept(FileSelectInfo fileInfo)
    {
        FileObject fo = fileInfo.getFile();
        return fo.getName().getBaseName().startsWith("ABC-");
    }
                
};
VFS.getManager().resolveFile("/base/folder").findFiles(new FileFilterSelector(ff));

You should consider using the more VFS native org.apache.commons.vfs.FileSelector interface as this allows you to automatically traverse recursive.

Available FileSelectors:

So the following will be the same as above, but will scan the whole tree starting at the base folder.

FileSelector ff = new FileSelector()
{
    public boolean includeFile(FileSelectInfo fileInfo) throws Exception
    {
        FileObject fo = fileInfo.getFile();
        return fo.getName().getBaseName().startsWith("ABC-");
    }

    public boolean traverseDescendents(FileSelectInfo fileInfo) throws Exception
    {
        return true;
    }                   
};
VFS.getManager().resolveFile("/base/folder").findFiles(ff);

How do I keep an SFTP connection from hanging?

If your program is transferring files, but not exiting afterward, this is actually an issue with properly closing down the connection when you are done. If you acquired a FileSystemManager with

private FileSystemManager fsManager = VFS.getManager();

the getManager() method probably returned a StandardFileSystemManager which extends the DefaultFileSystemManager class and implements the FileSystemManager interface.

What you are probably after is the close() method of the DefaultFileSystemManager to clean up any temporary files and close all providers. Cast the FileSystemManager to a DefaultFileSystemManager access the close() method like this:

((DefaultFileSystemManager) fsManager).close();

How can I connect to an SFTP server even if there is no entry in known_hosts?

JSch, the library that provides VFS with SFTP functionality, checks that the host one tries connecting to is a known host, i.e. has an entry in the file $HOME/.ssh/known_hosts. By default, connections to unkown hosts are rejected by throwing an exception. You can change this behaviour by specifying the strict host key checking option:

FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
FileSystemManager fsManager = VFS.getManager();
FileObject foo = fsManager.resolveFile("sftp://login:password@host/foo.bar", opts);

// Do some stuff

foo.close();
((DefaultFileSystemManager) fsManager).close();

VfsFaq (last edited 2009-09-20 23:48:37 by localhost)