Implementation of JSP Servlet File Upload

Source: Internet
Author: User

The implementation of JSP Servlet File Upload is first to prepare the environment and software:

Common-fileupload component

: Http://jakarta.apache.org/commons/fileupload/

Download and unzip the zip package, copy the commons-fileupload-1.0.jar to tomcat's webapps \ your webapp \ WEB-INF \ lib \

Create a servlet

 
 
  1. Import java. io .*;
  2. Import java. util .*;
  3. Import javax. servlet .*;
  4. Import javax. servlet. http .*;
  5. Import org. apache. commons. fileupload .*;
  6.  
  7. Public class Upload extends HttpServlet {
  8.  
  9. Private StringUploadPath="C: \ upload \\"; // Directory used to store uploaded files
  10. Private StringTempPath="C: \ upload \ tmp \\"; // Directory used to store temporary files
  11.  
  12. Public void doPost (HttpServletRequest request, HttpServletResponse response)
  13. Throws IOException, ServletException
  14. {
  15. Try {
  16. DiskFileUploadFu=NewDiskFileUpload ();
  17. // Set the maximum file size, which is 4 MB
  18. Fu. setSizeMax (4194304 );
  19. // Set the buffer size, which is 4 kb
  20. Fu. setSizeThreshold (4096 );
  21. // Set the temporary directory:
  22. Fu. setRepositoryPath (tempPath );
  23.  
  24. // Obtain all objects:
  25. ListFileItems=Fu. ParseRequest (request );
  26. IteratorI=FileItems. Iterator ();
  27. // Process each file in sequence:
  28. While (I. hasNext ()){
  29. FileItemFi= (FileItem) I. next ();
  30. // Get the file name, which includes the path:
  31. StringFiFileName= Fi. getName ();
  32. If (fileName! = Null ){
  33. // User and file information can be recorded here
  34. //...
  35. // Write the.txt file. You can also extract the file name from fileName:
  36. Fi. write (new File (uploadPath + "a.txt "));
  37. }
  38. }
  39. // Jump to the upload success prompt page
  40. }
  41. Catch (Exception e ){
  42. // Jump to the error page
  43. }
  44. }
  45. }


After 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:

 
 
  1. Public void init () throws ServletException {
  2. UploadPath= ....
  3. TempPath= ....
  4. // The folder is automatically created if it does not exist:
  5. If (! New File (uploadPath). isDirectory ())
  6. New File (uploadPath). mkdirs ();
  7. If (! New File (tempPath). isDirectory ())
  8. New File (tempPath). mkdirs ();
  9. }

Servlet File Upload configuration Servlet, open tomcat \ webapps \ your webapp \ WEB-INF \ web. xml with notepad, if not, create a new one. The typical configuration is as follows:

 
 
  1. ﹤?xml version="1.0" encoding="ISO-8859-1"?﹥  
  2. ﹤!DOCTYPE web-app  
  3.     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
  4.     "http://java.sun.com/dtd/web-app_2_3.dtd"﹥  
  5. ﹤web-app﹥  
  6.     ﹤servlet﹥  
  7.         ﹤servlet-name﹥Upload﹤/servlet-name﹥  
  8.         ﹤servlet-class﹥Upload﹤/servlet-class﹥  
  9.     ﹤/servlet﹥  
  10.     ﹤servlet-mapping﹥  
  11.         ﹤servlet-name﹥Upload﹤/servlet-name﹥  
  12.         ﹤url-pattern﹥/fileupload﹤/url-pattern﹥  
  13.     ﹤/servlet-mapping﹥  
  14. ﹤/web-app﹥ 

End of Servlet File Upload: After configuring the Servlet, start Tomcat and write a simple html test:

 
 
  1. ﹤form action="fileupload" method="post" enctype="multipart/form-data" name="form1"﹥  
  2.   ﹤input type="file" name="file"﹥  
  3.   ﹤input type="submit" name="Submit" value="upload"﹥  
  4. ﹤/form﹥ 

Note action = "fileupload" where fileupload is the url-pattern specified When configuring the servlet.

So the JSP Servlet File Upload implementation is complete. Do you have any changes in development ideas?

  1. What is JSP and comparison with Servlet?
  2. 8-point discussion on optimizing JSP Servlet applications
  3. Servlet import event-driven technology in JSP development
  4. Configuration of JSP, Servlet, and Bean in Tomcat
  5. How to Improve Servlet and JSP Application Efficiency

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.