如何使用Apache Common vfs列出文件目录/文件

fan*_*aid 8 ftp jsch file-transfer apache-commons-vfs

我是使用Apache Common vfs的新手,我成功连接到我已经阅读过docs的服务器但是我坚持使用这段代码.我如何列出目录/文件?

....
Session session = null;
        FileSystemManager fsManager = null;
        FileSystem fs = null;
        try {
            String host = "host_here";
            int port = 22;

            String userStr = "user_here";
            char [] username = userStr.toCharArray();

            String passStr = "password_here";
            char [] password = passStr.toCharArray();

            session = SftpClientFactory.createConnection(host, port, username, password, null);
            //session.connect();

            System.out.println("Connected to the server");

            FileSystemOptions opts = new FileSystemOptions();
            fsManager = VFS.getManager();
            FileObject file = fsManager.resolveFile("ftp://"+userStr+":"+passStr+"@"+host+"/home/", opts);    

            // .... whats next i do here? .....

        } catch (Exception e) {
            session.disconnect();
            e.printStackTrace();
        }
...
Run Code Online (Sandbox Code Playgroud)

请帮帮我,谢谢你:)

SRy*_*SRy 6

可以使用FileObject#getChildren()方法显示文件列表.

FileSystemOptions opts = new FileSystemOptions();
fsManager = VFS.getManager();

// List all the files in that directory.Try to give the directory path  
FileObject localFileObject=fsManager.resolveFile("ftp://"+userStr+":"+passStr+"@"+host+"/home");
FileObject[] children = localFileObject.getChildren();
for ( int i = 0; i < children.length; i++ ){
    System.out.println( children[ i ].getName().getBaseName() );
}
// End of List Files.

FileObject file = fsManager.resolveFile("ftp://"+userStr+":"+passStr+"@"+host+"/home/", opts);
Run Code Online (Sandbox Code Playgroud)

我的建议是使用最适合SFTP操作的JSCH框架.由于这Apache Common VFS固有地使用了这个框架.复杂城市将大大减少JSCH.