我是使用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);
我的建议是使用最适合SFTP运营的JSCH框架。由于该Apache Common VFS
固有地使用了该框架。CCD_ 2将大大降低复合度。