List all files in remote server using Jsch

后端 未结 4 375
萌比男神i
萌比男神i 2021-01-14 02:38

I am trying to list all the files/directory from a remote server using JSCH and i can able to get all the information as well..

But my problem is JSCH list all the

4条回答
  •  太阳男子
    2021-01-14 03:04

    Try running this code. Here we are typecasting the list elements to LsEntry and then printing the required attribute.

    import java.io.File;
    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 = "/tmp";
            String SFTPPRIVATEKEY = "/path/to/xxxxxxxxx.pem";
    
            Session     session     = null;
            Channel     channel     = null;
            ChannelSftp channelSftp = null;
    
            try{
                JSch jsch = new JSch();
                File privateKey = new File(SFTPPRIVATEKEY);
                if(privateKey.exists() && privateKey.isFile())
                    jsch.addIdentity(SFTPPRIVATEKEY);
                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

提交回复
热议问题