This article describes how to use Java's existing available libraries to write FTP client code in Java, and develop applet controls to upload and download controls based on the web for bulk, large files. On the basis of comparing a series of FTP client libraries, this paper discusses some common functions such as progress bar, continuous transmission of breakpoint, mapping of internal and external network, callback JavaScript function in applet and so on, which is a more general and powerful J-ftp class library. Hope that through this article to play a role in the.
First, a primer
In the process of implementing a project, a web-based file upload and download requirement is presented. Users across the province (or across the country) need to upload some files to a file server in a center. These documents are used in a number of large-scale construction projects, may involve tens of millions of millions of construction projects. The file has three distinct characteristics: first, the file is large, may reach 50M, the second is the number of files, there are probably about 15; third, data security requires digital signature and data encryption.
The first consideration is the HTTP based transmission mode. But the author through relatively quickly found to meet the above requirements:
1: Upload with HTTP protocol, seems to be more suitable for web programming convenience, upload less than 1M file speed than with FTP protocol upload file slightly faster. But for the bulk and large file transmission may be powerless. Of course, it also has its advantages, if not like FTP, you must start an FTP service on the server side.
2: Upload files with FTP protocol larger than 1M files faster than HTTP. The larger the file, the faster the upload is than the HTTP upload speed several times. and write programs in Java, FTP is more convenient than HTTP.
I have used VB has also written ActiveX control to carry out batch file upload download, its function is also very powerful. Just because there is no special digital signature of the CAB file or OCX, it is necessary to set up a cumbersome client, such as setting up a secure site, reducing the security level of the client, and so on, thus giving up some schemes.
At the same time, taking into account the need for digital signature and data encryption on the client, decided to use the applet way to achieve. Before the file is uploaded, the client can obtain the local Usbkey key information and complete the encryption and signature processing of the uploaded file. Although the use of applet requirements for the client to install the JRE Runtime environment, the management and use of the client to bring a one-time inconvenience, but relative to such a large number of files and file security, this may be a relatively small price.
To sum up, the operating environment is:
FTP Server side: Serv-u, professional FTP server-side program, the internet has off-the-shelf software downloads, of course, the reader may also write a server-side FTP file receiving program to explain. If there is no special requirements or features, serv-u should be able to meet the requirements of our general upload download;
Client: Java applet, that year let Java fire a so-called with Microsoft's ActiveX analogy technology of course, now Java out of the JavaFX, is not a substitute for applets.
Application environment: Internet network, the ultimate goal.
Second, the Java FTP client library selection
Let's imagine a situation where we want to write a pure Java application that uploads downloaded files from an FTP server running on a remote computer, and we also want to be able to get the basic file information, such as filename, data, or file size, for the remote files that are downloaded.
Although it is possible, and perhaps interesting, to write an FTP protocol handler from scratch, the task is difficult, lengthy, and potentially dangerous. Because we don't want to spend time, energy, or money to write a handler like this, we switch to a reusable component that already exists. And a lot of inventory is online.
Finding an excellent Java FTP client library that suits our needs is not as simple as it looks. On the contrary, it is a very painful and complicated task. First it takes some time to find an FTP client library, and secondly, after we find all of the existing libraries, which one should we choose? Each library is suitable for different needs. These libraries are not equivalent in performance, and they are fundamentally different in design. Each class library has its own features and uses different terms to describe them. Therefore, it is difficult to evaluate and compare the FTP client library.
The use of reusable components is a worthwhile approach, but in this case it is often daunting at first. Later, perhaps a little ashamed: After choosing a good FTP library, then the work is very simple, according to the simple rules to do. At present, there are a lot of open free FTP client class library, such as SIMPLEFTP, J-FTP, and many other ftpclient. As shown in the following table, the table is not fully listed, as readers have a better client FTP class library, please add further.
FTP Client class Library name |
Note |
J-ftp |
J-ftp |
Simpleftp |
HTTP://www.jibble.org/files/simpleftp.jar |
FtpClient |
Com.enterprisedt.net.ftp.FTPClient |
Ftpprotocol |
Com.ibm.network.ftp.protocol.FTPProtocol |
Ftpconnection |
Net.sf.jftp.net.FtpConnection |
FtpClient |
Org.apache.commons.net.ftp.FTPClient |
FtpClient |
Jshop.jnet.FTPClient |
FtpClient |
Sun.net.ftp.FtpClient |
Ftp |
Com.cqs.ftp.FTP |
Ftp |
Cz.dhl.ftp.Ftp |
FtpClient |
Org.globus.io.ftp.FTPClient |
In this paper, the author adopts the j-ftp. This is an open source and powerful client FTP class library. I like it very much, but also recommend it to you readers. Forget to do a free advertisement for it.
three, basic function
1, landing
The
uses FTP for file transfer, in fact, the use of Java.net.socket to communicate. The following code is just one of the login methods for the class net.sf.jftp.net.FtpConnection. Of course in the following code, in order to save the layout, as well as some of the principles elaborated, the author will be some unnecessary code removed, such as the log code. Complete code refer to J-ftp source code or the author of the sample source, followed by the same code example:
public int Login (string username, string password) {this.username = username;
This.password = password;
int status = LOGIN_OK;
Jcon = new Jconnection (host, Port);
if (Jcon.isthere ()) {in = Jcon.getreader ();
if (getline (POSITIVE) = = null)//ftp220_service_ready) = = null) {OK = false;
status = OFFLINE;
} if (!getline (loginack). StartsWith (POSITIVE))//ftp230_logged_in)) {
if (Success (POSITIVE))//ftp230_logged_in)) {} else
{OK = false;
status = Wrong_login_data; }} else {if (msg) {log.debug ("FTP not Avai
Lable! ");
OK = false; Status = Generic_failed;
} if (OK) {connected = true;
System ();
Binary ();
string[] advsettings = new String[6];
if (Getostype (). IndexOf ("OS/2") >= 0) {List_default = "LIST"; } if (List.equals ("Default")) {//just Get the ' the ' ' The ' the ' the ' the ' "
is the//ftp List command) advsettings = Loadset.loadset (settings.adv_settings);
If FILE not FOUND, CREATE it and SET it to list_default if (advsettings = null)
{LIST = List_default;
Saveset s = new Saveset (settings.adv_settings, LIST);
else {LIST = advsettings[0]; if (list = = null) {list = List_DEFAULT; }} if (Getostype (). IndexOf ("MVS") >= 0) {LIST =
"LIST";
}//*** Firedirectoryupdate (this);
Fireconnectioninitialized (this);
else {fireconnectionfailed (this, new Integer (status). ToString ());
} return status; In this login method, there is a jconnection class, this class is responsible for establishing socket sockets, and this class is a separate thread, the advantage is to match the interface changes, and the network socket connection work as a separate thread to deal with, conducive to the friendly interface.
The following is the run method for the Net.sf.jftp.net.JConnection class, and of course, the start of this thread is initiated in the construction method of the Jconnection class.
public void Run () {try {s = new Socket (host, Port);
LocalPort = S.getlocalport ();
if (Time > 0) s.setsotimeout (time);
out = new PrintStream (S.getoutputstream (), New Bufferedoutputstream (),
Settings.buffersize)); in = new BufferedReader (New InputStreamReader (S.getinputstream ()), settings.buffersize);
IsOk = true;
} catch (Exception ex) {ex.printstacktrace ();
Log.out ("Warning:connection Closed due to exception (" + Host + ":" + Port + ")");
IsOk = false; try {if ((S!= null) &&!s.isclosed ()) {S.close ()
;
} if (out!= null) {out.close ();
} if (in!= null) {in.close ();
} catch (Exception ex2) {ex2.printstacktrace ();
Log.out ("Warning:got more errors trying to close socket and streams");
} established = true; }
The socket in this run method illustrates that this class implements a client socket (or "socket"), which is the communication endpoint between two machines. The actual work of the socket is performed by an instance of the SocketImpl class. The application can configure itself to create sockets for the local firewall by changing the socket factory that creates the socket implementation. Please refer to JDK5 's API description for specific instructions, preferably in Chinese. Oh.