使用Jsch列出远程服务器中的所有文件

时间:2016-02-12 09:17:33

标签: java remote-server jsch ls

我正在尝试使用JSCH从远程服务器列出所有文件/目录,我也能够获取所有信息..

但我的问题是JSCH列出了所有具有文件创建日期,时间戳,读/写权限等类型的文件..,

但在我的情况下,我只需要远程服务器中的文件/目录名称,并且不需要其他信息..

以下是我的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();
        }
    }
}

上述计划的结果是

-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 ..

4 个答案:

答案 0 :(得分:15)

尝试运行此代码。这里我们将列表元素类型转换为LsEntry,然后打印所需的属性。

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<filelist.size();i++){
                LsEntry entry = (LsEntry) filelist.get(i);
                System.out.println(entry.getFilename());
            }
        }catch(Exception ex){
            ex.printStackTrace();
        } finally {
            if(session != null) session.disconnect();
            if(channel != null) channel.disconnect();
        }
    }
}

答案 1 :(得分:0)

使用LSEntrySelector访问返回文件列表的属性。

import java.util.Vector;

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


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<String> filelist=new Vector<String>();

            LsEntrySelector selector = new LsEntrySelector() {
                public int select(LsEntry entry)  {
                    final String filename = entry.getFilename();
                    if (filename.equals(".") || filename.equals("..")) {
                        return CONTINUE;
                    }
                    if (entry.getAttrs().isLink()) {
                        filelist.addElement(filename);
                    }
                    else if (entry.getAttrs().isDir()) {
                        //if (keepDirectory(filename)) {
                        filelist.addElement(entry.getFilename());
                        //}
                    }
                    else {
                        //if (keepFile(filename)) {
                        filelist.addElement(entry.getFilename());
                        //}
                    }
                    return CONTINUE;
                }
            };
            channelSftp.ls(SFTPWORKINGDIR,selector);
            for(int i=0; i<filelist.size();i++){
                System.out.println(filelist.get(i).toString());
            }

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

答案 2 :(得分:0)

对于sftp,您可以使用以下命令获取文件列表

Vector<ChannelSftp.LsEntry> list = channelSftp.ls("*.*");
for(ChannelSftp.LsEntry entry:list) {
    System.out.println(entry.getFilename());
}

,并且没有内置功能来获取execshell的列表文件 协议,您已经从InputStream数据中进行了解析。

答案 3 :(得分:-1)

尝试exec 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());
 }