![]() |
|
How would you like to add a functionality to send files to a remote server to your Java application?
The 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
| ||
openServer()).
login()).
cd()).
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>