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. }







【SFTP】使用Jsch实现Sftp文件上传-支持断点续传和进程监控的更多相关文章

  1. FTP文件上传 支持断点续传 并 打印下载进度(二) —— 单线程实现

    这个就看代码,哈哈哈哈哈  需要用到的jar包是: <dependency> <groupId>commons-net</groupId> <artifact ...

  2. 【SFTP】使用Jsch实现Sftp文件下载-支持断点续传和进程监控

    参考上篇文章: <[SFTP]使用Jsch实现Sftp文件下载-支持断点续传和进程监控>:http://www.cnblogs.com/ssslinppp/p/6248763.html  ...

  3. skymvc文件上传支持多文件上传

    skymvc文件上传支持多文件上传 支持单文件.多文件上传 可以设定 文件大小.存储目录.文件类型 //上传的文件目录 $this->upload->uploaddir="att ...

  4. .net core版 文件上传/ 支持批量上传,拖拽以及预览,bootstrap fileinput上传文件

    asp.net mvc请移步 mvc文件上传支持批量上传,拖拽以及预览,文件内容校验 本篇内容主要解决.net core中文件上传的问题  开发环境:ubuntu+vscode 1.导入所需要的包:n ...

  5. 4GB以上超大文件上传和断点续传服务器的实现

    随着视频网站和大数据应用的普及,特别是高清视频和4K视频应用的到来,超大文件上传已经成为了日常的基础应用需求. 但是在很多情况下,平台运营方并没有大文件上传和断点续传的开发经验,往往在网上找一些简单的 ...

  6. mvc文件上传支持批量上传,拖拽以及预览,文件内容校验等

    使用bootstrap-fileinput 使用方式: 1.nuget:Install-Package bootstrap-fileinput 2.语言本地化{下载fileinput_locale_z ...

  7. PHP实现阿里云OSS文件上传(支持批量)

    上传文件至阿里云OSS,整体逻辑是,文件先临时上传到本地,然后在上传到OSS,最后删除本地的临时文件(也可以不删,具体看自己的业务需求),具体实现流程如下:   1.下载阿里云OSS对象上传SDK(P ...

  8. php大文件上传支持断点上传

    文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...

  9. asp.net mvc大文件上传、断点续传功能。

    文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...

随机推荐

  1. C#获得网卡信息 NetworkInterface IPInterfaceProperties

    System.Net.NetworkInformation下的 1:NetworkInterface类,提供网络适配器的配置和统计信息. 可以通过它检测本机配置了多少网卡,哪些网络连接可用,获得网卡的 ...

  2. 必须掌握的八个cmd 命令

    一,ping 它是用来检查网络是否通畅或者网络连接速度的命令.作为一个生活在网络上的管理员或者黑客来说,ping命令是第一个必须掌握的DOS命令,它 所利用的原理是这样的:网络上的机器都有唯一确定的I ...

  3. maven - 安装与配置

    最近在了解maven的相关东西,在网上查了一些资料,现在简单整理一下. 一.maven 安装 1.先检查JDK ,Maven是一个java工具,所以请确保jdk环境已经正确安装在你的机器上. 2.点击 ...

  4. forEach遍历数组实例

    forEach()取出数组中2的倍数和3的倍数的数 //for IE if(!Array.prototype.forEach){ Array.prototype.forEach = function( ...

  5. c51

    ORG 0000HMOV R7,#08HMOV 83H,#01HMOV R4,#00HAA1:CLR P3.6 CLR P3.4 SETB P3.6 DJNZ R7,AA1AA2:JB P3.0,AA ...

  6. Lua IO库详解

    I/O 库提供了两套不同风格的文件处理接口. 第一种风格使用隐式的文件句柄: 它提供设置默认输入文件及默认输出文件的操作, 所有的输入输出操作都针对这些默认文件. 第二种风格使用显式的文件句柄. 当使 ...

  7. Linux删除apt-get安装的软件

    我们都知道安装软件最简单的方法是apt-get install,但是卸载就不常用了,如何卸载呢?1.sudo apt-get remove xxxxxsudo apt-get autoremove2. ...

  8. C#的提交表单方式主要有两种WebClient与HttpWebRequest

    根据黄聪:C#模拟网站页面POST数据提交表单(转) using System; using System.Collections.Generic; using System.IO; using Sy ...

  9. bash while/until循环学习

    while循环:条件满足,则循环:失败,则退出 如何退出? 必须有时刻,条件测试不成功 ? :条件控制变量 while 条件测试:do 循环体 done until循环:条件不满足,则循环:否则,退出 ...

  10. quick sort 的简化实现

    Pivot 随机选取意义不大 第一种方法使用随机pivot,使得尽可能平均二分序列,而实际上一般来说需要排序的集合往往是乱序的,无需重新生成随机数作为pivot,大可使用固定位置的数作为pivot,这 ...