Reference: http://www.informit.com/guides/content.asp? g = java & seqnum = 40
Although Jakarta Apache has released a large number of open source projects, the most popular, the most widely used Ibramnaphysics Tomcat Servlet Engine. If you have implemented these Internet protocols similar to FTP, Telnet, SMTP, POP3 and NNTP, you will know how difficult it is to check the errors in these protocols. But now you don't have to worry about this problem, the Commons / Nets library launched by Apache will be separated from the specific implementation of these agreements, and we don't need to rack your brains to achieve a certain agreement. The Commons / Nets library includes several protocols:
Finger
WHOIS
TFTP
Telnet
POP3
FTP
NNTP
SMTP
There are other protocols like TIME and ECHO.
The goal of the Commons / Net library is to provide programmers with a general, cross-platform and easy-to-use Internet protocol. If you are very familiar with these agreements, then your knowledge will be very helpful to understand a series of APIs in the Commons / Net library. If you don't understand these protocols, it doesn't matter, these APIs will help you with the agreement. To understanding.
The following describes how to use the API in the Commons / Net library to implement the features we have. First introduce the FTP (File Transfer Protocol) protocol
File Transfer Protocol
In a distributed system, it is a very common task from one machine to another machine. We often use the FTP tool to connect to a remote server, find the desired file and download it to the local machine. A 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 - r - 1 501 100 3147032 Mar 31 13:37 myfile.pdf
226 Transfer Complete.
FTP: 101 BYtes Received in 0.58seconds 0.17kBytes / sec.
FTP> LCD C: / Download
FTP> bin
FTP> get myfile.pdf
Greatly experienced the following steps:
1. connect to the server
2. Enter your username, password login
3. Change access folder "/ apps / mydir" (CD)
4. Get a list of files
5. Change the local folder path
6. Set the transfer mode is binary
7. Download myfile.pdf file
Each step in the above requires the interaction of the FTP client and the FTP server side, each step needs to be checked.
Below is a code using the commons / net library, using the FTP protocol from the server to download files
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 (); // ftpclient object Has a variety of methods of 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 { Date filedate = files [i] .getTimeStamp (). Gettime (); IF (FileDate.comPareto (Start.getTime ())> = 0 && FileDate.comPareto (End.getTime ()) <= 0) // Compare the file creation time compliance with 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.seParetor 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, you can download the Commons / Net library first at http://jakarta.apache.org/commons/net/index.html, then put the system's classpath point to the downloaded common-net-1.0.0jar file. In a getDataFiles () method, first connect to a preset username and password to connect an FTP server, then change the path to a specific directory, download the file to a specific local directory. All of these operations are completed by two Commons / Net classes: l ftpclient: Used to connect, log in to the server, navigation directory structure, get a list of files, upload, download files, exit, close the connection. l FTPFILE: Used to get file properties. FTPClient provides the following method for interaction with the server: l Connect (String Server) Connection Server l Disconnect () Close connection l login (String Username, String Password) Log in to the FTP server l logout () exits the FTP server l ChangeWorkingDirectiory (String Pathname) Change the work directory currently on the FTP server l ftpfile [] Listfiles () Get list of files in the current FTP server working directory l String [] ListNames () Get the name of the file in the current FTP server working directory 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 server's current work directory l Retrievefile (Java.lang.String Remotename, OutputStream Local) Download files in the FTP server working directory to the local The FTPFile class provides the following features: l string getName () get the file name l String getGroup () Get the group to which this file belongs l String getuser () get the file owner name l Long getsize () get the size of the file l int GETTYPE Getting File Type: 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 () If it is a Symbolic link, return true l Calendar getTimeStamp gets the specific time of the last modification of the file In the functions mentioned above, only a few lines of code have completed a large number of tasks. Therefore, it is obvious that the use of the Commons / Net library allows developers to get out of the cumbersome agreement, and several energy details are developed in the development of specific business details. Next, a Telnet and other protocols will be described.