The subject of how to use FTP in Java is often discussed, because it seems like a piece of functionality that is conspicuously missing from the regular Sun Java distributions. Here I'll discuss four of the options you have for accessing FTP sites in a Java program.
Option 1: Use a URL string
The simplest way to access an FTP site is to use a URL string. You may be able to do something like this:
URL url = new URL("ftp://username:password@ftp.whatever.com/file.zip;type=i"); URLConnection con = url.openConnection(); BufferedInputStream in = new BufferedInputStream(con.getInputStream()); FileOutputStream out = new FileOutputStream("C:\\file.zip"); int i = 0; byte[] bytesIn = new byte[1024]; while ((i = in.read(bytesIn)) >= 0) { out.write(bytesIn, 0, i); } out.close(); in.close();
This method can work if:
However, if you need additional functionality, or if that simply doesn't work (either because your network/firewall doesn't allow the connection, or because the remote FTP site doesn't support that type of operation), you'll need to try one of the other methods below.
Option 2: Write your own FTP class
This is a modification of an FTP class I found here (the link is no longer active, but you can look it up on archive.org if you're curious). Based on the code comments ("this class can be freely distributed and modified as long as Bret Taylor is given credit in the source code comments"), it looked like I could repost it to this site with modifications. So I did.
I've also included a small java program/class called TestFTP that demonstrates the use of some of the calls that the FTPConnection class makes.
FTPConnection.java
TestFTP.java
While this class will work most of the time, it does have some limitations. For example:
Because of this, the class is probably a better programming reference than anything else. It's a good example of what's involved in the background when you access an FTP site and try to perform operations, but the lack of features described above may be a deterrant. The biggest problem can end up being that passive mode isn't supported, since that is often a requirement in corporate firewall situations.
Option 3: Use the sun.net.ftp class
Sun java includes an internal ftp class called sun.net.ftp.FtpClient. Despite the fact that this class is officially undocumented and unsupported, it appears to be widely used. At the time of this writing, there are some seemingly unofficial JavaDocs for the Sun FtpClient class, along with some of the related sun.net.* classes at:
http://swig.stanford.edu/pub/java/javadoc/sun/net/ftp/FtpClient.html
If you want to see an example of how to actually use the class, here are a couple bits of code for you:
a wrapper around the Sun FtpClient class: SunFtpWrapper.java
a program that uses the wrapper: FtpWrapperTest.java
In many cases, this will be sufficient for your use. Here are some of the downsides:
While the "unsupported" part doesn't seem to bother most people, listing files and directories is something you might want to do. If you compile and run the FtpWrapperTest file above, you'll see that the results of the LIST command are pretty confusing. Not only is each file/directory name embedded in a hard-to-parse string containing permission flags, file size, and last modified date, but the format of the file information string can differ from server to server. The format of the results of a LIST command on a Unix FTP server versus a Windows FTP server are completely different. The format can even vary between different FTP server implementations on the same operating system.
So, as a general rule of thumb, you are probably safe using the sun.net.ftp.FtpClient class to access an FTP server as long as you don't need to list files or directories on a remote server.
Option 4: Use the Jakarta Net classes
Probably the most attractive option for FTP functionality in Java is the Jakarta Commons Net library, which includes a full-featured FTP client class. The library is pretty well tested, and is also open-source and free to use (as long as you abide by the terms of the Apache license). This library is actually the old ORO NetComponents.jar package, which was donated to Apache several years ago.
Here are some examples of using the Jakarta Commons FTP class:
a wrapper around the Jakarta FTP class: JakartaFtpWrapper.java
a program that uses the wrapper: JakartaWrapperTest.java
Not only is Jakarta FTP much more feature-rich than the other options above, but it also has its own internal support for parsing file/directory listings on multiple server types. To do this it uses the Jakarta ORO library (also open-source and covered by the Apache license) for regular expression parsing, although this is all done transparent to you.
For a good, general purpose FTP client implementation in Java, Jakarta Commons is a good bet.
Other useful information