ssslinppp

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
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. }











posted on 2017-01-04 15:09  ssslinppp  阅读(7735)  评论(2编辑  收藏  举报