Reference: http://www.informit.com/guides/content.asp? G = Java & seqnum = 40
Although the Jakarta Apache organization has released a large number of open source projects, I am afraid Tomcat Servlet Engine is the most popular and widely used project. If you have implemented internet protocols like FTP, telnet, SMTP, POP3, and NNTP, you will know how difficult it is to check the specific implementation errors of these protocols. But now you don't have to worry about this issue. The Commons/nets Library launched by the Apache organization disconnects us from the specific implementation of these protocols, we don't have to worry about implementing a certain agreement for a whole night. The commons/nets library includes the following protocols:
Finger
Whois
TFTP
Telnet
POP3
FTP
NNTP
SMTP
There are other protocols such as time and echo.
The goal of the commons/net Library is to provide programmers with a universal, cross-platform and easy-to-use Internet protocol family. If you are very familiar with these protocols, your knowledge will help you understand a series of APIs in the Commons/net Library. If you are not familiar with these protocols, it doesn't matter, these APIs help you understand the protocol.
The following describes how to use APIs in the Commons/net Library to implement the functions we need. First introduce the FTP (file transfer protocol) Protocol
File Transfer Protocol
In a distributed system, transferring files from one machine to another is a very common task. We often use ftp to connect to a remote server, find the desired file, and download it to a local machine. The FTP session process is as follows:
C: \> FTP 192.168.1.99
Connected to 192.168.1.99.
220 wgb01 FTP Server (version wu-2.6.2-5) ready.
User (192.168.1.99 :( none): myusername
331 Password required for myusername.
Password:
230 user myusername logged in.
Ftp> Cd/apps/mydir
Ftp> ls
200 PORT command successful.
150 opening ASCII mode data connection for directory listing.
Total 6160
-RW-r -- 1 501 100 3147032 Mar 31 myfileprohibited
226 transfer complete.
FTP: 101 bytes encoded ed in 0.58 seconds 0.17 Kbytes/sec.
Ftp> LCD c: \ Download
Ftp> Bin
Ftp> Get myfileprohibited
We have gone through the following steps:
1. Connect to the server
2. Enter the user name and password to log on.
3. Change the access folder "/apps/mydir" (cd)
4. Obtain the file list
5. Change the local folder path
6. Set the transmission mode to binary.
7. Download The myfilepath File
In each of the above steps, the FTP client and the FTP server must interact with each other, and an error check is required for each step.
Below is a piece of code for downloading files from the server using the commons/net library using the FTP protocol
Import org.apache.commons.net. FTP .*;
Public static void getdatafiles (string server, string username, string password,
String folder, string destinationfolder, calendar start, calendar end)
{
Try
{
// Connect and logon to FTP Server
Ftpclient FTP = new ftpclient (); // various methods for the ftpclient object to own the client
FTP. Connect (server );
FTP. login (username, password );
System. Out. println ("connected to" + SERVER + ".");
System. Out. Print (ftp. getreplystring ());
// List the files in the directory
FTP. changeworkingdirectory (folder );
Ftpfile [] files = ftp. listfiles ();
System. Out. println ("number of files in Dir:" + files. Length );
Dateformat df = dateformat. getdateinstance (dateformat. Short );
For (INT I = 0; I <files. length; I ++)
{
Date filedate = files [I]. gettimestamp (). gettime ();
If (filedate. compareto (start. gettime ()> = 0 &&
Filedate. compareto (end. gettime () <= 0) // compare whether the file creation time meets the requirements
{
// Download a file from the FTP server
System. Out. Print (DF. Format (files [I]. gettimestamp (). gettime ()));
System. Out. println ("\ t" + files [I]. getname ());
File file = new file (destinationfolder + file. Separator + files [I]. getname ());
Fileoutputstream Fos = new fileoutputstream (File );
FTP. retrievefile (files [I]. getname (), FOS );
FOS. Close ();
File. setlastmodified (filedate. gettime ());
}
}
// Logout from the FTP server and disconnect
FTP. logout ();
FTP. Disconnect ();
}
Catch (exception E)
{
E. printstacktrace ();
}
}
In order to make the above Code can be compiled and executed smoothly.
In the getdatafiles () method, the program uses the preset username and password to connect to an FTP server, then changes the path to a specific directory, and downloads the file to a specific local directory. These operations are completed using two commons/NET classes:
L ftpclient: used to connect to and log on to the server, navigate to the directory structure, obtain the file list, upload and download files, exit, and close the connection.
L ftpfile: used to obtain file attributes.
Ftpclient provides the following methods for interaction with the server:
L connect (string server) to the server
L disconnect () Close the connection
L login (string username, string password) log on to the FTP server
L logout () exits the FTP server
L changeworkingdireory Ory (string pathname) changes the current working directory on the FTP server
L ftpfile [] listfiles () obtain the file list in the working directory of the current FTP Server
L string [] listnames () obtain the name of the file in the working directory of the current FTP Server
L Rename (string from, string to), rename the file name in the FTP server working directory
L storefile (string remotename, inputstream local), upload local files to the current working directory of the server
L retrievefile (Java. Lang. String remotename, outputstream local) download files from the FTP server working directory to the local
The ftpfile class provides the following functions:
L string getname () Get the file name
L string getgroup () obtains the group to which the object belongs.
L string getuser () Get the file owner name
L long getsize () to get the file size
L int GetType: directory_type, file_type, symbolic_link_type, unknown_type
L Boolean isdirectory () is a folder
L Boolean isfile () is a file (not a folder)
L Boolean issymboliclink () returns true if it is a symbolic link.
L calendar gettimestamp: Obtain the last modification time of the file.
In the functions mentioned above, a large number of tasks are completed with just a few lines of code. Therefore, it is obvious that the use of the commons/net Library can allow developers to get out of tedious protocol implementation and focus on the development of specific business details.
Next we will introduce Telnet and other protocols.