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

问题描述 投票:0回答:1

我正在使用 Apache Common VFS 并成功连接到服务器。
我已经阅读了文档,但我被困在这段代码中。我如何列出目录/文件?

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();
}

请帮助我,
先谢谢你了:)

java ftp file-transfer jsch apache-commons-vfs
1个回答
9
投票

可以使用 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);

我的建议是使用最适合 SFTP 操作的 JSCH 框架。由于这个

Apache Common VFS
本身就使用了这个框架。
JSCH
复杂度会大大降低。

© www.soinside.com 2019 - 2024. All rights reserved.