2013-02-18 86 views
7

我是新的使用Apache Common vfs, 我成功連接到服務器 我已閱讀文檔,但我被困在此代碼中。 我如何列出目錄/文件?如何使用Apache常見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()方法來顯示文件的:)

回答

6

名單。

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

+0

謝謝srinivas,現在我用JSCH:D 但是我想知道,如何將目錄(不是文件)保存到目標目錄? – fanjavaid 2013-02-24 14:34:49

+0

你的意思是創建新的目錄或保存一個已經有一些文件的目錄? – SRy 2013-02-24 19:18:09

+0

是的,就像那樣。這是可能的? – fanjavaid 2013-02-25 07:09:42