工具类---FTP工具类

古城微笑少年丶 2023-07-19 03:40 129阅读 0赞

1. 导入commons-net

  1. <dependency>
  2. <groupId>commons-net</groupId>
  3. <artifactId>commons-net</artifactId>
  4. <version>3.3</version>
  5. </dependency>

2. 创建config.properties文件,配置FTP服务器信息

  1. #ftp服务器地址
  2. ftp.address=182.61.40.184
  3. #ftp默认端口是21
  4. ftp.port=21
  5. #ftp用户名
  6. ftp.username=root
  7. #ftp密码
  8. ftp.password=xxxxxxx

3. 读取config.properties配置文件的工具类ConfigReaderUtil

  1. import java.io.FileNotFoundException;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Properties;
  5. /**
  6. * DESC : 读取配置文件工具类
  7. *
  8. */
  9. public class ConfigReaderUtil {
  10. public ConfigReaderUtil(){}
  11. private static Properties props = new Properties();
  12. static{
  13. try {
  14. //加载配置文件,需要手动指定配置文件
  15. props.load(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"),"UTF-8"));
  16. } catch (FileNotFoundException e) {
  17. e.printStackTrace();
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. /*根据key获取value*/
  23. public static String getValue(String key){
  24. return props.getProperty(key).trim();
  25. }
  26. }

4. FTP工具类

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.nio.charset.Charset;
  8. import org.apache.commons.net.ftp.FTPClient;
  9. import org.apache.commons.net.ftp.FTPFile;
  10. import org.apache.log4j.Logger;
  11. /**
  12. * DESC : ftp工具类
  13. *
  14. * @author Lonely
  15. *
  16. */
  17. public class FTPUtil {
  18. private static Logger logger = Logger.getLogger(FTPUtil.class);
  19. static FTPClient ftp = null;
  20. private static String ftpAddress = ConfigReaderUtil.getValue("ftp.address"); // ftp服务器地址
  21. private static Integer ftpPort = Integer.valueOf(ConfigReaderUtil.getValue("ftp.port")); // ftp端口
  22. private static String ftpUsername = ConfigReaderUtil.getValue("ftp.username"); // ftp用户名
  23. private static String ftpPassword = ConfigReaderUtil.getValue("ftp.password"); // ftp密码
  24. /**
  25. * DESC 登录FTP
  26. * @param ip
  27. * @param port
  28. * @param username
  29. * @param password
  30. * @return
  31. */
  32. public static boolean login() {
  33. try {
  34. ftp = new FTPClient();
  35. ftp.connect(ftpAddress, ftpPort);
  36. boolean loginResu = ftp.login(ftpUsername, ftpPassword);
  37. if (loginResu) {
  38. logger.info("登录FTP成功");
  39. // 设置UTF-8编码
  40. ftp.setCharset(Charset.forName("UTF-8"));
  41. ftp.setControlEncoding("UTF-8");
  42. return true;
  43. } else {
  44. logger.info("登录FTP失败");
  45. return false;
  46. }
  47. } catch (IOException e) {
  48. logger.info("登录FTP出现异常");
  49. e.printStackTrace();
  50. return false;
  51. }
  52. }
  53. /**
  54. * DESC 登出FTP
  55. */
  56. public static void disconnect() {
  57. if (null != ftp && ftp.isConnected()) {
  58. try {
  59. ftp.logout();
  60. ftp.disconnect();
  61. logger.info("登出FTP......");
  62. logger.info("断开FTP......");
  63. } catch (IOException ex) {
  64. }
  65. }
  66. }
  67. /**
  68. * DESC 下载文件
  69. *
  70. * @param remoteFileUrl FTP服务器文件路径 例:/index.html
  71. * @param localFileUrl 要保存到本地的路径 例:D:/index.html
  72. * @return
  73. */
  74. public static boolean downloadFile(String remoteFileUrl, String localFileUrl) {
  75. FileOutputStream fos = null;
  76. InputStream is = null;
  77. boolean loginResu = login(); // 登录
  78. try {
  79. if (loginResu) {
  80. is = ftp.retrieveFileStream(remoteFileUrl);
  81. fos = new FileOutputStream(new File(localFileUrl));
  82. byte[] b = new byte[1024];
  83. int len = 0;
  84. while ((len = is.read(b)) != -1) {
  85. fos.write(b, 0, len);
  86. }
  87. logger.info("下载文件:" + remoteFileUrl + " 成功----------------------");
  88. return true;
  89. } else {
  90. logger.info("下载文件 :" + remoteFileUrl + " 失败----------------------");
  91. return false;
  92. }
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. logger.info("下载文件失败");
  96. return false;
  97. } finally {
  98. if (fos != null) {
  99. try {
  100. fos.close();
  101. } catch (IOException e) {
  102. e.printStackTrace();
  103. }
  104. }
  105. if (is != null) {
  106. try {
  107. is.close();
  108. } catch (IOException e) {
  109. e.printStackTrace();
  110. }
  111. }
  112. }
  113. }
  114. /**
  115. * DESC 上传文件
  116. *
  117. * @param remoteFileUrl 要保存文件的路径 例:/index.html
  118. * @param localFileUrl 要上传的文件路径 例:D:/index.html
  119. * @return
  120. */
  121. public static boolean uploadFile(String remoteFileUrl, String localFileUrl) {
  122. FileInputStream fis = null;
  123. OutputStream os = null;
  124. boolean loginResu = login(); // 登录
  125. try {
  126. if (loginResu) {
  127. os = ftp.storeFileStream(remoteFileUrl);
  128. fis = new FileInputStream(new File(localFileUrl));
  129. byte[] b = new byte[1024];
  130. int len = 0;
  131. while ((len = fis.read(b)) != -1) {
  132. os.write(b, 0, len);
  133. }
  134. logger.info("上传文件:" + localFileUrl + " 成功----------------");
  135. return true;
  136. } else {
  137. logger.info("上传文件:" + localFileUrl + " 失败----------------");
  138. return false;
  139. }
  140. } catch (IOException e) {
  141. e.printStackTrace();
  142. return false;
  143. } finally {
  144. if (fis != null) {
  145. try {
  146. fis.close();
  147. } catch (IOException e) {
  148. e.printStackTrace();
  149. }
  150. }
  151. if (os != null) {
  152. try {
  153. os.close();
  154. } catch (IOException e) {
  155. e.printStackTrace();
  156. }
  157. }
  158. }
  159. }
  160. /**
  161. * DESC 获取ftp某一文件(路径)下的文件名字,用于查看文件列表
  162. * @param remotedir 远程地址目录
  163. * @return
  164. */
  165. public static boolean getFilesName(String remotedir){
  166. boolean loginResu = login(); // 登录
  167. if (loginResu) {
  168. FTPFile[] files;
  169. try {
  170. files = ftp.listFiles(remotedir);
  171. for (int i = 0; i < files.length; i++) {
  172. logger.info(files[i].getName() + "----");
  173. //其他操作
  174. }
  175. } catch (IOException e) {
  176. e.printStackTrace();
  177. }
  178. return true;
  179. } else {
  180. return false;
  181. }
  182. }
  183. /**
  184. * DESC 删除FTP服务器文件
  185. * @param ftpFileName 要删除的文件路径
  186. */
  187. public static boolean deleteFile(String ftpFileName){
  188. boolean loginResu = login(); // 登录
  189. if (loginResu) {
  190. boolean delResu;
  191. try {
  192. delResu = ftp.deleteFile(ftpFileName); //删除文件
  193. if(delResu) {
  194. logger.info("删除FTP服务器文件:" + ftpFileName + "成功");
  195. return true;
  196. }else {
  197. logger.info("删除FTP服务器文件:" + ftpFileName + "失败");
  198. return false;
  199. }
  200. } catch (IOException e) {
  201. e.printStackTrace();
  202. return false;
  203. }
  204. }else {
  205. return false;
  206. }
  207. }
  208. }

调用测试

  1. public static void main(String[] args) {
  2. //打印 /usr 下的文件(文件夹)名
  3. FTPUtil.getFilesName("/usr");
  4. //上传本地 D:/G(other)/temp/index.html 到 FTP服务器 /index.html
  5. FTPUtil.uploadFile("/index.html", "D:/G(other)/temp/index.html");
  6. //下载FTP服务器中的 /index.html 到 本地 D:/G(other)/temp/index.html
  7. FTPUtil.downloadFile("/index.html", "D:/G(other)/temp/index.html");
  8. //删除FTP服务器文件 /index.html
  9. FTPUtil.deleteFile("/index.html");
  10. //登出 断开连接
  11. FTPUtil.disconnect();
  12. }

发表评论

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

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

相关阅读

    相关 FTP测试工具

    在进入正文前,先给大家分享一款比较好用的服务器连接工具: IIS7服务器管理工具是一款windows全系下用于连接并操控基于windows和linux系统的VPS、VNC、