免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

Java使用SFTP上傳文件到服務器的簡單使用

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

創新互聯主營南宮網站建設的網絡公司,主營網站建設方案,重慶APP開發公司,南宮h5小程序制作搭建,南宮網站營銷推廣歡迎南宮等地區企業咨詢

 /**
  * 將文件上傳到服務器
  * 
  * @param filePath
  *   文件路徑
  * @param channelSftp
  *   channelSftp對象
  * @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的端口號
  String ftpUserName = ""; //用戶名
  String ftpPassword = ""; //鏈接的密碼
  String privateKey = ""; //
  String passphrase = "";
  if (port != null && !port.equals("")) {
   ftpPort = Integer.valueOf(port);
  }
  JSch jsch = new JSch(); // 創建JSch對象
  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); // 根據用戶名,主機ip,端口獲取一個Session對象
  if (StringUtils.isNotBlank(ftpPassword)) {
   session.setPassword(ftpPassword); // 設置密碼
  }
  return session;
 }
 /**
  * 獲取ChannelSftp鏈接
  * 
  * @param timeout
  *   超時時間
  * @return 返回ChannelSftp對象
  * @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對象設置properties
  session.setTimeout(timeout); // 設置timeout時間
  session.connect(); // 通過Session建立鏈接
  channel = session.openChannel("sftp"); // 打開SFTP通道
  channel.connect(); // 建立SFTP通道的連接
  return (ChannelSftp) channel; 
 }
 /**
  * 斷開sftp鏈接
  * 
  * @param session
  *   會話
  * @param channel
  *   通道
  */
 public static void closeConnection(Channel channel, Session session) {
  try {
   if (session != null) {
    session.disconnect(); //關閉session鏈接
   }
   if (channel != null) {
    channel.disconnect(); //斷開連接
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

這里的用戶名密碼都是自己設置,這里的方法進行了簡單的封裝,方便使用。

以上所述是小編給大家介紹的Java使用SFTP上傳文件到服務器的簡單使用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對創新互聯網站的支持!

分享題目:Java使用SFTP上傳文件到服務器的簡單使用
標題來源:http://newbst.com/article12/jhsodc.html

成都網站建設公司_創新互聯,為您提供ChatGPT網站建設品牌網站制作網頁設計公司網站導航企業網站制作

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都做網站