Common-fileupload component
: Http://jakarta.apache (the most popular WEB server platform for Unix). org/commons/fileupload/
Download and unzip the zip package, copy the commons-fileupload-1.0.jar to tomcat (a good JSP running platform) webapps under your webappWEB-INFlib
Create a servlet
Import java. io .*;
Import java. util .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import org. apache (the most popular WEB server platform on the Unix platform). commons. fileupload .*;
Public class Upload extends HttpServlet {
Private String uploadPath = "C: \ upload \"; // directory used to store uploaded files
Private String tempPath = "C: \ upload \ tmp \"; // directory used to store temporary files
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws IOException, ServletException
{
Try {
DiskFileUpload fu = new DiskFileUpload ();
// Set the maximum file size, which is 4 MB
Fu. setSizeMax (4194304 );
// Set the buffer size, which is 4 kb
Fu. setSizeThreshold (4096 );
// Set the temporary directory:
Fu. setRepositoryPath (tempPath );
// Obtain all objects:
List fileItems = fu. parseRequest (request );
Iterator I = fileItems. iterator ();
// Process each file in sequence:
While (I. hasNext ()){
FileItem fi = (FileItem) I. next ();
// Get the file name, which includes the path:
String fileName = fi. getName ();
If (fileName! = Null ){
// User and file information can be recorded here
//...
// Write the.txt file. You can also extract the file name from fileName:
Fi. write (new File (uploadPath + "a.txt "));
}
}
// Jump to the upload success prompt page
}
Catch (Exception e ){
// Jump to the error page
}
}
}
// When the servlet receives a Post request from the browser, it uploads the file in the doPost () method. The following is the sample code:
To read the specified upload folder in the configuration file, run the following command in the init () method:
Public void init () throws ServletException {
UploadPath = ....
TempPath = ....