2016-02-12 1 views
0

Je suis en train de lister tous les fichiers/répertoire à partir d'un serveur distant en utilisant jsch et je peux en mesure d'obtenir toutes les informations et ..liste tous les fichiers serveur distant à l'aide jsch

Mais mon problème est la liste jsch tous les fichiers avec date de création de fichier, horodatage, type de permission de lecture/écriture etc.,

Mais dans mon cas j'ai juste besoin du nom du fichier/répertoire sur le serveur distant et aucune information supplémentaire n'est requise ..

Ci-dessous est mon morceau de code java ..

import java.util.Vector; 

import com.jcraft.jsch.Channel; 
import com.jcraft.jsch.ChannelSftp; 
import com.jcraft.jsch.JSch; 
import com.jcraft.jsch.Session; 


public class Listremoteserver { 


    /** 
    * @param args 
    */ 
    @SuppressWarnings("unchecked") 
    public static void main(String[] args) { 
     String SFTPHOST = "xxxxx"; 
     int SFTPPORT = 22; 
     String SFTPUSER = "xxx"; 
     String SFTPPASS = "xxxxx"; 
     String SFTPWORKINGDIR = "/root"; 

     Session  session  = null; 
     Channel  channel  = null; 
     ChannelSftp channelSftp = null; 

     try{ 
      JSch jsch = new JSch(); 
      session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT); 
      session.setPassword(SFTPPASS); 
      java.util.Properties config = new java.util.Properties(); 
      config.put("StrictHostKeyChecking", "no"); 
      session.setConfig(config); 
      session.connect(); 
      channel = session.openChannel("sftp"); 
      channel.connect(); 
      channelSftp = (ChannelSftp)channel; 
      channelSftp.cd(SFTPWORKINGDIR); 
      Vector filelist = channelSftp.ls(SFTPWORKINGDIR); 
      for(int i=0; i<filelist.size();i++){ 
       System.out.println(filelist.get(i).toString()); 
      } 

     }catch(Exception ex){ 
      ex.printStackTrace(); 
     } 
    } 
} 

Les résultats du programme ci-dessus est

-rw-r--r-- 1 root  root   3161 Feb 11 2014 install.log.syslog 
-rw-r--r-- 1 root  root   18 May 20 2009 .bash_logout 
-rw-r--r-- 1 root  root   176 Sep 23 2004 .bashrc 
-rw-r--r-- 1 root  root   176 May 20 2009 .bash_profile 
-rw-r--r-- 1 root  root   129 Dec 3 2004 .tcshrc 
-rw------- 1 root  root   1114 Feb 11 2014 anaconda-ks.cfg 
dr-xr-x--- 2 root  root   4096 Feb 11 2014 . 
-rw-r--r-- 1 root  root   9169 Feb 11 2014 install.log 
-rw------- 1 root  root   1055 Feb 11 2014 .bash_history 
-rw-r--r-- 1 root  root   100 Sep 23 2004 .cshrc 
dr-xr-xr-x 24 root  root   4096 Feb 12 04:19 .. 

Répondre

-1

essayer exec commande ls:

Channel channel=session.openChannel("exec"); 
    ((ChannelExec)channel).setCommand("cd " + SFTPWORKINGDIR + " && ls"); 
    channel.connect(); 
    channel.run(); 

Vector filelist = channel.run(); 
for(int i=0; i<filelist.size();i++){ 
    System.out.println(filelist.get(i).toString()); 
} 
+1

@CodelsLife: Cela fonctionne bien! –

+0

La ligne 5 renvoie une erreur indiquant que channel.run() renvoie void. –

+0

oui son donne une erreur à la ligne 5. @CodelsLife pouvez-vous mettre à jour la réponse –

6

Essayez d'exécuter ce code. Ici nous typecasting les éléments de liste à LsEntry, puis en imprimant l'attribut requis.

import java.util.Vector; 

import com.jcraft.jsch.Channel; 
import com.jcraft.jsch.ChannelSftp; 
import com.jcraft.jsch.ChannelSftp.LsEntry; 
import com.jcraft.jsch.JSch; 
import com.jcraft.jsch.Session; 


public class Listremoteserver { 


    /** 
    * @param args 
    */ 
    @SuppressWarnings("unchecked") 
    public static void main(String[] args) { 
     String SFTPHOST = "xxxxx"; 
     int SFTPPORT = 22; 
     String SFTPUSER = "xxx"; 
     String SFTPPASS = "xxxxx"; 
     String SFTPWORKINGDIR = "/root"; 

     Session  session  = null; 
     Channel  channel  = null; 
     ChannelSftp channelSftp = null; 

     try{ 
      JSch jsch = new JSch(); 
      session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT); 
      session.setPassword(SFTPPASS); 
      java.util.Properties config = new java.util.Properties(); 
      config.put("StrictHostKeyChecking", "no"); 
      session.setConfig(config); 
      session.connect(); 
      channel = session.openChannel("sftp"); 
      channel.connect(); 
      channelSftp = (ChannelSftp)channel; 
      channelSftp.cd(SFTPWORKINGDIR); 
      Vector filelist = channelSftp.ls(SFTPWORKINGDIR); 
      for(int i=0; i<filelist.size();i++){ 
       LsEntry entry = (LsEntry) filelist.get(i); 
       System.out.println(entry.getFilename()); 
      } 

     }catch(Exception ex){ 
      ex.printStackTrace(); 
     } 
    } 
} 
+0

Merci pour votre réponse rapide .. Il a résolu mon problème ... –