FTP 工具类(ftp4j) 分类: Java2010-12-31 17:50 209 人阅读 评论(0) 收藏 举报 view plain 1. package com.lyis.commons.util; 2. import it.sauronsoftware.ftp4j.FTPClient; 3. import it.sauronsoftware.ftp4j.FTPException; 4. import it.sauronsoftware.ftp4j.FTPFile; 5. import java.io.File; 6. import java.net.URL; 7. import java.util.Arrays; 8. import java.util.Comparator; 9. import org.apache.log4j.Logger; 10. /** 11. * FTP 文件处理工具类 12. * 13. * @author Johnson 14. * @version Tuesday December 28th, 2010 15. */ 16. public class FTPUtils { 17. private Logger log = Logger.getLogger(this.getClass()); 18. private static FTPUtils ftp; 19. /** 20. * FTP 服务地址 21. */ 22. private static String ADDRESS = PropUtils.getString("ftp_server_address"); 23. /** 24. * FTP 登录用户名 25. */ 26. private static String USERNAME = PropUtils.getString("ftp_server_username"); 27. /** 28. * FTP 登录密码 29. */ 30. private static String PASSWORD = PropUtils.getString("ftp_server_password"); 31. /** 32. * 构造方法 33. */ 34. protected FTPUtils() { 35. } 36. /** 37. * 实例化对象 38. * 39. * @return 40. */ 41. public static FTPUtils getInstance() { 42. if (ftp == null) { 43. ftp = new FTPUtils(); 44. } 45. return ftp; 46. } 47. /** 48. * 获取FTP 客户端对象 49. * 50. * @return 51. * @throws Exception 52. */ 53. private FTPClient getClient() throws Exception { 54. FTPClient client = new FTPClient(); 55. client.setCharset("utf-8"); 56. client.setType(FTPClient.TYPE_BINARY); 57. URL url = new URL(FTPUtils.ADDRESS); 58. int port = url.getPort() < 1 ? 21 : url.getPort(); 59. log.info("Ftp server listening on address " + url.toString()); 60. client.connect(url.getHost(), port); 61. client.login(FTPUtils.USERNAME, FTPUtils.PASSWORD); 62. return client; 63. } 64. /**...