2013-02-18 12 views
7

Jestem nowy za pomocą Apache Common vfs, I sukces połączyć z serwerem Już czytałem dokumenty, ale utknąłem w tym kodzie. Jak mogę wyświetlić katalog/pliki?Jak wyświetlić katalog plików/plików za pomocą 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(); 
     } 
... 

Proszę mi pomóc, zanim Dziękuję :)

Odpowiedz

6

Lista plików mogą być wyświetlane za pomocą FileObject#getChildren() metody.

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

Moja sugestia byłoby użyć JSCH ramy, która jest najlepsza dla operacji SFTP. Ponieważ ten Apache Common VFS z natury użył tego szkieletu. Kompleksowość znacznie zmniejszy się o JSCH.

+0

Dziękuję srinivas, Teraz używam JSCH: D Ale zastanawiam się, jak zapisać katalog (nie plik) do katalogu docelowego? – fanjavaid

+0

Masz na myśli tworzenie nowego katalogu lub zapisanie katalogu z już niektórymi plikami? – SRy

+0

Tak, w ten sposób. To jest możliwe? – fanjavaid

Powiązane problemy