您的位置 首页 > 娱乐休闲

javaweb上传文件到服务器中

项目中经常要做上传功能,除了页面使用上传组件外,后台的文件处理一种是 存放在项目中文件夹,另一种存放部署的服务器中,后一种更灵活对项目更友好。

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包,请从官网下载它:

ChannelSftp类是JSch实现SFTP核心类,它包含了所有SFTP的方法,如:

put(): 文件上传

get(): 文件下载

cd(): 进入指定目录

ls(): 得到指定目录下的文件列表

rename(): 重命名指定文件或目录

rm(): 删除指定文件

mkdir(): 创建目录

rmdir(): 删除目录

以下是stfpUtils工具类的方法


  1. package com.;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.u;
  6. import java.u;
  7. import org.a;
  8. import org.;
  9. import org.Factory;
  10. import com.jcra;
  11. import com.jcraSftp;
  12. import com.jcraSftp.LsEntry;
  13. import com.jcra;
  14. import com.jcra;
  15. import com.jcra;
  16. /**
  17. * sftp 工具类
  18. *
  19. * @author weird
  20. */
  21. public final class Sftps {
  22. private static final Logger log = LoggerFac);
  23. private Session sshSession;
  24. private ChannelSftp sftp;
  25. /**
  26. * 连接sftp服务器
  27. * @param host
  28. * @param port
  29. * @param username
  30. * @param password
  31. * @return
  32. * @throws Exception
  33. */
  34. public ChannelSftp connect(String host, int port, String username, String password) throws Exception {
  35. JSch jsch = new JSch();
  36. sshSession = j(username, host, port);
  37. log.debug("Session created.");
  38. (password);
  39. Properties sshConfig = new Properties();
  40. ("StrictHostKeyChecking", "no");
  41. (sshConfig);
  42. ();
  43. log.debug("Session connected.");
  44. log.debug("Opening Channel.");
  45. Channel channel = ("sftp");
  46. c();
  47. sftp = (ChannelSftp) channel;
  48. log.debug("Connected to " + host + ".");
  49. return sftp;
  50. }
  51. /**
  52. * 连接sftp服务器
  53. * @param host
  54. * @param port
  55. * @param username
  56. * @param privateKey
  57. * @param passphrase
  58. * @return
  59. * @throws Exception
  60. */
  61. public ChannelSftp connect(String host, int port, String username, String privateKey ,String passphrase) throws Exception {
  62. JSch jsch = new JSch();
  63. //设置密钥和密码
  64. if (!S(privateKey)) {
  65. if (!S(passphrase)) {
  66. //设置带口令的密钥
  67. j(privateKey, passphrase);
  68. } else {
  69. //设置不带口令的密钥
  70. j(privateKey);
  71. }
  72. }
  73. sshSession = j(username, host, port);
  74. log.debug("Session created.");
  75. Properties sshConfig = new Properties();
  76. ("StrictHostKeyChecking", "no");
  77. (sshConfig);
  78. ();
  79. log.debug("Session connected.");
  80. log.debug("Opening Channel.");
  81. Channel channel = ("sftp");
  82. c();
  83. sftp = (ChannelSftp) channel;
  84. log.debug("Connected to " + host + ".");
  85. return sftp;
  86. }
  87. public void portForwardingL(int lport, String rhost, int rport) throws Exception {
  88. int assinged_port = (lport, rhost, rport);
  89. Sy("localhost:"+assinged_port+" -> "+rhost+":"+rport);
  90. }
  91. /**
  92. * 断开连接
  93. */
  94. public void disconnect() {
  95. if (sftp != null) ();
  96. if (sshSession != null) ();
  97. }
  98. /**
  99. * 上传文件
  100. *
  101. * @param directory
  102. * 上传的目录
  103. * @param uploadFile
  104. * 要上传的文件
  105. * @param sftp
  106. */
  107. public void upload(String directory, String uploadFile) throws Exception {
  108. (directory);
  109. File file = new File(uploadFile);
  110. (new FileInputStream(file), ());
  111. }
  112. public void upload(String directory, File file) throws Exception {
  113. (directory);
  114. (new FileInputStream(file), ());
  115. Sy("upload file "+() + " to host " + ());
  116. }
  117. public void uploadDir(File src, String dst) throws Exception {
  118. if (!exist(dst)) {
  119. (dst);
  120. }
  121. if ()) {
  122. upload(dst, src);
  123. } else {
  124. for (File file : ()) {
  125. if ()) {
  126. uploadDir(file, dst + "/" + ());
  127. }
  128. upload(dst, file);
  129. }
  130. }
  131. }
  132. /**
  133. * 目录是否查找
  134. * @param path
  135. * @return
  136. * @throws SftpException
  137. */
  138. public boolean exist(String path) throws SftpException {
  139. String pwd = ();
  140. try {
  141. (path);
  142. } catch (SftpException e) {
  143. if == C) {
  144. return false;
  145. } else {
  146. throw e;
  147. }
  148. } finally {
  149. (pwd);
  150. }
  151. return true;
  152. }
  153. /**
  154. * 下载文件
  155. * @param directory
  156. * @param downloadFile
  157. * @param saveFile
  158. * @throws Exception
  159. */
  160. public void download(String directory, String downloadFile, String saveFile) throws Exception {
  161. (directory);
  162. File file = new File(saveFile);
  163. (downloadFile, new FileOutputStream(file));
  164. }
  165. /**
  166. * 下载文件
  167. * @param directory
  168. * @param downloadFile
  169. * @param saveFile
  170. * @throws Exception
  171. */
  172. public void download(String directory, String downloadFile, File saveFile) throws Exception {
  173. (directory);
  174. (downloadFile, new FileOutputStream(saveFile));
  175. Sy("download file "+directory + "/" +downloadFile + " from host " + ());
  176. }
  177. /**
  178. * 下载文件
  179. * @param src
  180. * @param dst
  181. * @throws Exception
  182. */
  183. @SuppressWarnings("unchecked")
  184. public void downloadDir(String src, File dst) throws Exception {
  185. try {
  186. (src);
  187. } catch (Exception e) {
  188. // TODO Auto-generated catch block
  189. e.printStackTrace();
  190. }
  191. d();
  192. Vector<LsEntry> files = (src);
  193. for (LsEntry lsEntry : files) {
  194. if ().equals(".") || l().equals("..")) {
  195. continue;
  196. }
  197. if ().startsWith("d")) {
  198. downloadDir(src + "/" + l(), new File(dst, l()));
  199. } else {
  200. download(src, l(), new File(dst, l()));
  201. }
  202. }
  203. }
  204. /**
  205. * 删除文件
  206. * @param directory
  207. * @param deleteFile
  208. * @throws SftpException
  209. */
  210. public void delete(String directory, String deleteFile) throws SftpException {
  211. (directory);
  212. (deleteFile);
  213. }
  214. /**
  215. * 列出目录下的文件
  216. * @param directory
  217. * @return
  218. * @throws SftpException
  219. */
  220. public Vector listFiles(String directory) throws SftpException {
  221. return (directory);
  222. }
  223. public static void main(String[] args) throws Exception {
  224. Sftps sf = new Sftps();
  225. String host = "192.168.56.101";
  226. int port = 22;
  227. String username = "root";
  228. String password = "123";
  229. String privateKey = "C:/Users/Administrator/Desktop/seRT_SSH_key/front/Identity";
  230. String passphrase = "h$VgBrx3nhwH#2!h6zs0uGhzcCX8dTPa";
  231. String src = "/usr/local;;
  232. (host, port, username, privateKey, passphrase);
  233. (1194, "10.169.97.248", 1194);
  234. // ChannelSftp sftp = (host, port, username, password);
  235. // (directory, uploadFile, sftp);
  236. // (directory, downloadFile, saveFile, sftp);
  237. // (directory, deleteFile, sftp);
  238. // try {
  239. //// Vector files = ("/root");
  240. // Dir(src, new File("C:/staticsfile"));
  241. //// Dir(new File("C:/staticsfile"), "/root/temp");
  242. // Sy("finished");
  243. // ();
  244. // } catch (Exception e) {
  245. // e.printStackTrace();
  246. // }
  247. // ();
  248. }
  249. public Session getSshSession() {
  250. return sshSession;
  251. }
  252. public ChannelSftp getSftp() {
  253. return sftp;
  254. }
  255. }

采用的是stfp协议连接。

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“javaweb上传文件到服务器中”边界阅读