ITeye博客

转:JAVA实现SFTP上传,下载,删除等方法

so_fast · 2013-12-26 10:33 · java · Web前端 · Markdown

  1. 原载:http://blog.csdn.net/haidage/article/details/6859716
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.io.LineNumberReader;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Properties;
  13. import com.jcraft.jsch.Channel;
  14. import com.jcraft.jsch.ChannelSftp;
  15. import com.jcraft.jsch.JSch;
  16. import com.jcraft.jsch.Session;
  17. import com.jcraft.jsch.SftpException;
  18. public class SFTPUtil {
  19. private String host = "127.0.0.1";
  20. private String username="MingMac";
  21. private String password="×××";
  22. private int port = 22;
  23. private ChannelSftp sftp = null;
  24. private String localPath = "/var/test";
  25. private String remotePath = "/var/tset";
  26. private String fileListPath = "/var/test/java/test.txt";
  27. private final String seperator = "/";
  28. /**
  29. * connect server via sftp
  30. */
  31. public void connect() {
  32. try {
  33. if(sftp != null){
  34. System.out.println("sftp is not null");
  35. }
  36. JSch jsch = new JSch();
  37. jsch.getSession(username, host, port);
  38. Session sshSession = jsch.getSession(username, host, port);
  39. System.out.println("Session created.");
  40. sshSession.setPassword(password);
  41. Properties sshConfig = new Properties();
  42. sshConfig.put("StrictHostKeyChecking", "no");
  43. sshSession.setConfig(sshConfig);
  44. sshSession.connect();
  45. System.out.println("Session connected.");
  46. System.out.println("Opening Channel.");
  47. Channel channel = sshSession.openChannel("sftp");
  48. channel.connect();
  49. sftp = (ChannelSftp) channel;
  50. System.out.println("Connected to " + host + ".");
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. /**
  56. * Disconnect with server
  57. */
  58. public void disconnect() {
  59. if(this.sftp != null){
  60. if(this.sftp.isConnected()){
  61. this.sftp.disconnect();
  62. }else if(this.sftp.isClosed()){
  63. System.out.println("sftp is closed already");
  64. }
  65. }
  66. }
  67. public void download() {
  68. // TODO Auto-generated method stub
  69. }
  70. private void download(String directory, String downloadFile,String saveFile, ChannelSftp sftp) {
  71. try {
  72. sftp.cd(directory);
  73. File file = new File(saveFile);
  74. sftp.get(downloadFile, new FileOutputStream(file));
  75. } catch (Exception e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. /**
  80. * upload all the files to the server
  81. */
  82. public void upload() {
  83. List<String> fileList = this.getFileEntryList(fileListPath);
  84. try {
  85. if(fileList != null){
  86. for (String filepath : fileList) {
  87. String localFile = this.localPath + this.seperator+ filepath;
  88. File file = new File(localFile);
  89. if(file.isFile()){
  90. System.out.println("localFile : " + file.getAbsolutePath());
  91. String remoteFile = this.remotePath + this.seperator + filepath;
  92. System.out.println("remotePath:" + remoteFile);
  93. File rfile = new File(remoteFile);
  94. String rpath = rfile.getParent();
  95. try {
  96. createDir(rpath, sftp);
  97. } catch (Exception e) {
  98. System.out.println("*******create path failed" + rpath);
  99. }
  100. this.sftp.put(new FileInputStream(file), file.getName());
  101. System.out.println("=========upload down for " + localFile);
  102. }
  103. }
  104. }
  105. } catch (FileNotFoundException e) {
  106. // TODO Auto-generated catch block
  107. e.printStackTrace();
  108. } catch (SftpException e) {
  109. // TODO Auto-generated catch block
  110. e.printStackTrace();
  111. }
  112. }
  113. /**
  114. * create Directory
  115. * @param filepath
  116. * @param sftp
  117. */
  118. private void createDir(String filepath, ChannelSftp sftp){
  119. boolean bcreated = false;
  120. boolean bparent = false;
  121. File file = new File(filepath);
  122. String ppath = file.getParent();
  123. try {
  124. this.sftp.cd(ppath);
  125. bparent = true;
  126. } catch (SftpException e1) {
  127. bparent = false;
  128. }
  129. try {
  130. if(bparent){
  131. try {
  132. this.sftp.cd(filepath);
  133. bcreated = true;
  134. } catch (Exception e) {
  135. bcreated = false;
  136. }
  137. if(!bcreated){
  138. this.sftp.mkdir(filepath);
  139. bcreated = true;
  140. }
  141. return;
  142. }else{
  143. createDir(ppath,sftp);
  144. this.sftp.cd(ppath);
  145. this.sftp.mkdir(filepath);
  146. }
  147. } catch (SftpException e) {
  148. System.out.println("mkdir failed :" + filepath);
  149. e.printStackTrace();
  150. }
  151. try {
  152. this.sftp.cd(filepath);
  153. } catch (SftpException e) {
  154. e.printStackTrace();
  155. System.out.println("can not cd into :" + filepath);
  156. }
  157. }
  158. /**
  159. * get all the files need to be upload or download
  160. * @param file
  161. * @return
  162. */
  163. private List<String> getFileEntryList(String file){
  164. ArrayList<String> fileList = new ArrayList<String>();
  165. InputStream in = null;
  166. try {
  167. in = new FileInputStream(file);
  168. InputStreamReader inreader = new InputStreamReader(in);
  169. LineNumberReader linreader = new LineNumberReader(inreader);
  170. String filepath = linreader.readLine();
  171. while(filepath != null){
  172. fileList.add(filepath);
  173. filepath = linreader.readLine();
  174. }
  175. in.close();
  176. } catch (FileNotFoundException e) {
  177. // TODO Auto-generated catch block
  178. e.printStackTrace();
  179. } catch (IOException e) {
  180. // TODO Auto-generated catch block
  181. e.printStackTrace();
  182. }finally{
  183. if(in != null){
  184. in = null;
  185. }
  186. }
  187. return fileList;
  188. }
  189. /**
  190. * @return the host
  191. */
  192. public String getHost() {
  193. return host;
  194. }
  195. /**
  196. * @param host the host to set
  197. */
  198. public void setHost(String host) {
  199. this.host = host;
  200. }
  201. /**
  202. * @return the username
  203. */
  204. public String getUsername() {
  205. return username;
  206. }
  207. /**
  208. * @param username the username to set
  209. */
  210. public void setUsername(String username) {
  211. this.username = username;
  212. }
  213. /**
  214. * @return the password
  215. */
  216. public String getPassword() {
  217. return password;
  218. }
  219. /**
  220. * @param password the password to set
  221. */
  222. public void setPassword(String password) {
  223. this.password = password;
  224. }
  225. /**
  226. * @return the port
  227. */
  228. public int getPort() {
  229. return port;
  230. }
  231. /**
  232. * @param port the port to set
  233. */
  234. public void setPort(int port) {
  235. this.port = port;
  236. }
  237. /**
  238. * @return the sftp
  239. */
  240. public ChannelSftp getSftp() {
  241. return sftp;
  242. }
  243. /**
  244. * @param sftp the sftp to set
  245. */
  246. public void setSftp(ChannelSftp sftp) {
  247. this.sftp = sftp;
  248. }
  249. /**
  250. * @return the localPath
  251. */
  252. public String getLocalPath() {
  253. return localPath;
  254. }
  255. /**
  256. * @param localPath the localPath to set
  257. */
  258. public void setLocalPath(String localPath) {
  259. this.localPath = localPath;
  260. }
  261. /**
  262. * @return the remotePath
  263. */
  264. public String getRemotePath() {
  265. return remotePath;
  266. }
  267. /**
  268. * @param remotePath the remotePath to set
  269. */
  270. public void setRemotePath(String remotePath) {
  271. this.remotePath = remotePath;
  272. }
  273. /**
  274. * @return the fileListPath
  275. */
  276. public String getFileListPath() {
  277. return fileListPath;
  278. }
  279. /**
  280. * @param fileListPath the fileListPath to set
  281. */
  282. public void setFileListPath(String fileListPath) {
  283. this.fileListPath = fileListPath;
  284. }
  285. public static void main(String[] args) {
  286. // TODO Auto-generated method stub
  287. SFTPUtil ftp= new SFTPUtil();
  288. ftp.connect();
  289. ftp.upload();
  290. ftp.disconnect();
  291. System.exit(0);
  292. }
  293. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。