JSP component commons-fileupload implements file upload and jsp File Upload components

Source: Internet
Author: User

JSP component commons-fileupload implements file upload and jsp File Upload components

This article shares with you the code for using commons-fileupload to upload files in JSP for your reference. The details are as follows:

1. preparation:

Copy commons-fileupload-1.1.zipand commons-io-1.1.zip to the "\ WEB-INF \ lib" Directory

2. Servlet: FileUpload. java

Package servlet; import java. io. file; import java. io. IOException; import java. io. printWriter; import java. util. *; import java. util. regex. pattern; import java. util. regex. matcher; 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. fileUploadException; import org. apache. commons. fileupload. disk. diskFileItemFactory; import org. apache. commons. fileupload. servlet. servletFileUpload; public class FileUpload extends HttpServlet {private String uploadPath = "E: \ addnetFile \"; // private File tempPath = new File ("E: \ tempFile \ "); // public void doGet (HttpServletRequest request, HttpServletResponse respo AUC) throws ServletException, IOException {} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setContentType ("text/html; charset = GB2312"); response. setCharacterEncoding ("gb2312"); PrintWriter out = response. getWriter (); out. println ("the length of the request content is:" + request. getContentLength (); out. println ("request content type:" + request. getContentType (); Di SkFileItemFactory factory = new DiskFileItemFactory (); factory. setRepository (tempPath); factory. setSizeThreshold (4096); ServletFileUpload upload = new ServletFileUpload (factory); upload. setSizeMax (1000000); List <?> Fileitems = null; try {fileitems = upload. parseRequest (request); Iterator <?> Iterator = fileitems. iterator (); String regex = ". + \\\\(. +) $ "; String [] errortype = {". exe ",". com ",". cgi ",". asp "}; Pattern p = Pattern. compile (regex); while (iterator. hasNext () {FileItem item = (FileItem) iterator. next (); if (! Item. isFormField () {String name = item. getName (); long size = item. getSize (); if (name = null | name. equals ("") & size = 0) continue; Matcher m = p. matcher (name); if (m. find () {for (int temp = 0; temp <errortype. length; temp ++) {if (m. group (1 ). endsWith (errortype [temp]) throw new IOException (name + ": wrong type");} try {item. write (new File (tempPath, m. group (1); out. println (name + "" + size + "<br/>"); out. println ("uploaded successfully" );} Catch (Exception e) {out. println ("333" + e) ;}} else {throw new IOException ("fail to upload") ;}}} catch (IOException e) {out. println ("222" + e);} catch (FileUploadException e1) {e1.printStackTrace (); out. println ("111" + e1) ;}} public void init () throws ServletException {if (! New File (uploadPath). isDirectory () new File (uploadPath). mkdir (); if (! TempPath. isDirectory () tempPath. mkdir ();} public void destroy () {super. destroy ();}}

3. Followed by html: Uploadfile.html

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

4. Configure web. xml

<servlet>  <description>This is the description of my J2EE component</description>  <display-name>This is the display name of my J2EE component</display-name>  <servlet-name>FileUpload</servlet-name>  <servlet-class>servlet.FileUpload</servlet-class> </servlet><servlet-mapping>  <servlet-name>FileUpload</servlet-name>  <url-pattern>/servlet/FileUpload</url-pattern>

First, run html and servlet to process the upload request.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.