【SFTP】使用Jsch實現Sftp文件上傳-支持斷點續傳和進程監控


JSch是Java Secure Channel的縮寫。JSch是一個SSH2的純Java實現。它允許你連接到一個SSH服務器,並且可以使用端口轉發,X11轉發,文件傳輸等,當然你也可以集成它的功能到你自己的應用程序。
  本文只介紹如何使用JSch實現的SFTP功能。
  SFTP是Secure File Transfer Protocol的縮寫,安全文件傳送協議。可以為傳輸文件提供一種安全的加密方法。SFTP 為 SSH的一部份,是一種傳輸文件到服務器的安全方式。SFTP是使用加密傳輸認證信息和傳輸的數據,所以,使用SFTP是非常安全的。但是,由於這種傳輸方式使用了加密/解密技術,所以傳輸效率比普通的FTP要低得多,如果您對網絡安全性要求更高時,可以使用SFTP代替FTP。(來自百度的解釋) 
  要使用JSch,需要下載它的jar包,請從官網下載它: http://www.jcraft.com/jsch/ 
 
ChannelSftp類是JSch實現SFTP核心類,它包含了所有SFTP的方法,如:
put():      文件上傳
get():      文件下載
cd():       進入指定目錄
ls():       得到指定目錄下的文件列表
rename():   重命名指定文件或目錄
rm():       刪除指定文件
mkdir():    創建目錄
rmdir():    刪除目錄
等等(這里省略了方法的參數,put和get都有多個重載方法,具體請看源代碼,這里不一一列出。)


步驟:
  1. 根據Jsch創建Session;
  2. 設置Session密碼、超時時間和屬性等;
  3. 連接session;
  4. 使用Session創建ChannelSftp通道;
  5. 接下來就可以使用ChannelSftp進行各種操作了:如文件上傳、文件下載;
  6. 最后,關系各種資源:如Session、ChannelSftp等;
其他:還可以設置監聽器,監控文件上傳和下載的進度;


創建ChannelSftp對象

編寫一個工具類,根據ip,用戶名及密碼得到一個SFTP channel對象,即ChannelSftp的實例對象,在應用程序中就可以使用該對象來調用SFTP的各種操作方法。



監控傳輸進度


文件上傳




測試斷點續傳







