Java code implementation FTP single file download (ii)

Source: Internet
Author: User
Tags ftp connection ftp login

One: Download the Commons-net-1.4.1.jar package, put it under LIB, and load it into the. classpath file

Two: Write Tool class Ftputil.java as follows

  1. Package common;
  2. Import Java.io.File;
  3. Import java.io.FileNotFoundException;
  4. Import Java.io.FileOutputStream;
  5. Import java.io.IOException;
  6. Import Java.io.OutputStream;
  7. Import java.net.SocketException;
  8. Import java.util.Properties;
  9. Import Org.apache.log4j.Logger;
  10. Import Org.apache.commons.logging.Log;
  11. Import Org.apache.commons.logging.LogFactory;
  12. Import org.apache.commons.net.ftp.*;
  13. Public class Ftputil {
  14. Private final static Log logger = Logfactory.getlog (ftputil.   Class);
  15. /** 
  16. * Get FtpClient Object
  17. *
  18. * @param ftphost
  19. * FTP Host Server
  20. * @param FTPPassword
  21. * FTP Login Password
  22. * @param ftpusername
  23. * FTP Login user name
  24. * @param ftpport
  25. * FTP port defaults to 21
  26. * @return
  27. */
  28. public static FtpClient getftpclient (String ftphost, String ftpusername,
  29. String FTPPassword, int ftpport) {
  30. FtpClient ftpclient = new FtpClient ();
  31. try {
  32. FtpClient = new FtpClient ();
  33. Ftpclient.connect (Ftphost, FtpPort); //Connect FTP server
  34. Ftpclient.login (FtpUserName, FTPPassword); //Login FTP server
  35. if (! Ftpreply.ispositivecompletion (Ftpclient.getreplycode ())) {
  36. Logger.info ("Not connected to FTP, user name or password is incorrect.  ");
  37. Ftpclient.disconnect ();
  38. } Else {
  39. Logger.info ("FTP connection succeeded.  ");
  40. }
  41. } catch (SocketException e) {
  42. E.printstacktrace ();
  43. Logger.info ("FTP IP address may be wrong, please configure it correctly.")  ");
  44. } catch (IOException e) {
  45. E.printstacktrace ();
  46. Logger.info ("FTP port error, please configure it correctly.")  ");
  47. }
  48. return ftpclient;
  49. }
  50. /* 
  51. * Download files from FTP server
  52. *
  53. * @param ftphost FTP IP Address
  54. *
  55. * @param ftpusername FTP user name
  56. *
  57. * @param ftppassword FTP user name password
  58. *
  59. * @param ftpport FTP port
  60. *
  61. * @param the path format of files in Ftppath FTP server: FTPTEST/AA
  62. *
  63. * @param localPath Download to local location format: h:/download
  64. *
  65. * @param filename File name
  66. */
  67. public static void Downloadftpfile (String ftphost, String ftpusername,
  68. String FTPPassword, int ftpport, string ftppath, String LocalPath,
  69. String fileName) {
  70. FtpClient ftpclient = null;
  71. try {
  72. FtpClient = Getftpclient (Ftphost, FtpUserName, FTPPassword, FtpPort);
  73. Ftpclient.setcontrolencoding ("UTF-8"); //Chinese support
  74. Ftpclient.setfiletype (Ftpclient.binary_file_type);
  75. Ftpclient.enterlocalpassivemode ();
  76. Ftpclient.changeworkingdirectory (Ftppath);
  77. File LocalFile = new File (LocalPath + File.separatorchar + fileName);
  78. OutputStream OS = new FileOutputStream (LocalFile);
  79. Ftpclient.retrievefile (fileName, OS);
  80. Os.close ();
  81. Ftpclient.logout ();
  82. } catch (FileNotFoundException e) {
  83. Logger.error ("not found" + Ftppath + "file");
  84. E.printstacktrace ();
  85. } catch (SocketException e) {
  86. Logger.error ("Connection to FTP failed.");
  87. E.printstacktrace ();
  88. } catch (IOException e) {
  89. E.printstacktrace ();
  90. Logger.error ("file read error.  ");
  91. E.printstacktrace ();
  92. }
  93. }
  94. }

Third, the test code

Suppose there is already an FTP server, login username and password for ZYF, Port 22, log into the FTPDir directory under the home directory, there is a 11.txt file for download test

  1. Package scripts.testscript;
  2. Import Org.testng.annotations.Test;
  3. Import common. Ftputil;
  4. Public class Ftpdownloadtestscript {
  5. @Test
  6. public Void Run () {
  7. String ftphost = "192.168.26.132";
  8. String ftpusername = "ZYF";
  9. String FTPPassword = "ZYF";
  10. int ftpport = 21;
  11. String Ftppath = "ftpdir/";
  12. String LocalPath = "H:/download";
  13. String fileName = "11.txt";
  14. Ftputil.downloadftpfile (Ftphost, FtpUserName, FTPPassword, FtpPort, Ftppath, LocalPath, fileName);
  15. }
  16. }

Java code implementation FTP single file download (ii)

Related Article

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.