天天看點

關于Springboot中JSCH使用說明1. JSCH簡介2. JSCH依賴3. 使用方法4. 使用連接配接池

1. JSCH簡介

JSch 是SSH2的一個純Java實作。它允許你連接配接到一個sshd 伺服器,使用端口轉發,X11轉發,檔案傳輸等等。你可以将它的功能內建到你自己的 程式中。同時該項目也提供一個J2ME版本用來在手機上直連SSHD伺服器。

2. JSCH依賴

<dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.55</version>
        </dependency>
           

3. 使用方法

3.1 連接配接遠端主機

/**
     * 初始化
     *
     * @param ip       遠端主機IP位址
     * @param port     遠端主機端口
     * @param username 遠端主機登陸使用者名
     * @param password 遠端主機登陸密碼
     * @throws JSchException JSch異常
     */
    public void init(String ip, Integer port, String username, String password) throws JSchException {
        JSch jsch = new JSch();
        session = jsch.getSession(username, ip, port);
        session.setPassword(password);
        Properties sshConfig = new Properties();
        sshConfig.put("StrictHostKeyChecking", strictHostKeyChecking);
        session.setConfig(sshConfig);
        session.connect(timeout);
        log.info("Session connected!");
    }

    public void init(String ip, String username, String password) throws JSchException {
        init(ip,22,username,password);
    }
           

3.2 ChannelExec使用說明

/**
     * 連接配接多次執行指令,執行指令完畢後需要執行close()方法
     *
     * @param command 需要執行的指令
     * @return 執行結果
     * @throws Exception 沒有執行初始化
     */
    public String execCmd(String command) throws Exception {
        // 打開執行shell指令的通道
        channel = session.openChannel("exec");
        channelExec = (ChannelExec) channel;
        if (session == null || channel == null || channelExec == null) {
            log.error("請先執行init()");
            throw new Exception("請先執行init()");
        }
        log.info("execCmd command - > {}", command);
        channelExec.setCommand(command);
        channel.setInputStream(null);
        channelExec.setErrStream(System.err);
        channel.connect();
        StringBuilder sb = new StringBuilder(16);
        try (InputStream in = channelExec.getInputStream();
             InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
             BufferedReader reader = new BufferedReader(isr)) {
            String buffer;
            while ((buffer = reader.readLine()) != null) {
                sb.append("\n").append(buffer);
            }
            log.info("execCmd result - > {}", sb);
            return sb.toString();
        }

    }

    /**
     * 執行指令關閉連接配接
     * @param command 需要執行的指令
     * @return 執行結果
     * @throws Exception 沒有執行初始化
     */
    public String execCmdAndClose(String command) throws Exception {
        String result = execCmd(command);
        close();
        return result;

    }
    
     /**
     * 釋放資源
     */
    public void close() {
        if (channelExec != null && channelExec.isConnected()) {
            channelExec.disconnect();
        }
        if (channel != null && channel.isConnected()) {
            channel.disconnect();
        }
        if (session != null && session.isConnected()) {
            session.disconnect();
        }
    }
           

3.3 ChannelSftp使用說明

3.3.1 ChannelSftp簡介

ChannelSftp類是JSch實作

SFTP

核心類,它包含了所有

SFTP

的方法,如:

  • put(): 檔案上傳
  • get(): 檔案下載下傳
  • cd(): 進入指定目錄
  • ls(): 得到指定目錄下的檔案清單
  • rename(): 重命名指定檔案或目錄
  • rm(): 删除指定檔案
  • mkdir(): 建立目錄
  • rmdir(): 删除目錄

3.3.2 JSch支援三種檔案傳輸模式:

模式 描述
OVERWRITE

完全覆寫模式

,這是JSch的預設檔案傳輸模式,即如果目标檔案已經存在,傳輸的檔案将完全覆寫目标檔案,産生新的檔案。
RESUME

恢複模式

,如果檔案已經傳輸一部分,這時由于網絡或其他任何原因導緻檔案傳輸中斷,如果下一次傳輸相同的檔案,則會從上一次中斷的地方續傳。
APPEND

追加模式