完整程序

   
   
   
           
  1. package com.sssppp.Communication;
  2. /**
  3. * This program will demonstrate the sftp protocol support.
  4. * $ CLASSPATH=.:../build javac Sftp.java
  5. * $ CLASSPATH=.:../build java Sftp
  6. * You will be asked username, host and passwd.
  7. * If everything works fine, you will get a prompt 'sftp>'.
  8. * 'help' command will show available command.
  9. * In current implementation, the destination path for 'get' and 'put'
  10. * commands must be a file, not a directory.
  11. *
  12. */
  13. import java.text.DecimalFormat;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. import java.util.Properties;
  17. import java.util.Timer;
  18. import java.util.TimerTask;
  19. import javax.swing.ProgressMonitor;
  20. import com.jcraft.jsch.Channel;
  21. import com.jcraft.jsch.ChannelSftp;
  22. import com.jcraft.jsch.JSch;
  23. import com.jcraft.jsch.JSchException;
  24. import com.jcraft.jsch.Session;
  25. import com.jcraft.jsch.SftpProgressMonitor;
  26. /**
  27. * <pre>
  28. * ----------命令集合---------------------
  29. * 可參考鏈接(官方示例程序):http://www.jcraft.com/jsch/examples/Sftp.java
  30. * ChannelSftp c = (ChannelSftp) channel;
  31. * c.quit();
  32. * c.exit();
  33. * c.cd("/home/example");
  34. * c.lcd("/home/example");
  35. * c.rm("/home/example.gz");
  36. * c.rmdir("/home/example");
  37. * c.mkdir("/home/example");
  38. * c.chgrp(777, "/home/example");
  39. * c.chown(777, "/home/example");
  40. * c.chmod(777, "/home/example");
  41. * c.pwd();
  42. * c.lpwd();
  43. * c.ls("/home/example");
  44. *
  45. * SftpProgressMonitor monitor = new MyProgressMonitor(); //顯示進度
  46. * //文件下載
  47. * c.get("srcPath", "dstPath", monitor, ChannelSftp.OVERWRITE);
  48. * c.get("srcPath", "dstPath", monitor, ChannelSftp.RESUME); //斷點續傳
  49. * c.get("srcPath", "dstPath", monitor, ChannelSftp.APPEND);
  50. * //文件上傳
  51. * c.put("srcPath", "dstPath", monitor, ChannelSftp.APPEND);
  52. * c.put("srcPath", "dstPath", monitor, ChannelSftp.APPEND);
  53. * c.put("srcPath", "dstPath", monitor, ChannelSftp.APPEND);
  54. *
  55. * c.hardlink("oldPath", "newPath");
  56. * c.rename("oldPath", "newPath");
  57. * c.symlink("oldPath", "newPath");
  58. * c.readlink("Path");
  59. * c.realpath("Path");
  60. * c.version();
  61. *
  62. * SftpStatVFS stat = c.statVFS("path"); //df 命令
  63. * long size = stat.getSize();
  64. * long used = stat.getUsed();
  65. * long avail = stat.getAvailForNonRoot();
  66. * long root_avail = stat.getAvail();
  67. * long capacity = stat.getCapacity();
  68. *
  69. * c.stat("path");
  70. * c.lstat("path");
  71. * ----------------------------------------------------------------------
  72. * </pre>
  73. *
  74. */
  75. public class SftpUtil {
  76. Session session = null;
  77. Channel channel = null;
  78. public static final String SFTP_REQ_HOST = "host";
  79. public static final String SFTP_REQ_PORT = "port";
  80. public static final String SFTP_REQ_USERNAME = "username";
  81. public static final String SFTP_REQ_PASSWORD = "password";
  82. public static final int SFTP_DEFAULT_PORT = 22;
  83. public static final String SFTP_REQ_LOC = "location";
  84. /**
  85. * 測試程序
  86. * @param arg
  87. * @throws Exception
  88. */
  89. public static void main(String[] arg) throws Exception {
  90. // 設置主機ip,端口,用戶名,密碼
  91. Map<String, String> sftpDetails = new HashMap<String, String>();
  92. sftpDetails.put(SFTP_REQ_HOST, "10.180.137.221");
  93. sftpDetails.put(SFTP_REQ_USERNAME, "root");
  94. sftpDetails.put(SFTP_REQ_PASSWORD, "xxx");
  95. sftpDetails.put(SFTP_REQ_PORT, "22");
  96. //測試文件上傳
  97. String src = "C:\\xxx\\TMP\\site-1.10.4.zip"; // 本地文件名
  98. String dst = "/tmp/sftp/"; // 目標文件名
  99. uploadFile(src, dst, sftpDetails);
  100. }
  101. public static void uploadFile(String src, String dst,
  102. Map<String, String> sftpDetails) throws Exception {
  103. SftpUtil sftpUtil = new SftpUtil();
  104. ChannelSftp chSftp = sftpUtil.getChannel(sftpDetails, 60000);
  105. /**
  106. * 代碼段1/代碼段2/代碼段3分別演示了如何使用JSch的不同的put方法來進行文件上傳。這三段代碼實現的功能是一樣的,
  107. * 都是將本地的文件src上傳到了服務器的dst文件
  108. */
  109. /**代碼段1
  110. OutputStream out = chSftp.put(dst,new MyProgressMonitor2(), ChannelSftp.OVERWRITE); // 使用OVERWRITE模式
  111. byte[] buff = new byte[1024 * 256]; // 設定每次傳輸的數據塊大小為256KB
  112. int read;
  113. if (out != null) {
  114. InputStream is = new FileInputStream(src);
  115. do {
  116. read = is.read(buff, 0, buff.length);
  117. if (read > 0) {
  118. out.write(buff, 0, read);
  119. }
  120. out.flush();
  121. } while (read >= 0);
  122. }
  123. **/
  124. // 使用這個方法時,dst可以是目錄,當dst是目錄時,上傳后的目標文件名將與src文件名相同
  125. // ChannelSftp.RESUME:斷點續傳
  126. chSftp.put(src, dst, new MyProgressMonitor(), ChannelSftp.RESUME); // 代碼段2
  127. // 將本地文件名為src的文件輸入流上傳到目標服務器,目標文件名為dst
  128. // chSftp.put(new FileInputStream(src), dst,new MyProgressMonitor2(), ChannelSftp.OVERWRITE); // 代碼段3
  129. chSftp.quit();
  130. sftpUtil.closeChannel();
  131. }
  132. /**
  133. * 根據ip,用戶名及密碼得到一個SFTP
  134. * channel對象,即ChannelSftp的實例對象,在應用程序中就可以使用該對象來調用SFTP的各種操作方法
  135. *
  136. * @param sftpDetails
  137. * @param timeout
  138. * @return
  139. * @throws JSchException
  140. */
  141. public ChannelSftp getChannel(Map<String, String> sftpDetails, int timeout)
  142. throws JSchException {
  143. String ftpHost = sftpDetails.get(SFTP_REQ_HOST);
  144. String port = sftpDetails.get(SFTP_REQ_PORT);
  145. String ftpUserName = sftpDetails.get(SFTP_REQ_USERNAME);
  146. String ftpPassword = sftpDetails.get(SFTP_REQ_PASSWORD);
  147. int ftpPort = SFTP_DEFAULT_PORT;
  148. if (port != null && !port.equals("")) {
  149. ftpPort = Integer.valueOf(port);
  150. }
  151. JSch jsch = new JSch(); // 創建JSch對象
  152. session = jsch.getSession(ftpUserName, ftpHost, ftpPort); // 根據用戶名,主機ip,端口獲取一個Session對象
  153. if (ftpPassword != null) {
  154. session.setPassword(ftpPassword); // 設置密碼
  155. }
  156. Properties config = new Properties();
  157. config.put("StrictHostKeyChecking", "no");
  158. session.setConfig(config); // Session對象設置properties
  159. session.setTimeout(timeout); // 設置timeout時間
  160. session.connect(5000); // 通過Session建立鏈接
  161. channel = session.openChannel("sftp"); // 打開SFTP通道
  162. channel.connect(); // 建立SFTP通道的連接
  163. return (ChannelSftp) channel;
  164. }
  165. public void closeChannel() throws Exception {
  166. if (channel != null) {
  167. channel.disconnect();
  168. }
  169. if (session != null) {
  170. session.disconnect();
  171. }
  172. }
  173. /**
  174. * 進度監控器-JSch每次傳輸一個數據塊,就會調用count方法來實現主動進度通知
  175. *
  176. */
  177. public static class MyProgressMonitor implements SftpProgressMonitor {
  178. private long count = 0; //當前接收的總字節數
  179. private long max = 0; //最終文件大小
  180. private long percent = -1; //進度
  181. /**
  182. * 當每次傳輸了一個數據塊后,調用count方法,count方法的參數為這一次傳輸的數據塊大小
  183. */
  184. @Override
  185. public boolean count(long count) {
  186. this.count += count;
  187. if (percent >= this.count * 100 / max) {
  188. return true;
  189. }
  190. percent = this.count * 100 / max;
  191. System.out.println("Completed " + this.count + "(" + percent
  192. + "%) out of " + max + ".");
  193. return true;
  194. }
  195. /**
  196. * 當傳輸結束時,調用end方法
  197. */
  198. @Override
  199. public void end() {
  200. System.out.println("Transferring done.");
  201. }
  202. /**
  203. * 當文件開始傳輸時,調用init方法
  204. */
  205. @Override
  206. public void init(int op, String src, String dest, long max) {
  207. System.out.println("Transferring begin.");
  208. this.max = max;
  209. this.count = 0;
  210. this.percent = -1;
  211. }
  212. }
  213. /**
  214. * 官方提供的進度監控器
  215. *
  216. */
  217. public static class DemoProgressMonitor implements SftpProgressMonitor {
  218. ProgressMonitor monitor;
  219. long count = 0;
  220. long max = 0;
  221. /**
  222. * 當文件開始傳輸時,調用init方法。
  223. */
  224. public void init(int op, String src, String dest, long max) {
  225. this.max = max;
  226. monitor = new ProgressMonitor(null,
  227. ((op == SftpProgressMonitor.PUT) ? "put" : "get") + ": "
  228. + src, "", 0, (int) max);
  229. count = 0;
  230. percent = -1;
  231. monitor.setProgress((int) this.count);
  232. monitor.setMillisToDecideToPopup(1000);
  233. }
  234. private long percent = -1;
  235. /**
  236. * 當每次傳輸了一個數據塊后,調用count方法,count方法的參數為這一次傳輸的數據塊大小。
  237. */
  238. public boolean count(long count) {
  239. this.count += count;
  240. if (percent >= this.count * 100 / max) {
  241. return true;
  242. }
  243. percent = this.count * 100 / max;
  244. monitor.setNote("Completed " + this.count + "(" + percent
  245. + "%) out of " + max + ".");
  246. monitor.setProgress((int) this.count);
  247. return !(monitor.isCanceled());
  248. }
  249. /**
  250. * 當傳輸結束時,調用end方法。
  251. */
  252. public void end() {
  253. monitor.close();
  254. }
  255. }
  256. }












免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM