9442db23b5e8419b21ed2863f2ae09ab7e2e6381.svn-base 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package com.chinacreator.process.util.ftp;
  2. import com.jcraft.jsch.*;
  3. import org.apache.commons.io.IOUtils;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import java.io.*;
  7. import java.util.Properties;
  8. import java.util.Vector;
  9. /**
  10. * 类说明 sftp工具类
  11. */
  12. public class SftpUtil2 {
  13. private transient Logger log = LoggerFactory.getLogger(this.getClass());
  14. private ChannelSftp sftp;
  15. private Session session;
  16. /** SFTP 登录用户名*/
  17. private String username;
  18. /** SFTP 登录密码*/
  19. private String password;
  20. /** 私钥 */
  21. private String privateKey;
  22. /** SFTP 服务器地址IP地址*/
  23. private String host;
  24. /** SFTP 端口*/
  25. private int port;
  26. /**
  27. * 构造基于密码认证的sftp对象
  28. */
  29. public SftpUtil2(String username, String password, String host, int port) {
  30. this.username = username;
  31. this.password = password;
  32. this.host = host;
  33. this.port = port;
  34. }
  35. /**
  36. * 构造基于秘钥认证的sftp对象
  37. */
  38. public SftpUtil2(String username, String host, int port, String privateKey) {
  39. this.username = username;
  40. this.host = host;
  41. this.port = port;
  42. this.privateKey = privateKey;
  43. }
  44. public SftpUtil2(){}
  45. /**
  46. * 连接sftp服务器
  47. */
  48. public void login(){
  49. try {
  50. JSch jsch = new JSch();
  51. if (privateKey != null) {
  52. jsch.addIdentity(privateKey);// 设置私钥
  53. }
  54. session = jsch.getSession(username, host, port);
  55. if (password != null) {
  56. session.setPassword(password);
  57. }
  58. Properties config = new Properties();
  59. config.put("StrictHostKeyChecking", "no");
  60. session.setConfig(config);
  61. session.connect();
  62. Channel channel = session.openChannel("sftp");
  63. channel.connect();
  64. sftp = (ChannelSftp) channel;
  65. } catch (JSchException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. /**
  70. * 关闭连接 server
  71. */
  72. public void logout(){
  73. if (sftp != null) {
  74. if (sftp.isConnected()) {
  75. sftp.disconnect();
  76. }
  77. }
  78. if (session != null) {
  79. if (session.isConnected()) {
  80. session.disconnect();
  81. }
  82. }
  83. }
  84. /**
  85. * 将输入流的数据上传到sftp作为文件。文件完整路径=basePath+directory
  86. * @param basePath 服务器的基础路径
  87. * @param directory 上传到该目录
  88. * @param sftpFileName sftp端文件名
  89. * @param in 输入流
  90. */
  91. public void upload(String basePath,String directory, String sftpFileName, InputStream input) throws SftpException{
  92. try {
  93. sftp.cd(basePath);
  94. sftp.cd(directory);
  95. } catch (SftpException e) {
  96. //目录不存在,则创建文件夹
  97. String [] dirs=directory.split("/");
  98. String tempPath=basePath;
  99. for(String dir:dirs){
  100. if(null== dir || "".equals(dir)) continue;
  101. tempPath+="/"+dir;
  102. try{
  103. sftp.cd(tempPath);
  104. }catch(SftpException ex){
  105. sftp.mkdir(tempPath);
  106. sftp.cd(tempPath);
  107. }
  108. }
  109. }
  110. sftp.put(input, sftpFileName); //上传文件
  111. }
  112. /**
  113. * 下载文件。
  114. * @param directory 下载目录
  115. * @param downloadFile 下载的文件
  116. * @param saveFile 存在本地的路径
  117. */
  118. public void download(String directory, String downloadFile, String saveFile) throws SftpException, FileNotFoundException{
  119. if (directory != null && !"".equals(directory)) {
  120. sftp.cd(directory);
  121. }
  122. File file = new File(saveFile);
  123. sftp.get(downloadFile, new FileOutputStream(file));
  124. }
  125. /**
  126. * 下载文件
  127. * @param directory 下载目录
  128. * @param downloadFile 下载的文件名
  129. * @return 字节数组
  130. */
  131. public byte[] download(String directory, String downloadFile) throws SftpException, IOException{
  132. if (directory != null && !"".equals(directory)) {
  133. sftp.cd(directory);
  134. }
  135. InputStream is = sftp.get(downloadFile);
  136. byte[] fileData = IOUtils.toByteArray(is);
  137. return fileData;
  138. }
  139. /**
  140. * 删除文件
  141. * @param directory 要删除文件所在目录
  142. * @param deleteFile 要删除的文件
  143. */
  144. public void delete(String directory, String deleteFile) throws SftpException{
  145. sftp.cd(directory);
  146. sftp.rm(deleteFile);
  147. }
  148. /**
  149. * 列出目录下的文件
  150. * @param directory 要列出的目录
  151. * @param sftp
  152. */
  153. public Vector<?> listFiles(String directory) throws SftpException {
  154. return sftp.ls(directory);
  155. }
  156. //上传文件测试
  157. public static void main(String[] args) throws SftpException, IOException {
  158. SftpUtil2 sftp = new SftpUtil2("ubuntu", "woshiSong&0224ATTX", "101.32.222.31", 22);
  159. sftp.login();
  160. File file = new File("F:\\1.jpg");
  161. InputStream is = new FileInputStream(file);
  162. sftp.upload("/opt","/", "test_sftp.jpg", is);
  163. sftp.logout();
  164. }
  165. }