,如果目标檔案已存在,傳輸的檔案将在目标檔案後追加。

3.3.3 檔案上傳

實作檔案上傳可以調用ChannelSftp對象的put方法。ChannelSftp中有12個put方法的重載方法:

方法 描述
public void put(String src, String dst) 将本地檔案名為src的檔案上傳到目标伺服器,目标檔案名為dst,若dst為目錄,則目标檔案名将與src檔案名相同。采用預設的傳輸模式:OVERWRITE
public void put(String src, String dst, int mode) 将本地檔案名為src的檔案上傳到目标伺服器,目标檔案名為dst,若dst為目錄,則目标檔案名将與src檔案名相同。指定檔案傳輸模式為mode(mode可選值為:ChannelSftp.OVERWRITE,ChannelSftp.RESUME,ChannelSftp.APPEND)
public void put(String src, String dst, SftpProgressMonitor monitor) 将本地檔案名為src的檔案上傳到目标伺服器,目标檔案名為dst,若dst為目錄,則目标檔案名将與src檔案名相同。采用預設的傳輸模式:OVERWRITE,并使用實作了SftpProgressMonitor接口的monitor對象來監控檔案傳輸的進度。
public void put(String src, String dst,SftpProgressMonitor monitor, int mode) 将本地檔案名為src的檔案上傳到目标伺服器,目标檔案名為dst,若dst為目錄,則目标檔案名将與src檔案名相同。指定傳輸模式為mode,并使用實作了SftpProgressMonitor接口的monitor對象來監控檔案傳輸的進度。
public void put(InputStream src, String dst) 将本地的input stream對象src上傳到目标伺服器,目标檔案名為dst,dst不能為目錄。采用預設的傳輸模式:OVERWRITE
public void put(InputStream src, String dst, int mode) 将本地的input stream對象src上傳到目标伺服器,目标檔案名為dst,dst不能為目錄。指定檔案傳輸模式為mode
public void put(InputStream src, String dst, SftpProgressMonitor monitor) 将本地的input stream對象src上傳到目标伺服器,目标檔案名為dst,dst不能為目錄。采用預設的傳輸模式:OVERWRITE,并使用實作了SftpProgressMonitor接口的monitor對象來監控傳輸的進度。
public void put(InputStream src, String dst,SftpProgressMonitor monitor, int mode) 将本地的input stream對象src上傳到目标伺服器,目标檔案名為dst,dst不能為目錄。指定檔案傳輸模式為mode,并使用實作了SftpProgressMonitor接口的monitor對象來監控傳輸的進度。
public OutputStream put(String dst) 該方法傳回一個輸出流,可以向該輸出流中寫入資料,最終将資料傳輸到目标伺服器,目标檔案名為dst,dst不能為目錄。采用預設的傳輸模式:OVERWRITE
public OutputStream put(String dst, final int mode) 該方法傳回一個輸出流,可以向該輸出流中寫入資料,最終将資料傳輸到目标伺服器,目标檔案名為dst,dst不能為目錄。指定檔案傳輸模式為mode
public OutputStream put(String dst, final SftpProgressMonitor monitor, final int mode) 該方法傳回一個輸出流,可以向該輸出流中寫入資料,最終将資料傳輸到目标伺服器,目标檔案名為dst,dst不能為目錄。指定檔案傳輸模式為mode,并使用實作了SftpProgressMonitor接口的monitor對象來監控傳輸的進度。
public OutputStream put(String dst, final SftpProgressMonitor monitor, final int mode, long offset) 該方法傳回一個輸出流,可以向該輸出流中寫入資料,最終将資料傳輸到目标伺服器,目标檔案名為dst,dst不能為目錄。指定檔案傳輸模式為mode,并使用實作了SftpProgressMonitor接口的monitor對象來監控傳輸的進度。offset指定了一個偏移量,從輸出流偏移offset開始寫入資料。
/**
     * SFTP檔案上傳
     *
     * @param src 源位址
     * @param dst 目的位址
     * @throws Exception 上傳檔案失敗
     */
    public void putAndClose(String src, String dst) throws Exception {
        putAndClose(src, dst, ChannelSftp.OVERWRITE);
    }

    /**
     * SFTP檔案上傳
     *
     * @param src  源位址
     * @param dst  目的位址
     * @param mode 上傳模式 預設為ChannelSftp.OVERWRITE
     * @throws Exception 上傳檔案失敗
     */
    public void putAndClose(String src, String dst, int mode) throws Exception {
        initChannelSftp();
        log.info("Upload File {} -> {}", src, dst);
        channelSftp.put(src, dst, mode);
        log.info("Upload File Success!");
        close();
    }

    /**
     * SFTP檔案上傳并監控上傳進度
     *
     * @param src 源位址
     * @param dst 目的位址
     * @throws Exception 上傳檔案失敗
     */
    public void putMonitorAndClose(String src, String dst) throws Exception {
        putMonitorAndClose(src, dst, ChannelSftp.OVERWRITE);
    }

    /**
     * SFTP檔案上傳并監控上傳進度
     *
     * @param src  源位址
     * @param dst  目的位址
     * @param mode 上傳模式 預設為ChannelSftp.OVERWRITE
     * @throws Exception 上傳檔案失敗
     */
    public void putMonitorAndClose(String src, String dst, int mode) throws Exception {
        initChannelSftp();
        UploadMonitor monitor = new UploadMonitor(new File(src).length());
        log.info("Upload File {} -> {}", src, dst);
        channelSftp.put(src, dst, monitor, mode);
        log.info("Upload File Success!");
        close();
    }

    /**
     * 釋放資源
     */
    public void close() {
        if (channelSftp != null && channelSftp.isConnected()) {
            channelSftp.disconnect();
        }
        if (channel != null && channel.isConnected()) {
            channel.disconnect();
        }
        if (session != null && session.isConnected()) {
            session.disconnect();
        }
    }

    private void initChannelSftp() throws Exception {
        channel = session.openChannel("sftp");
        channel.connect(); // 建立SFTP通道的連接配接
        channelSftp = (ChannelSftp) channel;
        if (session == null || channel == null || channelSftp == null) {
            log.error("請先執行init()");
            throw new Exception("請先執行init()");
        }
    }

}

           

