说明:需要只获得第一级文件夹目录 

package com.sunsheen.jfids.studio.monitor.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.Assert;

import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.StreamGobbler;


/**
 * 遍历出远程服务器上指定目录下的所有文件夹
 *
 * @author WangSong
 *
 */
public class GetAllSubfolders {

    private GetAllSubfolders(){}

    /**
     * 得到服务器上指定文件夹下所有子文件夹(第一级子文件夹)
     * @return
     */
    public static List<Object> getSubfolderName(String romoteAddr,String username,
            String password,String targetFolder) {

        List<Object> folderNameList = new ArrayList<Object>();
        try {
              Connection connection = new Connection(romoteAddr);// 创建一个连接实例
              connection.connect();// Now connect
              boolean isAuthenticated = connection.authenticateWithPassword(username, password);//認證
              Assert.isTrue(isAuthenticated, "用戶名或密碼錯誤!");
              ch.ethz.ssh2.Session sess = connection.openSession();// 創建一個會話
              sess.requestPTY("bash");
              sess.startShell();
              InputStream stdout = new StreamGobbler(sess.getStdout());
              InputStream stderr = new StreamGobbler(sess.getStderr());
              BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
              BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));
              //向服务器上输入命令
              PrintWriter out = new PrintWriter(sess.getStdin());
              out.println("cd " + targetFolder);//進入日志文件存放的目录
              out.println("ls -ld */");
              out.println("exit");
              out.close();
              sess.waitForCondition(ChannelCondition.CLOSED|ChannelCondition.EOF | ChannelCondition.EXIT_STATUS,30000);

              //本机查看远程操作的指令及状态
              System.out.println("输入指令后对应的显示信息:");
              while (true) {
                  String line = stdoutReader.readLine();
                  if (line == null)
                      break;
                  System.out.println(line);
                  //取出文件夹那条记录
                  if(line.contains("drwxr-xr-x")){
                      //取出正确的文件夹名
                      StringBuffer sb =
                              new StringBuffer(line.substring(line.lastIndexOf(" "),line.lastIndexOf("/")));
                      line = sb.toString().replace(" ", "");
                      folderNameList.add(line);
                  }
              }
              System.out.println("ExitCode: " + sess.getExitStatus());

              //关闭连接
              sess.close();
              connection.close();
              stderrReader.close();
              stdoutReader.close();
          } catch (IOException e) {
              e.printStackTrace(System.err);
              System.exit(2);
          }
        return folderNameList;
    }

}
01-12 15:45