溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java使用SFTP上傳文件到服務(wù)器的簡單使用

發(fā)布時(shí)間:2020-08-28 08:19:36 來源:腳本之家 閱讀:1010 作者:唐凱 欄目:編程語言

最近用到SFTP上傳文件查找了一些資料后自己做了一點(diǎn)總結(jié),方便以后的查詢。具體代碼如下所示:

 /**
  * 將文件上傳到服務(wù)器
  * 
  * @param filePath
  *   文件路徑
  * @param channelSftp
  *   channelSftp對(duì)象
  * @return
  */
 public static boolean uploadFile(String filePath, ChannelSftp channelSftp) {
  OutputStream outstream = null;
  InputStream instream = null;
  boolean successFlag = false;
  try {
   File isfile = new File(filePath);
   if (isfile.isFile()) {
    outstream = channelSftp.put(isfile.getName());
    File file = new File(filePath);
    if (file.exists()) {
     instream = new FileInputStream(file);
     byte b[] = new byte[1024];
     int n;
     while ((n = instream.read(b)) != -1) {
      outstream.write(b, 0, n);
     }
     outstream.flush();
    }
    successFlag = true;
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    if (instream != null) {
     instream.close();
    }
    if (outstream != null) {
     outstream.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  return successFlag;
 }
 private static Session initJschSession()
   throws JSchException {
  int ftpPort = 0;
  String ftpHost = "";
  String port = "00"; //sftp的端口號(hào)
  String ftpUserName = ""; //用戶名
  String ftpPassword = ""; //鏈接的密碼
  String privateKey = ""; //
  String passphrase = "";
  if (port != null && !port.equals("")) {
   ftpPort = Integer.valueOf(port);
  }
  JSch jsch = new JSch(); // 創(chuàng)建JSch對(duì)象
  if (StringUtils.isNotBlank(privateKey)
    && StringUtils.isNotBlank(passphrase)) {
   jsch.addIdentity(privateKey, passphrase);
  }
  if (StringUtils.isNotBlank(privateKey)
    && StringUtils.isBlank(passphrase)) {
   jsch.addIdentity(privateKey);
  }
  jsch.getSession(ftpUserName, ftpHost, ftpPort);
  Session session = jsch.getSession(ftpUserName, ftpHost, ftpPort); // 根據(jù)用戶名,主機(jī)ip,端口獲取一個(gè)Session對(duì)象
  if (StringUtils.isNotBlank(ftpPassword)) {
   session.setPassword(ftpPassword); // 設(shè)置密碼
  }
  return session;
 }
 /**
  * 獲取ChannelSftp鏈接
  * 
  * @param timeout
  *   超時(shí)時(shí)間
  * @return 返回ChannelSftp對(duì)象
  * @throws JSchException
  */
 public static ChannelSftp getChannelSftp(Session session, int timeout)
   throws JSchException {
  Channel channel = null;
  Properties config = new Properties();
  config.put("StrictHostKeyChecking", "no");
  session.setConfig(config); // 為Session對(duì)象設(shè)置properties
  session.setTimeout(timeout); // 設(shè)置timeout時(shí)間
  session.connect(); // 通過Session建立鏈接
  channel = session.openChannel("sftp"); // 打開SFTP通道
  channel.connect(); // 建立SFTP通道的連接
  return (ChannelSftp) channel; 
 }
 /**
  * 斷開sftp鏈接
  * 
  * @param session
  *   會(huì)話
  * @param channel
  *   通道
  */
 public static void closeConnection(Channel channel, Session session) {
  try {
   if (session != null) {
    session.disconnect(); //關(guān)閉session鏈接
   }
   if (channel != null) {
    channel.disconnect(); //斷開連接
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

這里的用戶名密碼都是自己設(shè)置,這里的方法進(jìn)行了簡單的封裝,方便使用。

以上所述是小編給大家介紹的Java使用SFTP上傳文件到服務(wù)器的簡單使用,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI