free web hosting | free hosting | Web Hosting | Free Website Submission | shopping cart | Promoter Online | php hosting
affordable web hosting Pets web page hosting web hosting website hosting web hosting service web hosting web host
Sending files via FTP -- the FtpClient class

Sending files via FTP -- the FtpClient class

How would you like to add a functionality to send files to a remote server to your Java application?

The FtpClient does just that. This is an FTP client class that is equipped with methods to open a connection to the remote server, login to an account, issue commands, and so on.

(→ Class FtpClient)

Here I would like to show you the way to use the class by the code that downloads a file from the remote server.

There are a few things that can complicate the task in using this FtpClient class, so let me point out that first before going on to explain how the process of sending a file should go.

That is, the FTP connection to the server, is not through one port, but it needs another port to send/receive data. The control connection that sends just commands, is not bidirectional. To send a file, one more connection must be established for data transaction.

The overall process of opening the connection, goes as follows. The first step is to specify the remote server name and the port number, which is 21 for FTP. Then the user name and the password must be sent to the remote server. Go to the directory where the file is, and then start downloading. This is the tricky part. Here a data connection must be established.

Opening up a control connection is fairly straight foward. So how do you open a data connection? The answer is, to use the Socket class. The openDataConnection() method returns a Socket class that is going to send and receive data from the remote server. In the code shown below the data read from the InputStream is buffered and stored in FileOutputStream.

     
  1. Open a control connection (openServer()).
  2. Login to an account (login()).
  3. Go to the directory where the file is (cd()).
  4. Download the file.
    1. Issue "RETR filename" command and open a data connection to the remote server.
    2. Read data and write them to a file.
    3. Close the data connection.
  5. Close the control connection (closeServer()).

The above process is illustrated in the code below, which is an FTP client class. In the downloadFile() method, a control connection is made, then a data connection with openDataConnection() method. The data is written to the file as it reads the data from the remote server. The buffer size is 1MB.

I found the class quite handy, and would like to introduce this class for those who want to send files via FTP from Java applications. Incorporating the class into your application is just as fun and easy as the usual Java classes. Namely, instantiate the class, and then call the methods as necessary.

For those who would like to point out or comment on the article, please send an e-mail to E.Asai<easai@acm.org>


Return to Home Page
Erica Asai
Last Modified: Wed Dec 20 15:58:42 2006