Common-fileupload is a powerful File Upload Component developed by the Jakarta project team.
Next we will first introduce how to upload files to the server (Multifile upload ):
Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. Io .*;
Import java. util .*;
Import java. util. RegEx .*;
Import org. Apache. commons. fileupload .*;
Public class upload extends httpservlet {
Private Static final string content_type = "text/html; charset = gb2312 ";
// Process the http post request
Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
Response. setcontenttype (content_type );
Printwriter out = response. getwriter ();
Try {
Diskfileupload Fu = new diskfileupload ();
// Set the size of the file to be uploaded. Unit: byte. Set it to 2 MB.
Fu. setsizemax (2*1024*1024 );
// Set up to only data that can be stored in the memory, in bytes
Fu. setsizethreshold (4096 );
// Set the data stored in the hard disk directory when the file size exceeds the value of getsizethreshold ()
Fu. setrepositorypath ("C :\\ Windows \ Temp ");
// Start reading upload information
List fileitems = Fu. parserequest (request );
// Process each uploaded file in sequence
Iterator iter = fileitems. iterator ();
// Regular Expression matching, filtering the path to get the file name
String Regexp = ". + \\\\ (. +) $ ";
// The filtered file type
String [] errortype = {". EXE", ". com", ". cgi", ". asp "};
Pattern P = pattern. Compile (Regexp );
While (ITER. hasnext ()){
Fileitem item = (fileitem) ITER. Next ();
// Ignore all other forms that are not in the file Field
If (! Item. isformfield ()){
String name = item. getname ();
Long size = item. getsize ();
If (name = NULL | Name. Equals ("") & size = 0)
Continue;
Matcher M = P. matcher (name );
Boolean result = M. Find ();
If (result ){
For (INT temp = 0; temp If (M. Group (1). endswith (errortype [temp]) {
Throw new ioexception (name + ": Wrong type ");
}
}
Try {
// Save the uploaded file to the specified directory
// When you upload a file to the database in the following text, it will be rewritten here
Item. Write (new file ("D: \" + M. Group (1 )));
Out. Print (name + "" + size +"
");
}
Catch (exception e ){
Out. println (E );
}
}
Else
{
Throw new ioexception ("fail to upload ");
}
}
}
}
Catch (ioexception e ){
Out. println (E );
}
Catch (fileuploadexception e ){
Out. println (E );
}
}
}
The following is an HTML upload page:
File Upload demonstration
Introduction and Comparison of smartupload and commons-fileupload (zt)
Web file upload may be one of the most commonly used functions in website construction. I almost need to implement the file upload function in project development. some time ago I collected some Upload components. this article describes how to use these components and compares their advantages and disadvantages.
1. smartupload component.
I think anyone who uploads a file knows this component, and most of the people I know use it! When I first arrived at the company, the company also used smartupload, which is a good choice for uploading relatively small files. The following example is used:
<% @ Page contenttype = "text/html; charset = gb2312" %> <% @ page import = "Java. SQL. * "%> <% @ page import =" com. jspsmart. upload. * "%>
<% // Instantiate the Upload Bean smartupload mysmartupload = new smartupload (); // initialize mysmartupload. initialize (pagecontext); // sets the maximum upload value. Note: If this parameter is set, an error occurs! Mysmartupload. setmaxfilesize (500*1024*1024); // upload the mysmartupload file. upload (); // cyclically obtain all uploaded files for (INT I = 0; I <mysmartupload. getfiles (). getcount (); I ++) {// get the uploaded file COM. jspsmart. upload. file myfile = mysmartupload. getfiles (). getFile (I); If (! Myfile. ismissing () {// get the uploaded file name string myfilename = myfile. getfilename (); // get the file name without a suffix string suffix = myfilename. substring (0, myfilename. lastindexof ('. '); // get the suffix string ext = mysmartupload. getfiles (). getFile (0 ). getfileext (); // get the file size int filesize = myfile. getsize (); // save path string AA = getservletcontext (). getrealpath ("/") + "JSP \"; string trace = AA + myfilename; // obtain other parameters string explain = (string) mysmartupload. getrequest (). getparameter ("text"); string send = (string) mysmartupload. getrequest (). getparameter ("send"); // save the file to myfile on the server. saveas (trace, mysmartupload. save_physical); %>
However, when using smartupload to upload a large file or multiple files, the CPU or memory usage may be too high. In addition, you can only restart the container to restore the normal state! This is why I finally gave up using smartupload.
2. commons-fileupload component
This component is the one I use now, which is running.
Example of using this component:
<% @ Page Language = "Java" contenttype = "text/html; charset = GBK" %> <% @ page import = "Java. util. * "%> <% @ page import =" Org. apache. commons. fileupload. * "%> <HTML>
From the above program, we can see that this component can use a place to store temporary files during upload, and directly write the files after the upload is complete. This will not occupy too much memory! This component is inefficient at uploading large files!
In the comparison between the two options, I chose the latter because I often upload files larger than 10 MB in my projects. When I used the former, the server almost crashed.
However, now I don't need these two components, because the efficiency of uploading files via HTTP is always very low. Now we are using FTP file upload via web, I will write in the next article how I implement it in the project.
Thanks
Http://java.mblogger.cn/Elune