3.3.4 檔案下載下傳

JSch檔案下載下傳是通過調用ChannelSftp對象的get方法來實作的。ChannelSftp中有9個get方法的重載方法:

方法 描述
publicvoid get(String src, String dst) 将目标伺服器上檔案名為src的檔案下載下傳到本地,本地檔案名為dst。若dst為目錄,則下載下傳到本地的檔案名将與src檔案名相同。(注:src必須是檔案,不能為目錄),采用預設的傳輸模式:OVERWRITE
publicvoid get(String src, String dst, SftpProgressMonitor monitor) 将目标伺服器上檔案名為src的檔案下載下傳到本地,本地檔案名為dst。若dst為目錄,則下載下傳到本地的檔案名将與src檔案名相同。(注:src必須是檔案,不能為目錄),采用預設的傳輸模式:OVERWRITE
publicvoid get(String src, String dst,SftpProgressMonitor monitor, int mode) 将目标伺服器上檔案名為src的檔案下載下傳到本地,本地檔案名為dst。若dst為目錄,則下載下傳到本地的檔案名将與src檔案名相同。(注:src必須是檔案,不能為目錄)指定檔案傳輸模式為mode(mode可選值為:ChannelSftp.OVERWRITE,ChannelSftp.RESUME,ChannelSftp.APPEND),并使用實作了SftpProgressMonitor接口的monitor對象來監控檔案的傳輸進度。
publicvoid get(String src, OutputStream dst) 将目标伺服器上檔案名為src的檔案下載下傳到本地,下載下傳的資料寫入到輸出流對象dst(如:檔案輸出流)。采用預設的傳輸模式:OVERWRITE
publicvoid get(String src, OutputStream dst, SftpProgressMonitor monitor) 将目标伺服器上檔案名為src的檔案下載下傳到本地,下載下傳的資料寫入到輸出流對象dst(如:檔案輸出流)。采用預設的傳輸模式:OVERWRITE,并使用實作了SftpProgressMonitor接口的monitor對象來監控檔案的傳輸進度。
publicvoid get(String src, OutputStream dst, SftpProgressMonitor monitor, int mode, long skip) 将目标伺服器上檔案名為src的檔案下載下傳到本地,下載下傳的資料寫入到輸出流對象dst(如:檔案輸出流)。指定檔案傳輸模式為mode并使用實作了SftpProgressMonitor接口的monitor對象來監控檔案的傳輸進度。skip指定了一個跳讀量,即下載下傳時從src檔案跳過skip位元組的資料。(一般不推薦使用該參數,預設設為0)
public InputStream get(String src) 該方法傳回一個輸入流,該輸入流含有目标伺服器上檔案名為src的檔案資料。可以從該輸入流中讀取資料,最終将資料傳輸到本地(如:讀取資料後将資料寫入到本地的檔案中)(注:該方法不支援多種檔案傳輸模式,如何讀取與儲存資料由應用程式自己确定)
public InputStream get(String src, SftpProgressMonitor monitor) 該方法傳回一個輸入流,該輸入流含有目标伺服器上檔案名為src的檔案資料。可以從該輸入流中讀取資料,最終将資料傳輸到本地(如:讀取資料後将資料寫入到本地的檔案中)并使用實作了SftpProgressMonitor接口的monitor對象來監控檔案的傳輸進度。(注:該方法不支援多種檔案傳輸模式,如何讀取與儲存資料由應用程式自己确定)
public InputStream get(String src, final SftpProgressMonitor monitor, finallong skip) 該方法傳回一個輸入流,該輸入流含有目标伺服器上檔案名為src的檔案資料。可以從該輸入流中讀取資料,最終将資料傳輸到本地(如:讀取資料後将資料寫入到本地的檔案中)并使用實作了SftpProgressMonitor接口的monitor對象來監控檔案的傳輸進度。(注:該方法不支援多種檔案傳輸模式,如何讀取與儲存資料由應用程式自己确定)skip指定了一個跳讀量,即下載下傳時從src檔案跳過skip位元組的資料。(一般不推薦使用該參數,預設設為0)
/**
     * SFTP檔案下載下傳
     *
     * @param src 源檔案位址
     * @param dst 目的位址
     * @throws Exception 下載下傳檔案失敗
     */
    public void getAndClose(String src, String dst) throws Exception {
        initChannelSftp();
        log.info("Download File {} -> {}", src, dst);
        channelSftp.get(src, dst);
        log.info("Download File Success!");
        close();
    }

    public void getMonitorAndClose(String src, String dst) throws Exception {
        initChannelSftp();
        FileProgressMonitor monitor = new FileProgressMonitor(new File(src).length());
        log.info("Download File {} -> {}", src, dst);
        channelSftp.get(src, dst, monitor);
        log.info("Download File Success!");
        close();
    }
           

