中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

通過(guò)JSch - Java實(shí)現(xiàn)的SFTP

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

 JSch是Java Secure Channel的縮寫(xiě)。JSch是一個(gè)SSH2的純Java實(shí)現(xiàn)。它允許你連接到一個(gè)SSH服務(wù)器,并且可以使用端口轉(zhuǎn)發(fā),X11轉(zhuǎn)發(fā),文件傳輸?shù)龋?dāng)然你也可以集成它的功能到你自己的應(yīng)用程序。

  本文只介紹如何使用JSch實(shí)現(xiàn)的SFTP功能。

  SFTP是Secure File Transfer Protocol的縮寫(xiě),安全文件傳送協(xié)議?梢詾閭鬏斘募峁┮环N安全的加密方法。SFTP 為 SSH的一部份,是一種傳輸文件到服務(wù)器的安全方式。SFTP是使用加密傳輸認(rèn)證信息和傳輸?shù)臄?shù)據(jù),所以,使用SFTP是非常安全的。但是,由于這種傳輸 方式使用了加密/解密技術(shù),所以傳輸效率比普通的FTP要低得多,如果您對(duì)網(wǎng)絡(luò)安全性要求更高時(shí),可以使用SFTP代替FTP。(來(lái)自百度的解釋?zhuān)?nbsp;

  要使用JSch,需要下載它的jar包,請(qǐng)從官網(wǎng)下載它:http://www.jcraft.com/jsch/

import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.Vector;
 
import org.apache.commons.httpclient.auth.AuthenticationException;
 
import com.ebao.jewel.gs.integration.pub.CommonLog.exception.ExceptionUtil;
import com.ebao.jewel.gs.integration.pub.CommonLog.utils.JewelIntLogUtils;
import com.ebao.jewel.gs.integration.pub.commons.InterfaceUtil;
import com.ebao.jewel.gs.integration.pub.connection.IConnection;
import com.ebao.jewel.gs.integration.pub.constants.InterfaceConstants;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
 
public class JewelSftpClient implements IConnection {
 
  private Session sshSession = null;
 
  private ChannelSftp channelSftp = null;
 
  private int connectTimes = 0;
 
  public boolean connect(String host, int port, String username, String pwd)
 
  throws Exception {
    if (sshSession == null || !sshSession.isConnected()) {
 
      JSch jsch = new JSch();
 
      try {
        sshSession = jsch.getSession(username, host, port);
      } catch (JSchException je) {
        // throw new Connection
        throw je;
      }
    }
    if (!sshSession.isConnected()) {
      InterfaceUtil.batchLog("[TEST] sshSession.connect()");
      sshSession.setPassword(pwd);
 
      Properties sshConfig = new Properties();
      sshConfig.put("StrictHostKeyChecking", "no");
      sshSession.setConfig(sshConfig);
      try {
        sshSession.connect();
      } catch (JSchException ex) {
 
        if (ex.getMessage().indexOf("Session.connect") != -1) {
          throw new UnknownHostException(ExceptionUtil.getExceptionMsg(ex));
        } else {
          throw new AuthenticationException(ExceptionUtil.getExceptionMsg(ex));
        }
      }
    }
    InterfaceUtil.batchLog("[TEST] sshSession has connected.");
    try {
      if (channelSftp == null || !channelSftp.isConnected()) {
        InterfaceUtil.batchLog("[TEST] channelSftp.connect()");
        channelSftp = (ChannelSftp) sshSession.openChannel("sftp");
        channelSftp.connect();
      }
 
      InterfaceUtil.batchLog("[TEST] channelSftp has been established.");
      return true;
    } catch (JSchException je) {
      if (connectTimes++ < InterfaceConstants.MAX_CONNECT_TIMES) {
        String seq = connectTimes == 1 ? "1st" : connectTimes == 2
            ? "2nd"
            : connectTimes == 3 ? "3rd" : connectTimes + "st";
        InterfaceUtil.batchLog("[TEST]" + seq
            + " connection failed. Disconnected "
            + (disconnect() == true ? "succeeded" : "failed"));
        long sleepTime = InterfaceUtil.getConnectSleepTime() * 1000L;
        Thread.sleep(sleepTime);
        InterfaceUtil.batchLog("[TEST]" + sleepTime + " ms passed. Try "
            + (int) (connectTimes + 1) + " connection.");
        if (connectTimes == 4) {
          InterfaceUtil
              .batchLog("[TEST] !!!!!!!!!!!connection goes wrong!!!!!!!!!!!!!!!!");
        }
        return connect(host, port, username, pwd);
      } else {
        InterfaceUtil.batchLog("[TEST] connect excceeded 10 times.");
        throw je;
      }
    }
  }
 
  public boolean disconnect() throws Exception {
    try {
      if (channelSftp != null && channelSftp.isConnected()) {
 
        channelSftp.disconnect();
        InterfaceUtil.batchLog("[TEST] channelSftp has cloesd.");
      }
      if (sshSession != null && sshSession.isConnected()) {
 
        sshSession.disconnect();
        InterfaceUtil.batchLog("[TEST] sshSession has cloesd.");
      }
      return true;
    } catch (Exception e) {
      return false;
 
    }
 
  }
 
  public void upload(String src, String dst) throws Exception {
    channelSftp.put(src, dst);
  }
 
  public void download(String src, String dst) throws Exception {
    channelSftp.get(src, dst);
 
  }
 
  @SuppressWarnings("unchecked")
  public Vector<Object> listFiles(String directory) throws Exception {
    try {
      channelSftp.cd(directory);
    } catch (Exception e) {
      throw new FileNotFoundException("No such file or directory.src:"
          + directory);
    }
    Vector<Object> files = channelSftp.lsnames(directory);
    return files;
 
  }
 
  public boolean moveFile(String src, String dst) throws Exception {
    try {
      channelSftp.rename(src, dst);
      return true;
    } catch (Exception e) {
      JewelIntLogUtils.batchLog("[TEST] Move the file from " + src + " to "
          + dst + " fail", e);
      throw e;
    }
  }
 
  public boolean isConnected() {
    return channelSftp != null && channelSftp.isConnected();
  }
 
  @Override
  public void download(String src, OutputStream out) throws Exception {
    channelSftp.get(src, out);
  }
}

標(biāo)簽: 安全 服務(wù)器 網(wǎng)絡(luò) 網(wǎng)絡(luò)安全 網(wǎng)絡(luò)安全性

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Java判斷圖片格式的代碼

下一篇:JFreeChart生成柱形圖代碼