The jar to be referenced
Commons-fileupload-1.3.1.jar
Commons-io-2.2.jar
index.jsp
<body> <center>
fileuploadservlet.jsp
Package com.accp.fileupload.servlet; Import Java.io.File; Import java.io.IOException; Import java.util.List; 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.FileUploadBase.FileSizeLimitExceededException; Import Org.apache.commons.fileupload.disk.DiskFileItemFactory; Import Org.apache.commons.fileupload.servlet.ServletFileUpload; public class Fileuploadservlet extends HttpServlet {private static final long Serialversionuid = 282729729 9439162553L; public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioex ception {//using the same processing method DoPost (request, response); } public void DoPost (HttpServletRequest request, httpservletresponse response) throws Serv Letexception, IOException {request.setcharacterencoding ("UTF-8"); Response.setcontenttype ("text/html; Charset=utf-8 "); Save path String Savepath = Getservletcontext (). Getrealpath ("/upload"); File Savedir = new file (Savepath); If the directory does not exist, create a directory if (!savedir.exists ()) {Savedir.mkdir (); }//Create file Upload Core class Diskfileitemfactory factory = new Diskfileitemfactory (); Servletfileupload SFU = new Servletfileupload (factory); Set the encoding sfu.setheaderencoding ("UTF-8"); Set the maximum number of bytes uploaded for a single file to 2M Sfu.setfilesizemax (1024*1024*2); Set the maximum number of bytes for the entire form to 10M Sfu.setsizemax (1024*1024*10); try{//Processing form request List<fileitem> ItemList = sfu.parserequest (request); for (Fileitem fileitem:itemlist) {//corresponds to the name of the control in the form String fie Ldname = Fileitem.getfieldname (); SYSTEM.OUT.PRINTLN ("Control Name:" + fieldName); If it is a normal form control if (Fileitem.isformfield ()) {String value = fileitem.getstring () ; Recoding, resolving garbled value = new String (value.getbytes ("iso-8859-1"), "UTF-8"); System.out.println ("Normal content:" + fieldName + "=" + value); Upload file}else{//Get file size Long size = Fileitem.getsi Ze (); Get filename String filename = fileitem.getname (); System.out.println ("File name:" +filename+ "\ T size:" +Size + "byte"); Sets the file format that is not allowed to be uploaded if (filename.endswith ("EXE")) {Request.setattribute (" Msg "," does not allow the type of upload! "); }else{//Saves files to the specified path file File = new file (savepath,filename); Fileitem.write (file); Request.setattribute ("msg", "Upload succeeded!") "); }}}}catch (Filesizelimitexceededexception e) {reques T.setattribute ("msg", "File Too large"); }catch (fileuploadexception e) {e.printstacktrace (); }catch (Exception e) {e.printstacktrace (); }//After uploading to the homepage request.getrequestdispatcher ("/index.jsp"). Forward (request, response); } }
Note that you should submit the form in action, and do not use Ajax to submit the form, otherwise there will be exception information.
Org.apache.commons.fileupload.fileuploadbase$invalidcontenttypeexception:the request doesn ' t contain a multipart/ Form-data or multipart/mixed stream, content type header is null
At Org.apache.commons.fileupload.fileuploadbase$fileitemiteratorimpl.<init> (FileUploadBase.java:885)
Jsp/serlet Uploading files using FileUpload