Although it is possible to use Java networking API (by using the interfaces and classes available in the packages java.net and javax.net) to write code that communicates with a FTP server, that approach is discouraged because you will have to spend a lot of time on understanding the underlying FTP protocol, implementing the protocol handlers, testing, fixing bugs… and finally you re-invent the wheel! Instead, it’s advisable to look around and pick up some ready-made libraries, and that definitely saves your time! The Apache Commons Net library is an ideal choice for developing FTP based applications. It’s not just for only FTP, but for all common standard internet protocols such as NNTP, SMTP, POP3, Telnet… to name just a few.

With built-in Java API, we can use the java.net.URLConnection to do some FTP operations such as listing files and directories, upload and download. However that is very limited in terms of controllability and flexibility.

So, if you are going to write a Java application that needs to communicate with a FTP server, go on with Apache Commons Net.

Download a zipped binary distribution of the library (commons-net-3.6-bin.zip) and extract the zip file to a desired location on your computer. The following screenshot shows content of the distribution:

Apache Commons Net distribution directory structure

You can find the following items included in the distribution:

    • Directory apidocs: contains Java doc for Commons Net 3.6 API. You should consult this documentation for API reference.
    • Directory examples: contains Java source code for examples. The examples are categorized by protocol (ftp, mail, telnet…) and designed as standalone programs, so that you can run and experiment the examples yourself.
    • File commons-net-3.6.jar: this is the JAR library to which your application must refer, in order to use APIs related to FTP protocol.
    • File commons-net-examples-3.6.jar: Compiled example programs are packed inside this JAR file. You can use this syntax:

      java –jar commons-net-examples-3.6.jar <main-class> <arguments>

      to execute the example programs. Consult example source code to choose a correct main class and arguments needed.
So to compile and run your code, you must have commons-net-3.6.jar file present in your project’s classpath.

The package org.apache.commons.net.ftp contains interfaces and classes for working with FTP protocol. The FTPClient class implements necessary functions for developing a FTP client program.

And that’s only the first step. Check out our tutorials dedicated to Java FTP programming with Apache Commons Net library:

 



 

 

  • FTP File Download:
 

 


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#16Arief Yudhawarman2021-02-18 19:11
Is there any source code to resume download or upload file using Apache Commons Net library ?
Quote
#15Ewa2020-05-06 09:12
Thank you Nam for your great tutorial. Very good job! @>-}---
Quote
#14Nam2015-02-25 08:38
Hi Serj,

It's simple, that file doesn't exist (FileNotFoundException).
Quote
#13Serj2015-02-25 05:56
i have exception when trying copy firefox profile
java.io.FileNotFoundException: D:\temp\Roaming\Mozilla\Firefox\Profiles\ojt048j5.default\addons.json (Системе не удается найти указанный путь)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.(Unknown Source)
at java.io.FileOutputStream.(Unknown Source)
at helpClasses.FTPUtil.downloadSingleFile(FTPUtil.java:107)
at helpClasses.FTPUtil.downloadDirectory(FTPUtil.java:76)
at helpClasses.FTPDownloadDirectoryTest.main(FTPDownloadDirectoryTest.java:30)
Quote
#12Iann2015-01-27 15:00
Hi Mohit
Usually the method is to add "something else" at the server end to notify the transfer is complete. Most sites I know create an empty file on the server once the transfer is complete, and the receiver simply waits for that file as a notification.It is also good because it is hard for the server to know if the transfer was successful and complete.
Quote