导读


由于项目需要,使用了ftp上传下载,这里记录下工具类。

使用


依赖

  1. <!--文件上传-->
  2. <dependency>
  3. <groupId>commons-fileupload</groupId>
  4. <artifactId>commons-fileupload</artifactId>
  5. <version>1.3.3</version>
  6. </dependency>
  7. <!--FTPClient-->
  8. <dependency>
  9. <groupId>commons-net</groupId>
  10. <artifactId>commons-net</artifactId>
  11. <version>3.1</version>
  12. </dependency>

工具类

  1. import org.apache.commons.net.ftp.FTPClient;
  2. import org.apache.commons.net.ftp.FTPFile;
  3. import org.apache.commons.net.ftp.FTPReply;
  4. import java.io.*;
  5. /**
  6. * FTP上传下载工具类
  7. */
  8. public class FtpUtils {
  9. public static void main(String[] args) {
  10. uploadFtpFile("127.0.0.1", 21, "root", "123456", "/", "aa.exe", "F:\\", "_60_2.exe");
  11. downloadFtpFile("127.0.0.1", 21, "root", "123456", "/", "aa.exe", "F:\\test\\", "bb.exe");
  12. }
  13. /**
  14. * @param ftpHost 主机IP
  15. * @param ftpPort 主机端口号
  16. * @param ftpUser FTP用户名
  17. * @param ftpPass FTP密码
  18. * @return
  19. */
  20. public static FTPClient getFTPClient(String ftpHost, int ftpPort, String ftpUser, String ftpPass) {
  21. FTPClient ftpClient = null;
  22. try {
  23. ftpClient = new FTPClient();
  24. ftpClient.connect(ftpHost, ftpPort); //连接ftp服务器
  25. if (ftpClient.login(ftpUser, ftpPass) && FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { //登录ftp服务器
  26. ftpClient.setControlEncoding("UTF-8");
  27. ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
  28. ftpClient.enterLocalPassiveMode();
  29. System.out.println("FTP连接成功 ftpHost:" + ftpHost + ",ftpPort:" + ftpPort + ",ftpUser:" + ftpUser + ",ftpPass:" + ftpPass);
  30. } else {
  31. System.err.println("FTP连接失败 ftpHost:" + ftpHost + ",ftpPort:" + ftpPort + ",ftpUser:" + ftpUser + ",ftpPass:" + ftpPass);
  32. }
  33. } catch (Exception e) {
  34. System.err.println("FTP连接失败 ftpHost:" + ftpHost + ",ftpPort:" + ftpPort + ",ftpUser:" + ftpUser + ",ftpPass:" + ftpPass);
  35. e.printStackTrace();
  36. ftpClient = null;
  37. }
  38. return ftpClient;
  39. }
  40. /**
  41. * @param ftpHost 主机IP
  42. * @param ftpPort 主机端口号
  43. * @param ftpUser FTP用户名
  44. * @param ftpPass FTP密码
  45. * @param remotePath 远程路径
  46. * @param remoteFileName 远程文件名
  47. * @param localPath 本地路径
  48. * @param localFileName 本地文件名
  49. * @param remotePathFlag true 参考remotePath在localPath下创建路径 false直接将文件保存到localPath目录下
  50. * @return
  51. */
  52. public static boolean downloadFtpFile(String ftpHost, int ftpPort, String ftpUser, String ftpPass, String remotePath, String remoteFileName, String localPath, String localFileName, boolean remotePathFlag) {
  53. FTPClient ftpClient = null;
  54. OutputStream os = null;
  55. try {
  56. if (remotePathFlag) {
  57. localPath = localPath + File.separator + remotePath;
  58. }
  59. File localFilePath = new File(localPath);
  60. if (!localFilePath.exists()) {
  61. localFilePath.mkdirs();
  62. }
  63. File localFile = new File(localPath + File.separator + localFileName);
  64. if (localFile.exists()) {
  65. return true;
  66. }
  67. ftpClient = getFTPClient(ftpHost, ftpPort, ftpUser, ftpPass);
  68. if (null == ftpClient) {
  69. return false;
  70. }
  71. ftpClient.setBufferSize(10 * 1024);
  72. if (ftpClient.changeWorkingDirectory(remotePath)) {
  73. os = new FileOutputStream(localFile);
  74. if (ftpClient.retrieveFile(remoteFileName, os)) {
  75. System.out.println("FTP文件下载成功remote:" + remotePath + File.separator + remoteFileName + ",local:" + localPath + File.separator + localFileName);
  76. } else {
  77. return false;
  78. }
  79. } else {
  80. return false;
  81. }
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. return false;
  85. } finally {
  86. try {
  87. if (os != null) {
  88. os.close();
  89. }
  90. if (ftpClient != null) {
  91. ftpClient.logout();
  92. }
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. return true;
  98. }
  99. /**
  100. * @param ftpHost ip
  101. * @param ftpPort 端口
  102. * @param ftpUser 用户名
  103. * @param ftpPass 密码
  104. * @param remotePath 远程文件路径
  105. * @param remoteFileName 远程文件名
  106. * @param localPath 本地文件路径
  107. * @param localFileName 本地文件名
  108. * @return
  109. */
  110. public static boolean downloadFtpFile(String ftpHost, int ftpPort, String ftpUser, String ftpPass, String remotePath, String remoteFileName, String localPath, String localFileName) {
  111. return downloadFtpFile(ftpHost, ftpPort, ftpUser, ftpPass, remotePath, remoteFileName, localPath, localFileName, false);
  112. }
  113. /**
  114. * @param ftpHost
  115. * @param ftpPort
  116. * @param ftpUser
  117. * @param ftpPass
  118. * @param remotePath
  119. * @param remoteFileName
  120. * @param localPath
  121. * @param localFileName
  122. * @return
  123. */
  124. public static boolean uploadFtpFile(String ftpHost, int ftpPort, String ftpUser, String ftpPass, String remotePath, String remoteFileName, String localPath, String localFileName) {
  125. FTPClient ftpClient = null;
  126. InputStream is = null;
  127. try {
  128. ftpClient = getFTPClient(ftpHost, ftpPort, ftpUser, ftpPass);
  129. if (null == ftpClient) {
  130. return false;
  131. }
  132. ftpClient.setBufferSize(10 * 1024);
  133. if (!ftpClient.changeWorkingDirectory(remotePath)) {
  134. ftpClient.makeDirectory(remotePath);
  135. ftpClient.changeWorkingDirectory(remotePath);
  136. }
  137. is = new FileInputStream(localPath + File.separator + localFileName);
  138. if (ftpClient.storeFile(remoteFileName, is)) {
  139. System.out.println("FTP文件上传成功remote:" + remotePath + File.separator + remoteFileName + ",local:" + localPath + File.separator + localFileName);
  140. }
  141. } catch (Exception e) {
  142. e.printStackTrace();
  143. return false;
  144. } finally {
  145. try {
  146. if (is != null) {
  147. is.close();
  148. }
  149. if (ftpClient != null) {
  150. ftpClient.logout();
  151. }
  152. } catch (Exception e) {
  153. e.printStackTrace();
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. public static FTPFile[] listFiles(String workDirectory, String ftpHost, int ftpPort, String ftpUser, String ftpPass) {
  160. FTPFile[] listFiles = null;
  161. FTPClient ftpClient = null;
  162. try {
  163. ftpClient = getFTPClient(ftpHost, ftpPort, ftpUser, ftpPass);
  164. if (null == ftpClient) {
  165. return null;
  166. }
  167. if (!ftpClient.changeWorkingDirectory(workDirectory)) {
  168. ftpClient.makeDirectory(workDirectory);
  169. ftpClient.changeWorkingDirectory(workDirectory);
  170. }
  171. listFiles = ftpClient.listFiles();
  172. } catch (Exception e) {
  173. e.printStackTrace();
  174. return null;
  175. } finally {
  176. try {
  177. if (ftpClient != null) {
  178. ftpClient.logout();
  179. }
  180. } catch (Exception e) {
  181. e.printStackTrace();
  182. return null;
  183. }
  184. }
  185. return listFiles;
  186. }
  187. }

测试

服务器端

image.png

本地

image.png

END


搞定~