Most JSP file uploads use open-source projects to simplify processing. Here we list the implementation of the two commonly used jar packages and compare them to illustrate their advantages and disadvantages and the issues that should be paid attention.
Commons fileupload can be downloaded at http://jakarta.apache.org/commons/fileupload/. this package supports commons Io and can be downloaded at http://jakarta.apache.org/commons/io/
Com. oreilly. servlet, which can be downloaded at http://www.servlets.com/cos/
Commons fileupload provides three File Upload Methods: diskfileupload, servletfileupload, and portletfileupload. Among them, diskfileupload has been marked as an expiration method in javadoc. We recommend that you use servletfileupload instead, portletfileupload needs to be used with the Portlet-API, so here we only introduce servletfileupload, which is also the most common.
Com. oreilly. servlet also provides three File Upload Methods: multipartwrapper, multipartrequest, and multipartparser. multipartwrapper and multipartrequest have the same usage, and there are no operations provided by multipartrequest, the multipartrequest, multipartparser, and the first two are different. They can be used to handle some special cases. For example, there are two File Upload selection boxes with the same name in the form.
For the time being, three File Upload methods are: servletfileupload (multiparttestservlet), multipartrequest (multiparttestservlet2), and multipartparser (multiparttestservlet3)
CodeAs follows:
Test
Multiparttestservlet. Java
Package com. Bug. servlet;
Import java. Io. file;
Import java. Io. ioexception;
Import java. util. arraylist;
Import java. util. iterator;
Import java. util. List;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import org. Apache. commons. fileupload. fileitem;
Import org. Apache. commons. fileupload. fileupload;
Import org. Apache. commons. fileupload. fileuploadexception;
Import org. Apache. commons. fileupload. requestcontext;
Import org. Apache. commons. fileupload. disk. diskfileitemfactory;
Import org. Apache. commons. fileupload. servlet. servletfileupload;
Import org. Apache. commons. fileupload. servlet. servletrequestcontext;
Public class multiparttestservlet extends httpservlet {
Public multiparttestservlet (){
Super ();
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Request. setcharacterencoding ("GBK ");
Requestcontext = new servletrequestcontext (request );
If (fileupload. ismultipartcontent (requestcontext )){
Diskfileitemfactory factory = new diskfileitemfactory ();
Factory. setrepository (new file ("C:/tmp /"));
Servletfileupload upload = new servletfileupload (factory );
// Upload. setheaderencoding ("GBK ");
Upload. setsizemax (2000000 );
List items = new arraylist ();
Try {
Items = upload. parserequest (request );
} Catch (fileuploadexception E1 ){
System. Out. println ("File Upload error" + e1.getmessage ());
}
Iterator it = items. iterator ();
While (it. hasnext ()){
Fileitem = (fileitem) it. Next ();
If (fileitem. isformfield ()){
System. out. println (fileitem. getfieldname () + "" + fileitem. getname () + "" + new string (fileitem. getstring (). getbytes ("iso8859-1"), "GBK "));
} Else {
System. Out. println (fileitem. getfieldname () + "" +
Fileitem. getname () + "" +
Fileitem. isinmemory () + "" +
Fileitem. getcontenttype () + "" +
Fileitem. getsize ());
If (fileitem. getname ()! = NULL & fileitem. getsize ()! = 0 ){
File fullfile = new file (fileitem. getname ());
File newfile = new file ("C:/temp/" + fullfile. getname ());
Try {
Fileitem. Write (newfile );
} Catch (exception e ){
E. printstacktrace ();
}
} Else {
System. Out. println ("file not selected or file content is blank ");
}
}
}
}
}
}
Multiparttestservlet2.java
Package com. Bug. servlet;
Import java. Io. ioexception;
Import java. util. enumeration;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import com. oreilly. servlet. multipartrequest;
Import com. oreilly. servlet. multipart. defaultfilerenamepolicy;
Public class multiparttestservlet2 extends httpservlet {
Public multiparttestservlet2 (){
Super ();
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
// Request. setcharacterencoding ("GBK"); does not work
System. Out. println ("START ");
Multipartrequest multi = new multipartrequest (request, "C:/tmp/", 2*1024*1024, "GBK", new defaultfilerenamepolicy ());
System. Out. println ("START ");
Enumeration filesname = multi. getfilenames ();
Enumeration paramsname = multi. getparameternames ();
While (paramsname. hasmoreelements ()){
String paramname = (string) paramsname. nextelement ();
System. Out. println (multi. getparameter (paramname ));
}
While (filesname. hasmoreelements ()){
String filename = (string) filesname. nextelement ();
System. Out. println (multi. getfilesystemname (filename) + "" +
Multi. getoriginalfilename (filename) + "" +
Multi. getcontenttype (filename) + "");
If (multi. getfilesystemname (filename )! = NULL &&! Multi. getfilesystemname (filename). Equals (""))
System. Out. println (multi. GetFile (filename). touri ());
}
}
}
Multiparttestservlet3.java
Package com. Bug. servlet;
Import java. Io. file;
Import java. Io. ioexception;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import com. oreilly. servlet. multipart. filepart;
Import com. oreilly. servlet. multipart. multipartparser;
Import com. oreilly. servlet. multipart. parampart;
Import com. oreilly. servlet. multipart. part;
Public class multiparttestservlet3 extends httpservlet {
Public multiparttestservlet3 (){
Super ();
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Multipartparser MP = new multipartparser (request, 2*1024*1024, false, false, "GBK ");
Part part;
While (Part = mp. readnextpart ())! = NULL ){
String name = part. getname ();
If (part. isparam ()){
Parampart = (parampart) part;
String value = parampart. getstringvalue ();
System. Out. println ("Param: Name =" + name + "; value =" + value );
}
Else if (part. isfile ()){
// It's a file part
Filepart = (filepart) part;
String filename = filepart. getfilename ();
If (filename! = NULL ){
Long size = filepart. writeto (new file ("C:/tmp /"));
System. Out. println ("file: Name =" + name + "; filename =" + filename +
", Filepath =" + filepart. getfilepath () +
", Contenttype =" + filepart. getcontenttype () +
", Size =" + size );
}
Else {
System. Out. println ("file: Name =" + name + "; empty ");
}
System. Out. Flush ();
}
}
}
}
Add in Web. xml
Multiparttestservlet
Com. Bug. servlet. multiparttestservlet
Multiparttestservlet2
Com. Bug. servlet. multiparttestservlet2
Multiparttestservlet3
Com. Bug. servlet. multiparttestservlet3
Multiparttestservlet
/Multiparttestservlet
Multiparttestservlet2
/Multiparttestservlet2
Multiparttestservlet3
/Multiparttestservlet3
Question 1:
You can set encoding to GBK for processing and solve Chinese problems in any of the three methods, including passing GBK as a parameter during initialization or using setencoding after initialization.
In addition, the servletfileupload method can also be implemented through the request. setcharacterencoding ("GBK"); method, but the other two methods do not support this method.
Question 2: file size limit
In the servletfileupload mode, you can set the file size limit, or you do not need to set the limit. In the example, upload. setsizemax (2000000) can be commented out. If you set upload. setsizemax (-1), the upload size is not limited. The document does not specify the default limit. I uploaded a 9 m object without setting it. It can be uploaded. It is estimated that the size is not limited by default.
In the multipartrequest and multipartparser modes, the size limit of the file to be uploaded must be set. If not set, the default value is 1 MB.
Problem 3. File Upload Error
If any errors occur during file upload or the file size is out of the range, the system will throw an error.
The servletfileupload method throws an error in upload. parserequest (request ).
The multipartrequest method is in the new multipartrequest (...) Throw an error
The multipartparser method is in the new multipartparser (...) Throw an error
Problem 4: When uploading files with the same name, they are saved to the temporary directory.
The servletfileupload method does not conflict with each other. The system renames the uploaded files according to certain rules to ensure that they do not conflict with each other.
In multipartparser mode, a conflict occurs. The system saves the file to the temporary directory according to the uploaded file name. If the two files are uploaded with the same file name, in this case, a conflict may occur. One Party replaces the temporary file of the other party.
In multipartrequest mode, if a name conversion policy is provided during initialization, there will be no conflicts. If no bucket is requested, there will be conflicts. In the above example, we provide a new defaultfilerenamepolicy () to ensure that there is no conflict in the file name.
Question 5: There are two File Upload selection boxes with the same name in the form. Just like myfile in our example, each form has two upload boxes with name = "myfile ".
Servletfileupload method, which can be processed and their files can be obtained separately,
The multipartrequest method cannot be processed, and conflicts may occur. A file in the upload box overwrites the other one.
Multipartparser mode, which can be processed and their files can be obtained separately,
Note:
The code is messy, mainly to illustrate the differences between them. Their detailed usage instructions should refer to their Javadoc and domo