Java.ftp上传下载

约定不等于承诺〃 2021-09-20 13:38 549阅读 0赞

1:jar的maven的引用:

  1. 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. 3
  4. 4 <!-- jar包依赖 -->
  5. 5 <dependencies>
  6. 6
  7. 7 <!-- Apache工具组件 -->
  8. 8 <dependency>
  9. 9 <groupId>commons-net</groupId>
  10. 10 <artifactId>commons-net</artifactId>
  11. 11 </dependency>
  12. 12
  13. 13 </dependencies>
  14. 14
  15. 15
  16. 16 </project>

2:javaCode:

  1. 1 package com.taotao.common.utils;
  2. 2
  3. 3 import java.io.File;
  4. 4 import java.io.FileInputStream;
  5. 5 import java.io.FileNotFoundException;
  6. 6 import java.io.FileOutputStream;
  7. 7 import java.io.IOException;
  8. 8 import java.io.InputStream;
  9. 9 import java.io.OutputStream;
  10. 10
  11. 11 import org.apache.commons.net.ftp.FTP;
  12. 12 import org.apache.commons.net.ftp.FTPClient;
  13. 13 import org.apache.commons.net.ftp.FTPFile;
  14. 14 import org.apache.commons.net.ftp.FTPReply;
  15. 15
  16. 16 /**
  17. 17 *
  18. 18 * @ClassName: FtpUtil
  19. 19 * @Description: ftp上传下载工具类
  20. * 来源:传智播客
  21. 20 * @author: 刘军/shall_liu(1136808529@qq.com)
  22. 21 * @date: 2017年8月26日 下午1:22:06
  23. 22 *
  24. 23 * @Copyright: 2017
  25. 24 */
  26. 25 public class FtpUtil {
  27. 26
  28. 27 /**
  29. 28 * Description: 向FTP服务器上传文件
  30. 29 * @param host FTP服务器hostname
  31. 30 * @param port FTP服务器端口
  32. 31 * @param username FTP登录账号
  33. 32 * @param password FTP登录密码
  34. 33 * @param basePath FTP服务器基础目录
  35. 34 * @param filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath
  36. 35 * @param filename 上传到FTP服务器上的文件名
  37. 36 * @param input 输入流
  38. 37 * @return 成功返回true,否则返回false
  39. 38 */
  40. 39 public static boolean uploadFile(String host, int port, String username, String password, String basePath,
  41. 40 String filePath, String filename, InputStream input) {
  42. 41 boolean result = false;
  43. 42 FTPClient ftp = new FTPClient();
  44. 43 try {
  45. 44 int reply;
  46. 45 ftp.connect(host, port);// 连接FTP服务器
  47. 46 // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
  48. 47 ftp.login(username, password);// 登录
  49. 48 reply = ftp.getReplyCode();
  50. 49 if (!FTPReply.isPositiveCompletion(reply)) {
  51. 50 ftp.disconnect();
  52. 51 return result;
  53. 52 }
  54. 53 //切换到上传目录
  55. 54 if (!ftp.changeWorkingDirectory(basePath+filePath)) {
  56. 55 //如果目录不存在创建目录
  57. 56 String[] dirs = filePath.split("/");
  58. 57 String tempPath = basePath;
  59. 58 for (String dir : dirs) {
  60. 59 if (null == dir || "".equals(dir)) continue;
  61. 60 tempPath += "/" + dir;
  62. 61 if (!ftp.changeWorkingDirectory(tempPath)) {
  63. 62 if (!ftp.makeDirectory(tempPath)) {
  64. 63 return result;
  65. 64 } else {
  66. 65 ftp.changeWorkingDirectory(tempPath);
  67. 66 }
  68. 67 }
  69. 68 }
  70. 69 }
  71. 70 //设置上传文件的类型为二进制类型
  72. 71 ftp.setFileType(FTP.BINARY_FILE_TYPE);
  73. 72 //上传文件
  74. 73 if (!ftp.storeFile(filename, input)) {
  75. 74 return result;
  76. 75 }
  77. 76 input.close();
  78. 77 ftp.logout();
  79. 78 result = true;
  80. 79 } catch (IOException e) {
  81. 80 e.printStackTrace();
  82. 81 } finally {
  83. 82 if (ftp.isConnected()) {
  84. 83 try {
  85. 84 ftp.disconnect();
  86. 85 } catch (IOException ioe) {
  87. 86 }
  88. 87 }
  89. 88 }
  90. 89 return result;
  91. 90 }
  92. 91
  93. 92 /**
  94. 93 * Description: 从FTP服务器下载文件
  95. 94 * @param host FTP服务器hostname
  96. 95 * @param port FTP服务器端口
  97. 96 * @param username FTP登录账号
  98. 97 * @param password FTP登录密码
  99. 98 * @param remotePath FTP服务器上的相对路径
  100. 99 * @param fileName 要下载的文件名
  101. 100 * @param localPath 下载后保存到本地的路径
  102. 101 * @return
  103. 102 */
  104. 103 public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
  105. 104 String fileName, String localPath) {
  106. 105 boolean result = false;
  107. 106 FTPClient ftp = new FTPClient();
  108. 107 try {
  109. 108 int reply;
  110. 109 ftp.connect(host, port);
  111. 110 // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
  112. 111 ftp.login(username, password);// 登录
  113. 112 reply = ftp.getReplyCode();
  114. 113 if (!FTPReply.isPositiveCompletion(reply)) {
  115. 114 ftp.disconnect();
  116. 115 return result;
  117. 116 }
  118. 117 ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
  119. 118 FTPFile[] fs = ftp.listFiles();
  120. 119 for (FTPFile ff : fs) {
  121. 120 if (ff.getName().equals(fileName)) {
  122. 121 File localFile = new File(localPath + "/" + ff.getName());
  123. 122
  124. 123 OutputStream is = new FileOutputStream(localFile);
  125. 124 ftp.retrieveFile(ff.getName(), is);
  126. 125 is.close();
  127. 126 }
  128. 127 }
  129. 128
  130. 129 ftp.logout();
  131. 130 result = true;
  132. 131 } catch (IOException e) {
  133. 132 e.printStackTrace();
  134. 133 } finally {
  135. 134 if (ftp.isConnected()) {
  136. 135 try {
  137. 136 ftp.disconnect();
  138. 137 } catch (IOException ioe) {
  139. 138 }
  140. 139 }
  141. 140 }
  142. 141 return result;
  143. 142 }
  144. 143
  145. 144 public static void main(String[] args) {
  146. 145 try {
  147. 146 FileInputStream in=new FileInputStream(new File("D:\\temp\\image\\gaigeming.jpg"));
  148. 147 boolean flag = uploadFile("192.168.25.133", 21, "ftpuser", "ftpuser", "/home/ftpuser/www/images","/2015/01/21", "gaigeming.jpg", in);
  149. 148 System.out.println(flag);
  150. 149 } catch (FileNotFoundException e) {
  151. 150 e.printStackTrace();
  152. 151 }
  153. 152 }
  154. 153 }
  155. 154

转载于:https://www.cnblogs.com/ios9/p/JavaCode\_Ftp\_UpDownload.html

发表评论

表情:
评论列表 (有 0 条评论,549人围观)

还没有评论,来说两句吧...

相关阅读

    相关 sftp下载

    """使用paramiko模块中的sftp登陆远程主机,实现上传和下载功能""" \ 根据输入参数判断是文件还是目录,进行上传和下载 \ 本地参数local需要与远程参数