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.