<?xml:namespace prefix = o ns = "Urn:schemas-microsoft-com:office:office"/>
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 and widely used is probably the Tomcat Servlet engine. If you've ever implemented an Internet protocol like FTP,TELNET,SMTP,POP3 and NNTP, you know how difficult it is to check the implementation of these protocols. But now you don't have to worry about that, and the Apache Commons/nets Library has freed us from the specifics of these agreements, and we don't have to rack our brains to get a deal out of the night. The Commons/nets library includes the following 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 common, cross-platform, and easy-to-use Internet Protocol family. If you are familiar with these protocols, your knowledge will greatly help you understand the range of APIs in the Commons/net library, and if you are not very knowledgeable about these protocols, it will help you understand the protocol.
Here are how to use the APIs in the Commons/net library to implement the functionality we want. First, the FTP (File Transfer Protocol) protocol is introduced
File Transfer Protocol
In a distributed system, transferring files from one machine to another is a very common task. We often use the FTP tool to connect to a remote server, locate the desired file and download it to the local machine. An FTP session procedure is shown below
c:/> FTP 192.168.1.99
Connected to 192.168.1.99.
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
PORT command successful.
Opening ASCII mode data connection for directory listing.
Total 6160
-rw-r--r--1 501 3147032 13:37 myfile.pdf
Transfer complete.
ftp:101 bytes received in 0.58Seconds 0.17kbytes/sec.
ftp> LCD C:/download
Ftp> bin
Ftp> Get Myfile.pdf
The following steps are roughly experienced:
1. Connecting to a server
2. Enter user name, password login
3. Change Access Folder "/apps/mydir" (CD)
4. Get file List
5. Change local Folder path
6. Set transfer mode to binary
7. Download Myfile.pdf file
Each of these steps requires interaction between the FTP client and the FTP server, each of which requires error checking.
Here is a section of code that uses the Commons/net library to download files from the server 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 ();//ftpclient objects have various methods of 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 file creation time to meet 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 ();
}
}
To enable the above code to compile smoothly, first download the Commons/net library in http://jakarta.apache.org/commons/net/index.html, Then point the system's classpath to the downloaded Common-net-1.0.0jar file.
In the Getdatafiles () method, the program first connects an FTP server with a preset username and password, and then changes the path to a specific directory and downloads the file to a specific local directory. All of these operations are done through two commons/net classes:
L FtpClient: Used to connect, login server, navigation directory structure, get file list, upload, download file, exit, close connection.
L Ftpfile: Used to get file attributes.
FtpClient provides the following methods for interacting with the server:
L Connect (String server) connection server
L Disconnect () Close connection
L Login (String username,string password) logon to FTP server
L Logout () exit FTP server
L changeworkingdirectiory (String pathname) changes the working directory currently on the FTP server
L ftpfile[] Listfiles () Gets the list of files in the working directory of the current FTP server
L string[] ListNames () Gets the name of the file in the working directory of the current FTP server
L Rename (string from, string to) renaming the file name in the working directory of the FTP server
L StoreFile (String Remotename,inputstream Local), uploading native files to the server's current working directory
L Retrievefile (java.lang.String remotename,outputstream local) downloads files from the FTP server's working directory to native
The Ftpfile class provides the following features:
L String getName () Get filename
L String Getgroup () Gets the group that this file belongs to
L String getuser () get file owner name
L Long GetSize () to get the size of the file
L Int GetType Get the type of file: 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