Use FtpClient to upload downloads in Java

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/hbcui1984/article/details/2720204

In Java programs, often need to deal with FTP, such as uploading files to the FTP server, download files, this article briefly describes how to use the Jakarta Commons in the FtpClient (in the commons-net package) implementation of the upload download file.

First, upload files

The principle is not introduced, we directly look at the code bar

[Java]View PlainCopy
  1. /**
  2. * Description: Uploading files to FTP server
  3. * @Version1.0 4:31:09 PM by Tri Hongbao ([email protected]) create
  4. * @param URL FTP server hostname
  5. * @param Port FTP Server
  6. * @param username FTP login Account
  7. * @param password FTP login password
  8. * Save Directory @param path FTP server
  9. * @param filename uploaded to the FTP server
  10. * @param input stream
  11. * @return returns TRUE, otherwise false
  12. */
  13. Public Static boolean uploadfile (string url,int port,string username, string password, string path, String fil ename, InputStream input) {
  14. Boolean success = false;
  15. ftpclient ftp = new FtpClient ();
  16. try {
  17. int reply;
  18. Ftp.connect (URL, port); //Connect FTP server
  19. //If using the default port, you can connect directly to the FTP server using Ftp.connect (URL)
  20. Ftp.login (username, password); //Login
  21. Reply = Ftp.getreplycode ();
  22. if (! Ftpreply.ispositivecompletion (Reply)) {
  23. Ftp.disconnect ();
  24. return success;
  25. }
  26. Ftp.changeworkingdirectory (path);
  27. Ftp.storefile (filename, input);
  28. Input.close ();
  29. Ftp.logout ();
  30. Success = true;
  31. } catch (IOException e) {
  32. E.printstacktrace ();
  33. } finally {
  34. if (ftp.isconnected ()) {
  35. try {
  36. Ftp.disconnect ();
  37. } catch (IOException IoE) {
  38. }
  39. }
  40. }
  41. return success;
  42. }<pre></pre>

Below we write two small examples:

1. Upload the local file to the FTP server with the following code:

[Java]View PlainCopy
  1. @Test
  2. Public void Testuploadfromdisk () {
  3. try {
  4. FileInputStream in=New FileInputStream (new File ("D:/test.txt"));
  5. Boolean flag = UploadFile ("127.0.0.1", + , "test", "test", "D:/ftp", "Test.txt", in);
  6. SYSTEM.OUT.PRINTLN (flag);
  7. } catch (FileNotFoundException e) {
  8. E.printstacktrace ();
  9. }
  10. }<pre></pre>

2. Generate a file on the FTP server and write a string to the file

[Java]View PlainCopy
  1. @Test
  2. Public void Testuploadfromstring () {
  3. try {
  4. InputStream input = new Bytearrayinputstream ("Test ftp". GetBytes ("Utf-8"));
  5. Boolean flag = UploadFile ("127.0.0.1", + , "test", "test", "D:/ftp", "test.txt", input);
  6. SYSTEM.OUT.PRINTLN (flag);
  7. } catch (Unsupportedencodingexception e) {
  8. E.printstacktrace ();
  9. }
  10. }<pre></pre>


Second, download the file

The code to download the file from the FTP server is also very simple, as follows:

[Java]View PlainCopy
  1. /**
  2. * Description: Download files from FTP server
  3. * @Version1.0 5:32:36 PM by Tri Hongbao ([email protected]) create
  4. * @param URL FTP server hostname
  5. * @param Port FTP Server
  6. * @param username FTP login Account
  7. * @param password FTP login password
  8. * @param the relative path on the RemotePath FTP server
  9. * @param filename to download
  10. * @param localPath saved to local path after download
  11. * @return
  12. */
  13. Public Static boolean downfile (string url, int port,string username, string password, string Remotepath,strin G filename,string LocalPath) {
  14. Boolean success = false;
  15. ftpclient ftp = new FtpClient ();
  16. try {
  17. int reply;
  18. Ftp.connect (URL, port);
  19. //If using the default port, you can connect directly to the FTP server using Ftp.connect (URL)
  20. Ftp.login (username, password); //Login
  21. Reply = Ftp.getreplycode ();
  22. if (! Ftpreply.ispositivecompletion (Reply)) {
  23. Ftp.disconnect ();
  24. return success;
  25. }
  26. Ftp.changeworkingdirectory (RemotePath); //Transfer to FTP server directory
  27. ftpfile[] fs = Ftp.listfiles ();
  28. For (Ftpfile ff:fs) {
  29. if (Ff.getname (). Equals (FileName)) {
  30. File LocalFile = new file (localpath+"/" +ff.getname ());
  31. OutputStream is = new FileOutputStream (LocalFile);
  32. Ftp.retrievefile (Ff.getname (), is);
  33. Is.close ();
  34. }
  35. }
  36. Ftp.logout ();
  37. Success = true;
  38. } catch (IOException e) {
  39. E.printstacktrace ();
  40. } finally {
  41. if (ftp.isconnected ()) {
  42. try {
  43. Ftp.disconnect ();
  44. } catch (IOException IoE) {
  45. }
  46. }
  47. }
  48. return success;
  49. }<pre></pre>

Use FtpClient to upload downloads in Java

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.