JSP/servlet File Upload

Source: Internet
Author: User

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 = ....

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.