Java中com.jcraft.jsch.ChannelSftp講解

來源:互聯網
上載者:User

標籤:system   tco   throws   重新命名   class   hex   star   ack   host   

http://blog.csdn.net/allen_zhao_2012/article/details/7941631

http://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html

http://xpenxpen.iteye.com/blog/2061869

http://blog.csdn.net/fyqcdbdx/article/details/23863793

http://blog.csdn.net/u013256816/article/details/52701563?utm_source=gold_browser_extension

SFTP是Secure File Transfer Protocol的縮寫,安全檔案傳送協議。可以為傳輸檔案提供一種安全的加密方法。SFTP 為 SSH的一部份,是一種傳輸檔案到伺服器的安全方式。SFTP是使用加密傳輸認證資訊和傳輸的資料,所以,使用SFTP是非常安全的。但是,由於這種傳輸方式使用了加密/解密技術,所以傳輸效率比普通的FTP要低得多,如果您對網路安全性要求更高時,可以使用SFTP代替FTP。

 

ChannelSftp類是JSch實現SFTP核心類,它包含了所有SFTP的方法,如:put():      檔案上傳get():      檔案下載cd():       進入指定目錄ls():       得到指定目錄下的檔案清單rename():   重新命名指定檔案或目錄rm():       刪除指定檔案mkdir():    建立目錄rmdir():    刪除目錄等等(這裡省略了方法的參數,put和get都有多個重載方法,具體請看原始碼,這裡不一一列出。)

 

一個簡單的jsch連結Linux並執行命令的utils。

 

  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.io.InputStreamReader;  
  5.   
  6. import com.jcraft.jsch.Channel;  
  7. import com.jcraft.jsch.ChannelExec;  
  8. import com.jcraft.jsch.JSch;  
  9. import com.jcraft.jsch.JSchException;  
  10. import com.jcraft.jsch.Session;  
  11.   
  12.   
  13.   
  14. public class ShellUtils {  
  15.     private static JSch jsch;  
  16.     private static Session session;  
  17.   
  18.       
  19.     /** 
  20.      * 串連到指定的IP 
  21.      *  
  22.      * @throws JSchException 
  23.      */  
  24.     public static void connect(String user, String passwd, String host) throws JSchException {  
  25.         jsch = new JSch();  
  26.         session = jsch.getSession(user, host, 22);  
  27.         session.setPassword(passwd);  
  28.           
  29.         java.util.Properties config = new java.util.Properties();  
  30.         config.put("StrictHostKeyChecking", "no");  
  31.         session.setConfig(config);  
  32.           
  33.         session.connect();  
  34.     }  
  35.   
  36.     /** 
  37.      * 執行相關的命令 
  38.      * @throws JSchException  
  39.      */  
  40.     public static void execCmd(String command, String user, String passwd, String host) throws JSchException {  
  41.         connect(user, passwd, host);  
  42.           
  43.         BufferedReader reader = null;  
  44.         Channel channel = null;  
  45.   
  46.         try {  
  47.             while (command != null) {  
  48.                 channel = session.openChannel("exec");  
  49.                 ((ChannelExec) channel).setCommand(command);  
  50.                   
  51.                 channel.setInputStream(null);  
  52.                 ((ChannelExec) channel).setErrStream(System.err);  
  53.   
  54.                 channel.connect();  
  55.                 InputStream in = channel.getInputStream();  
  56.                 reader = new BufferedReader(new InputStreamReader(in));  
  57.                 String buf = null;  
  58.                 while ((buf = reader.readLine()) != null) {  
  59.                     System.out.println(buf);  
  60.                 }  
  61.             }  
  62.         } catch (IOException e) {  
  63.             e.printStackTrace();  
  64.         } catch (JSchException e) {  
  65.             e.printStackTrace();  
  66.         } finally {  
  67.             try {  
  68.                 reader.close();  
  69.             } catch (IOException e) {  
  70.                 e.printStackTrace();  
  71.             }  
  72.             channel.disconnect();  
  73.             session.disconnect();  
  74.         }  
  75.     }  
  76.      
  77. }  

Java中com.jcraft.jsch.ChannelSftp講解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.