3.4 ChannelShell使用說明

3.4.1 shell代碼

/**
     * 執行複雜shell指令
     * @param cmds 多條指令
     * @return 執行結果
     * @throws Exception 連接配接異常
     */
    public String execCmdByShell(String... cmds)throws Exception{
        return execCmdByShell(Arrays.asList(cmds));
    }
 /**
     * 執行複雜shell指令
     * @param cmds 多條指令
     * @return 執行結果
     * @throws Exception 連接配接異常
     */
    public String execCmdByShell(List<String> cmds) throws Exception {
        String result = "";
        initChannelShell();
        InputStream inputStream = channelShell.getInputStream();
        channelShell.setPty(true);
        channelShell.connect();

        OutputStream outputStream = channelShell.getOutputStream();
        PrintWriter printWriter = new PrintWriter(outputStream);
        for (String cmd : cmds) {
            printWriter.println(cmd);
        }
        printWriter.flush();

        byte[] tmp = new byte[1024];
        while (true) {

            while (inputStream.available() > 0) {
                int i = inputStream.read(tmp, 0, 1024);
                if (i < 0) {
                    break;
                }
                String s = new String(tmp, 0, i);
                if (s.contains("--More--")) {
                    outputStream.write((" ").getBytes());
                    outputStream.flush();
                }
                System.out.println(s);
            }
            if (channelShell.isClosed()) {
                System.out.println("exit-status:" + channelShell.getExitStatus());
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        outputStream.close();
        inputStream.close();
        return result;
    }

private void initChannelShell() throws Exception {
        // 打開執行shell指令的通道
        channel = session.openChannel("shell");
        channelShell = (ChannelShell) channel;
        if (session == null || channel == null || channelShell == null) {
            log.error("請先執行init()");
            throw new Exception("請先執行init()");
        }
    }
           

3.5 完整工具類代碼

ShellUtil.java

@Slf4j
@Component
@Slf4j
@Component
@Scope(value = "prototype")
public class ShellUtil {


    @Value("${ssh.strictHostKeyChecking:no}")
    private String strictHostKeyChecking;

    @Value("${ssh.timeout:30000}")
    private Integer timeout;

    private Session session;

    private Channel channel;

    private ChannelExec channelExec;

    private ChannelSftp channelSftp;

    private ChannelShell channelShell;

    /**
     * 初始化
     *
     * @param ip       遠端主機IP位址
     * @param port     遠端主機端口
     * @param username 遠端主機登陸使用者名
     * @param password 遠端主機登陸密碼
     * @throws JSchException JSch異常
     */
    public void init(String ip, Integer port, String username, String password) throws JSchException {
        JSch jsch = new JSch();
        session = jsch.getSession(username, ip, port);
        session.setPassword(password);
        Properties sshConfig = new Properties();
        sshConfig.put("StrictHostKeyChecking", strictHostKeyChecking);
        session.setConfig(sshConfig);
        session.connect(timeout);
        log.info("Session connected!");
    }

    public void init(String ip, String username, String password) throws JSchException {
        init(ip, 22, username, password);
    }

    /**
     * 連接配接多次執行指令,執行指令完畢後需要執行close()方法
     *
     * @param command 需要執行的指令
     * @return 執行結果
     * @throws Exception 沒有執行初始化
     */
    public String execCmd(String command) throws Exception {
        initChannelExec();
        log.info("execCmd command - > {}", command);
        channelExec.setCommand(command);
        channel.setInputStream(null);
        channelExec.setErrStream(System.err);
        channel.connect();
        StringBuilder sb = new StringBuilder(16);
        try (InputStream in = channelExec.getInputStream();
             InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
             BufferedReader reader = new BufferedReader(isr)) {
            String buffer;
            while ((buffer = reader.readLine()) != null) {
                sb.append("\n").append(buffer);
            }
            log.info("execCmd result - > {}", sb);
            return sb.toString();
        }

    }


    /**
     * 執行指令關閉連接配接
     *
     * @param command 需要執行的指令
     * @return 執行結果
     * @throws Exception 沒有執行初始化
     */
    public String execCmdAndClose(String command) throws Exception {
        String result = execCmd(command);
        close();
        return result;
    }

    /**
     * 執行複雜shell指令
     *
     * @param cmds 多條指令
     * @return 執行結果
     * @throws Exception 連接配接異常
     */
    public String execCmdByShell(String... cmds) throws Exception {
        return execCmdByShell(Arrays.asList(cmds));
    }

    /**
     * 執行複雜shell指令
     *
     * @param cmds 多條指令
     * @return 執行結果
     * @throws Exception 連接配接異常
     */
    public String execCmdByShell(List<String> cmds) throws Exception {
        String result = "";
        initChannelShell();
        InputStream inputStream = channelShell.getInputStream();
        channelShell.setPty(true);
        channelShell.connect();

        OutputStream outputStream = channelShell.getOutputStream();
        PrintWriter printWriter = new PrintWriter(outputStream);
        for (String cmd : cmds) {
            printWriter.println(cmd);
        }
        printWriter.flush();

        byte[] tmp = new byte[1024];
        while (true) {

            while (inputStream.available() > 0) {
                int i = inputStream.read(tmp, 0, 1024);
                if (i < 0) {
                    break;
                }
                String s = new String(tmp, 0, i);
                if (s.contains("--More--")) {
                    outputStream.write((" ").getBytes());
                    outputStream.flush();
                }
                System.out.println(s);
            }
            if (channelShell.isClosed()) {
                System.out.println("exit-status:" + channelShell.getExitStatus());
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        outputStream.close();
        inputStream.close();
        return result;
    }


    /**
     * SFTP檔案上傳
     *
     * @param src 源位址
     * @param dst 目的位址
     * @throws Exception 上傳檔案失敗
     */
    public void putAndClose(String src, String dst) throws Exception {
        putAndClose(src, dst, ChannelSftp.OVERWRITE);
    }

    /**
     * SFTP檔案上傳
     *
     * @param src  源位址
     * @param dst  目的位址
     * @param mode 上傳模式 預設為ChannelSftp.OVERWRITE
     * @throws Exception 上傳檔案失敗
     */
    public void putAndClose(String src, String dst, int mode) throws Exception {
        put(src, dst, mode);
        close();
    }

    public void put(String src, String dst) throws Exception {
        put(src, dst, ChannelSftp.OVERWRITE);
    }

    public void put(String src, String dst, int mode) throws Exception {
        initChannelSftp();
        log.info("Upload File {} -> {}", src, dst);
        channelSftp.put(src, dst, mode);
        log.info("Upload File Success!");
    }

    /**
     * SFTP檔案上傳并監控上傳進度
     *
     * @param src 源位址
     * @param dst 目的位址
     * @throws Exception 上傳檔案失敗
     */
    public void putMonitorAndClose(String src, String dst) throws Exception {
        putMonitorAndClose(src, dst, ChannelSftp.OVERWRITE);
    }

    /**
     * SFTP檔案上傳并監控上傳進度
     *
     * @param src  源位址
     * @param dst  目的位址
     * @param mode 上傳模式 預設為ChannelSftp.OVERWRITE
     * @throws Exception 上傳檔案失敗
     */
    public void putMonitorAndClose(String src, String dst, int mode) throws Exception {
        initChannelSftp();
        FileProgressMonitor monitor = new FileProgressMonitor(new File(src).length());
        log.info("Upload File {} -> {}", src, dst);
        channelSftp.put(src, dst, monitor, mode);
        log.info("Upload File Success!");
        close();
    }

    /**
     * SFTP檔案下載下傳
     *
     * @param src 源檔案位址
     * @param dst 目的位址
     * @throws Exception 下載下傳檔案失敗
     */
    public void getAndClose(String src, String dst) throws Exception {
        get(src,dst);
        close();
    }

    public void get(String src, String dst) throws Exception {
        initChannelSftp();
        log.info("Download File {} -> {}", src, dst);
        channelSftp.get(src, dst);
        log.info("Download File Success!");
    }

    /**
     * SFTP檔案下載下傳并監控下載下傳進度
     *
     * @param src 源檔案位址
     * @param dst 目的位址
     * @throws Exception 下載下傳檔案失敗
     */
    public void getMonitorAndClose(String src, String dst) throws Exception {
        initChannelSftp();
        FileProgressMonitor monitor = new FileProgressMonitor(new File(src).length());
        log.info("Download File {} -> {}", src, dst);
        channelSftp.get(src, dst, monitor);
        log.info("Download File Success!");
        close();
    }

    /**
     * 删除指定目錄檔案
     *
     * @param path 删除路徑
     * @throws Exception 遠端主機連接配接異常
     */
    public void deleteFile(String path) throws Exception {
        initChannelSftp();
        channelSftp.rm(path);
        log.info("Delete File {}", path);
    }

    /**
     * 删除指定目錄
     *
     * @param path 删除路徑
     * @throws Exception 遠端主機連接配接異常
     */
    public void deleteDir(String path) throws Exception {
        initChannelSftp();
        channelSftp.rmdir(path);
        log.info("Delete Dir {} ", path);
    }

    /**
     * 釋放資源
     */
    public void close() {
        if (channelSftp != null && channelSftp.isConnected()) {
            channelSftp.disconnect();
        }
        if (channelExec != null && channelExec.isConnected()) {
            channelExec.disconnect();
        }
        if (channel != null && channel.isConnected()) {
            channel.disconnect();
        }
        if (session != null && session.isConnected()) {
            session.disconnect();
        }
    }

    private void initChannelSftp() throws Exception {
        channel = session.openChannel("sftp");
        channel.connect(); // 建立SFTP通道的連接配接
        channelSftp = (ChannelSftp) channel;
        if (session == null || channel == null || channelSftp == null) {
            log.error("請先執行init()");
            throw new Exception("請先執行init()");
        }
    }

    private void initChannelExec() throws Exception {
        // 打開執行shell指令的通道
        channel = session.openChannel("exec");
        channelExec = (ChannelExec) channel;
        if (session == null || channel == null || channelExec == null) {
            log.error("請先執行init()");
            throw new Exception("請先執行init()");
        }
    }

    private void initChannelShell() throws Exception {
        // 打開執行shell指令的通道
        channel = session.openChannel("shell");
        channelShell = (ChannelShell) channel;
        if (session == null || channel == null || channelShell == null) {
            log.error("請先執行init()");
            throw new Exception("請先執行init()");
        }
    }


}



           

FileProgressMonitor.java

@Slf4j
public class FileProgressMonitor extends TimerTask implements SftpProgressMonitor {

    private boolean isEnd = false;

    private long transfered;

    private long fileSize;

    private ScheduledExecutorService executorService;

    private boolean isScheduled = false;

    long startTime = 0L;

    public FileProgressMonitor(long fileSize) {
        this.fileSize = fileSize;
    }

    @Override
    public void run() {
        if (!isEnd()) {
            log.info("Transfering is in progress.");
            long transfered = getTransfered();
            // 判斷目前已傳輸資料大小是否等于檔案總大小
            if (transfered != fileSize) {
                log.info("Current transfered: {} bytes", transfered);
                sendProgressMessage(transfered);
            } else {
                // 如果目前已傳輸資料大小等于檔案總大小,說明已完成,設定end
                log.info("File transfering is done.");
                setEnd(true);
            }
        } else {
            log.info("Transfering done. Cancel timer.");
            // 如果傳輸結束,停止timer記時器
            stop();
            return;
        }
    }

    /**
     * 實作了SftpProgressMonitor接口的count方法
     */
    @Override
    public boolean count(long count) {
        if (isEnd()) {
            return false;
        }
        if (!isScheduled) {
            start();
        }
        add(count);
        return true;
    }

    /**
     * 實作了SftpProgressMonitor接口的end方法
     */
    @Override
    public void end() {
        setEnd(true);
        log.info("transfering end. time ->{} s", (System.currentTimeMillis() - startTime) / 1000);
    }

    @Override
    public void init(int op, String src, String dest, long max) {
        startTime = System.currentTimeMillis();
    }

    public void stop() {
        log.info("Try to stop progress monitor.");
        boolean isShutdown = executorService.isShutdown();
        if (!isShutdown) {
            executorService.shutdown();
        }
        log.info("Progress monitor stoped.");
    }

    public void start() {
        log.info("Try to start progress monitor.");
        executorService = new ScheduledThreadPoolExecutor(1);
        //1秒鐘後開始執行,每2杪鐘執行一次
        executorService.scheduleWithFixedDelay(this, 1, 1, TimeUnit.SECONDS);
        isScheduled = true;
        log.info("Progress monitor started.");
    }

    /**
     * 列印progress資訊
     *
     * @param transfered
     */
    private void sendProgressMessage(long transfered) {
        if (fileSize != 0) {
            double d = ((double) transfered * 100) / (double) fileSize;
            DecimalFormat df = new DecimalFormat("#.##");
            log.info("Sending progress message: {} %", df.format(d));
        } else {
            log.info("Sending progress message: {}", transfered);
        }
    }

    private synchronized void add(long count) {
        transfered = transfered + count;
    }

    private synchronized long getTransfered() {
        return transfered;
    }

    public synchronized void setTransfered(long transfered) {
        this.transfered = transfered;
    }

    private synchronized void setEnd(boolean isEnd) {
        this.isEnd = isEnd;
    }

    private synchronized boolean isEnd() {
        return isEnd;
    }
}

           

4. 使用連接配接池

jsch連接配接池

繼續閱讀