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.2-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.2 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.2.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.2.jar: Compiled example programs are packed inside this JAR file. You can use this syntax:

      java –jar commons-net-examples-3.2.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.2.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:

Submit to DeliciousSubmit to DiggSubmit to FacebookSubmit to Google BookmarksSubmit to StumbleuponSubmit to TechnoratiSubmit to TwitterSubmit to LinkedIn
avatar
is there need to change a IIS server to apache server in MS-XP for using this library????
VOTES:0
avatar
Do you mean that you are running FTP on ISS?
The Apache Commons Net library works with any server which supports FTP protocol, regardless of server type (IIS or Apache or something else).
VOTES:0
avatar
Hi ,

I have downloaded the text file using FTP, but here the downloaded file is not readable format. I have read the documenatation and this is what it tells:

* &lt;p /&gt;
* &lt;code&gt; FTP.NON_PRINT_TEXT_FORMAT &lt;/code&gt;,
* &lt;code&gt; FTP.STREAM_TRANSFER_MODE &lt;/code&gt;, and
* &lt;code&gt; FTP.FILE_STRUCTURE &lt;/code&gt; are the only supported formats,
* transfer modes, and file structures.

So i tried to override it, but it was not successfull. Can any one suggest how i can download the text file in readable format.
VOTES:0
avatar
Hai Guyz im familiar with this FTP concept...If u hav doubt u can Contact me
ID:sheerinfa@gmail.com
No:9095990326

Cheers
Sheerin
VOTES:0
avatar
Hi Nara,

You should always set transfer mode to binary stream. See this tutorial:

[Java FTP file download tutorial and example][1]


[1]: www.codejava.net/java-se/networking/ftp/...tutorial-and-example
VOTES:0
avatar
Hi Nam,

I have used the same program to download the file from the Windows server, but the downloaded file is not in readable format. I tried various options, but all resulted in the same.
VOTES:0
avatar
Can you get the file downloaded correctly using the sample program provided in the given link above?

Make sure you call this statement:

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
VOTES:0
avatar
Hi Nam,

My program is using the method you have mentioned.
VOTES:0
avatar
I haven't tested with a Windows FTP server yet. Could you provide the login details so I can test?
VOTES:0