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

标签 ftp file-transfer jsch apache-commons-vfs

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

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

最佳答案

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

我的建议是使用JSCH最适合 SFTP 操作的框架。由于Apache Common VFS本身就使用了这个框架。JSCH将大大降低复杂度。

关于ftp - 如何使用 Apache Common vfs 列出文件目录/文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14932439/

相关文章:

Java、jsch 和 EC2...无法在 root 中运行应用程序

ubuntu - SFTP 到谷歌计算引擎

r - 将 RCurl 与嵌入 null 的 ftp 服务器结合使用

java-me - 如何使用蓝牙从 J2me 应用程序发送文件

sftp - 将文件传输至 session /从 session 传输文件 我已使用 PuTTY 登录

java - 使用 JSch 分别为各个提示提供输入

ftp - Azure网站FTP访问/530用户无法登录

java - Java:从FTP下载.Zip文件并提取内容而不将文件保存在本地系统上

python - 安全直接连接文件传输程序

java - 等待 JSch 命令执行,而不是硬编码一个固定的等待时间