Obtain the last modification time of the file on FTP

Source: Internet
Author: User
Tags ftp connection ftp file
  1. /**
  2. *
  3. */
  4. Package com. zjpii. hxj. util;
  5. Import java. Io. ioexception;
  6. Import java.net. socketexception;
  7. Import java. Text. simpledateformat;
  8. Import java. util. date;
  9. Import java. util. hashmap;
  10. Import java. util. Map;
  11. Import org.apache.commons.net. FTP. ftpclient;
  12. Import org.apache.commons.net. FTP. ftpfile;
  13. /**
  14. * Operations on files on FTP
  15. * Use Apache's commons-net-1.4.1.jar package
  16. * @ Author hxj
  17. * @ Date 2008-11-1
  18. *
  19. */
  20. Public class ftputil {
  21. /**
  22. * Obtain the last modification time of the FTP file.
  23. * @ Param host FTP host address e.g. "127.0.0.1"
  24. * @ Param username e.g. "username"
  25. * @ Param Password e.g. "passowrd"
  26. * @ Param path file path e.g. "ftputil/test.txt"
  27. * @ Return date: If the connection is successful, return the java. util. Date time. If the connection fails, return null.
  28. * @ Throws ioexception
  29. * @ Throws socketexception
  30. */
  31. Public date getfilelastmodifiedtime (string host, string username, string password, string path) throws socketexception, ioexception {
  32. Date lastmodifieddate = NULL;
  33. Ftpclient = new ftpclient ();
  34. Ftpclient. Connect (host );
  35. Boolean islogin = ftpclient. login (username, password );
  36. // Return NULL if FTP connection fails
  37. If (! Islogin ){
  38. Ftpclient. Disconnect ();
  39. Ftpclient = NULL;
  40. Return NULL;
  41. }
  42. // Obtain the file in the path on the FTP
  43. Ftpfile [] filelist = ftpclient. listfiles (PATH );
  44. For (INT I = 0; I <filelist. length; I ++ ){
  45. Lastmodifieddate = filelist [I]. gettimestamp (). gettime ();
  46. }
  47. Return lastmodifieddate;
  48. }
  49. /**
  50. * Obtain the modification time of all files in the FTP path.
  51. * @ Param host FTP host address e.g. "127.0.0.1"
  52. * @ Param username e.g. "username"
  53. * @ Param Password e.g. "passowrd"
  54. * @ Param path file path e.g. "ftputil"
  55. * @ Return java. util. Map returns a map whose key is the file name and whose value is the last modification time (Java. util. Date). If the connection fails, null is returned.
  56. * @ Throws socketexception
  57. * @ Throws ioexception
  58. */
  59. Public map getallfilelastmodifiedtime (string host, string username, string password, string path) throws socketexception, ioexception {
  60. Date lastmodifieddate = NULL;
  61. Ftpclient = new ftpclient ();
  62. Ftpclient. Connect (host );
  63. Boolean islogin = ftpclient. login (username, password );
  64. // Return NULL if FTP connection fails
  65. If (! Islogin ){
  66. Ftpclient. Disconnect ();
  67. Ftpclient = NULL;
  68. Return NULL;
  69. }
  70. // Obtain the file in the path on the FTP
  71. Ftpfile [] filelist = ftpclient. listfiles (PATH );
  72. // Hashmap capacity: filelist. Length
  73. Map map = new hashmap (filelist. Length );
  74. For (INT I = 0; I <filelist. length; I ++ ){
  75. Lastmodifieddate = filelist [I]. gettimestamp (). gettime ();
  76. // Key file name; value modification time (Java. util. Date)
  77. Map. Put (filelist [I]. getname (), lastmodifieddate );
  78. }
  79. Return map;
  80. }
  81. /**
  82. * @ Param ARGs
  83. */
  84. Public static void main (string [] ARGs ){
  85. Ftputil = new ftputil ();
  86. Try {
  87. String host = "134.98.8.214 ";
  88. String username = "HZ ";
  89. String Password = "HZ ";
  90. Simpledateformat dateformat = new simpledateformat ("yyyy-mm-dd hh: mm: dd ");
  91. Date = ftputil. getfilelastmodifiedtime (host, username, password, "qxtmp/wangjun-test.txt ");
  92. System. Out. println ("Method1:" + dateformat. Format (date ));
  93. Map map = ftputil. getallfilelastmodifiedtime (host, username, password, "qxtmp ");
  94. System. Out. println (Map. Size ());
  95. System. Out. println ("method2:" + dateformat. Format (Map. Get ("ipashd.txt ")));
  96. } Catch (socketexception e ){
  97. E. printstacktrace ();
  98. } Catch (ioexception e ){
  99. E. printstacktrace ();
  100. }
  101. }
  102. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.