<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      大家如果熟悉Linux系統(tǒng)話,對ssh,sftp,scp等命令非常熟悉。ssh是一個安全協(xié)議,用來在不同系統(tǒng)或者服務(wù)器之間進行安全連接。ssh 在連接和傳送的過程中會加密所有的數(shù)據(jù)。

      而今天我要介紹的一個jar包,是使用 JSCH。JSCH是一個純粹的用Java實現(xiàn)SSH功能的java  library.

      官方地址為:http://www.jcraft.com/jsch/
      GitHub 地址為:https://github.com/vngx/vngx-jsch

      maven配置如下:

       <!-- 加入sftp依賴包 -->  
          <dependency>  
              <groupId>com.jcraft</groupId>  
              <artifactId>jsch</artifactId>  
              <version>0.1.50</version>  
          </dependency>  
      View Code

      對于這樣的設(shè)計我們先設(shè)計一個FTP連接接口:

      package com.xuanyuan.tools;
      
      import java.io.InputStream;
      import java.io.OutputStream;
      import java.util.Vector;
      /**
       * FTP 接口
       * @author Punk Lin
       * @email lentr@sina.cn
       * @date 2016年12月23日
       *
       */
      public interface FTPService {
      
      
          /**
           * 登入
           * @param host 登入主機地址
           * @param userName 用戶名
           * @param password 密碼
           * @param port 端口
           * @throws Exception
           */
          public void login(String host, String userName, String password, int port) throws Exception;
      
          /**
           * 退出
           */
          public void logout();
      
          /**
           * 上傳文件
           * 
           * @param in
           *            輸入流
           * @param remoteFilePath
           *            遠程文件絕對路徑
           * @throws Exception
           */
          public void uploadFile(InputStream in, String remoteFilePath) throws Exception;
      
          /**
           * 文件下載到本地
           * 
           * @param sourceFilePath
           *            遠程文件絕對路徑
           * @param localFilePath
           *            本地目錄或絕對路徑
           * @throws Exception
           */
          public void downloadFile(String sourceFilePath, String localFilePath)
                  throws Exception;
      
          /**
           * 文件下載到輸出流
           * 
           * @param sourceFilePath
           *            遠程文件絕對路徑
           * @param out
           *            輸出流
           * @throws Exception
           */
          public void downloadFile(String sourceFilePath, OutputStream out) throws Exception;
      
          /**
           * 刪除文件
           * 
           * @param directory
           *            要刪除文件所在目錄
           * @param deleteFile
           *            要刪除的文件
           * 
           * @throws Exception
           */
          public void deleteFile(String directory, String fileName) throws Exception;
      
          /**
           * 列出目錄下的文件,包括目錄
           * 
           * @param directory
           *            要列出的目錄
           * 
           * @return list 文件名列表
           * 
           * @throws Exception
           */
          public Vector<?> listFiles(String directory) throws Exception;
      
      }
      View Code

      對于它的具體實現(xiàn),則如下:

      package com.xuanyuan.tools.impl;
      
      import java.io.File;
      import java.io.FileOutputStream;
      import java.io.InputStream;
      import java.io.OutputStream;
      import java.util.Properties;
      import java.util.Vector;
      
      import com.jcraft.jsch.ChannelSftp;
      import com.jcraft.jsch.JSch;
      import com.jcraft.jsch.JSchException;
      import com.jcraft.jsch.Session;
      import com.jcraft.jsch.SftpException;
      import com.xuanyuan.tools.FTPService;
      
      public class SFTPServiceImpl implements FTPService {
          /** sftp會話session */
          protected Session sshSession = null;
      
          /** sftp通道 */
          protected ChannelSftp sftp = null;
      
          @Override
          public void login(String host, String userName, String password, int port)
                  throws Exception {
              try {
                  JSch jsch = new JSch();
                  this.sshSession = jsch.getSession(userName, host, port);
                  this.sshSession.setPassword(password);
                  Properties sshConfig = new Properties();
                  sshConfig.put("StrictHostKeyChecking", "no");
                  this.sshSession.setConfig(sshConfig);
                  this.sshSession.connect(20000);
                  this.sftp = (ChannelSftp) sshSession.openChannel("sftp");
                  this.sftp.connect();
                  this.sftp.setFilenameEncoding("UTF-8");
              } catch (JSchException e) {
                  throw new RuntimeException("無法使用sftp登陸,請檢查用戶名密碼或端口");
              }
      
          }
      
          @Override
          public void logout() {
              if (this.sftp != null) {
                  this.sftp.disconnect();
                  this.sshSession.disconnect();
                  this.sftp = null;
                  this.sshSession = null;
              }
          }
      
          @Override
          public void uploadFile(InputStream in, String remoteFilePath)
                  throws Exception {
              try {
                  String filepath = remoteFilePath.substring(0,
                          remoteFilePath.lastIndexOf("/"));
                  String fileName = remoteFilePath.substring(
                          remoteFilePath.lastIndexOf("/") + 1,
                          remoteFilePath.length());
                  this.sftp.cd(filepath);
                  this.sftp.put(in, fileName, 0);
      
              } catch (SftpException e) {
                  throw new RuntimeException("上傳文件時發(fā)生錯誤!請檢查文件路徑是否正確");
              }
      
          }
      
          @Override
          public void downloadFile(String sourceFilePath, String localFilePath)
                  throws Exception {
              File file = new File(localFilePath);
              FileOutputStream out = null;
              try {
                  if (file.isDirectory()) {
                      this.sftp.get(sourceFilePath, localFilePath);
                  } else {
                      out = new FileOutputStream(file);
                      this.sftp.get(sourceFilePath, out);
                  }
              } catch (SftpException e) {
                  throw new RuntimeException("下載文件時發(fā)生錯誤!請檢查文件路徑是否正確");
              } finally {
                  if (out != null)
                      out.close();
              }
          }
      
          @Override
          public void downloadFile(String sourceFilePath, OutputStream out)
                  throws Exception {
              try {
                  this.sftp.get(sourceFilePath, out);
              } catch (SftpException e) {
                  throw new RuntimeException("下載文件時發(fā)生錯誤!請檢查文件路徑是否正確");
              }
      
          }
      
          @Override
          public void deleteFile(String directory, String fileName) throws Exception {
              this.sftp.cd(directory);
              this.sftp.rm(fileName);
      
          }
      
          @Override
          public Vector<?> listFiles(String directory) throws Exception {
              return this.sftp.ls(directory);
          }
      
      }
      View Code

      至此工具完成了。這樣我們也可以通過這個工具提供,網(wǎng)頁這樣的上傳道服務(wù)器。

       

      相信別人往往比相信自己更簡單,是恐于未知,還是懼于無力。

       

      posted on 2016-12-23 22:50  高山月小  閱讀(588)  評論(0)    收藏  舉報

      主站蜘蛛池模板: 精品无码人妻一区二区三区| 蜜芽久久人人超碰爱香蕉 | 国产蜜臀在线一区二区三区| 在线日韩日本国产亚洲| 日韩一本不卡一区二区三区| 国产午夜亚洲精品不卡网站| 亚洲午夜爱爱香蕉片| 国精品91人妻无码一区二区三区 | 99热成人精品热久久66| 亚洲av日韩av永久无码电影| 亚洲国产综合性亚洲综合性| 暖暖 免费 高清 日本 在线观看5 色老头亚洲成人免费影院 | 西西人体大胆444WWW| 欧美性色黄大片| 人妻在线无码一区二区三区| 日韩精品一卡二卡在线观看| 久爱无码精品免费视频在线观看| 免费无码高H视频在线观看| 久久中文字幕av第二页| 亚洲区1区3区4区中文字幕码| 亚洲人妻精品中文字幕| 欧美丰满熟妇xxxx性ppx人交| 色婷婷久久综合中文久久一本| 精品久久人人做爽综合| 伊人久久大香线蕉网av| 亚洲另类无码专区国内精品| 沙田区| 成人国产亚洲精品天堂av| 亚洲欧美日韩愉拍自拍美利坚| 无码高潮爽到爆的喷水视频app| 久久久久香蕉国产线看观看伊| 亚洲理论在线A中文字幕| 国产成人毛片无码视频软件| 国产无遮挡裸体免费久久| 日本亚洲色大成网站www久久| 四川丰满少妇无套内谢| 亚洲日本VA午夜在线电影| 日本欧美一区二区三区在线播放| 四虎影院176| 国产久免费热视频在线观看| 免费人成网站视频在线观看 |