1 ImportJava.io.File;2 ImportJava.io.FileOutputStream;3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 Importjava.util.List;6 Importjavax.servlet.ServletException;7 ImportJavax.servlet.http.HttpServlet;8 Importjavax.servlet.http.HttpServletRequest;9 ImportJavax.servlet.http.HttpServletResponse;Ten ImportOrg.apache.commons.fileupload.FileItem; One Importorg.apache.commons.fileupload.disk.DiskFileItemFactory; A ImportOrg.apache.commons.fileupload.servlet.ServletFileUpload; - - Public classUploadhandleservletextendsHttpServlet { the - Public voiddoget (httpservletrequest request, httpservletresponse response) - throwsservletexception, IOException { - //get uploaded files to save the directory, the uploaded files in the Web-inf directory, do not allow direct access to the outside world, to ensure the security of uploading files +String Savepath = This. Getservletcontext (). Getrealpath ("/web-inf/upload"); -File File =NewFile (savepath); + //determine if the saved directory for the uploaded file exists A if(!file.exists () &&!file.isdirectory ()) { atSystem.out.println (savepath+ "directory does not exist, need to create"); - //Create a directory - File.mkdir (); - } - //message Prompt -String message = ""; in Try{ - //Use the Apache File Upload component to process file upload steps: to //1. Create a diskfileitemfactory factory +Diskfileitemfactory factory =Newdiskfileitemfactory (); - //2. Create a file Upload parser theServletfileupload upload =Newservletfileupload (factory); * //solve the Chinese characters of uploading file name $Upload.setheaderencoding ("UTF-8"); Panax Notoginseng //3, determine whether the submitted data is the upload form data - if(!servletfileupload.ismultipartcontent (Request)) { the //get data in the traditional way + return; A } the //4, using the Servletfileupload parser to parse the upload data, the results of the analysis returned is a list<fileitem> collection, each fileitem corresponding to a form form input +list<fileitem> list =upload.parserequest (request); - for(Fileitem item:list) { $ //if the data in the Fileitem is encapsulated in an ordinary entry $ if(Item.isformfield ()) { -String name =item.getfieldname (); - //solve the problem of Chinese garbled data of ordinary input items theString value = item.getstring ("UTF-8"); - //value = new String (value.getbytes ("iso8859-1"), "UTF-8");WuyiSYSTEM.OUT.PRINTLN (name + "=" +value); the}Else{//If the upload file is encapsulated in Fileitem - //get the uploaded file name, WuString filename =item.getname (); - System.out.println (filename); About if(filename==NULL|| Filename.trim (). Equals ("")){ $ Continue; - } - //Note: Different browser submissions are not the same file name, some browsers submitted by the file name is with a path, such as: C:\a\b\1.txt, and some just simple file name, such as: 1.txt - //handles the path portion of the file name of the uploaded files that are obtained, leaving only the file name section Afilename = filename.substring (filename.lastindexof ("\ \") +1); + //gets the input stream of the uploaded file in item theInputStream in =Item.getinputstream (); - //Create a file output stream $FileOutputStream out =NewFileOutputStream (Savepath + "\ \" +filename); the //Create a buffer the byteBuffer[] =New byte[1024]; the //identifies whether the data in the input stream has been read out the intLen = 0; - //The loop reads the input stream into the buffer, and (len=in.read (buffer)) >0 indicates that there is data in it. in while(Len=in.read (buffer)) >0){ the //writes the data of the buffer to the specified directory (savepath + "\ \" + filename) using the FileOutputStream output stream theOut.write (buffer, 0, Len); About } the //close the input stream the in.close (); the //turn off the output stream + out.close (); - //Delete temporary files generated when processing file uploads the Item.delete ();Bayimessage = "File Upload succeeded!" "; the } the } -}Catch(Exception e) { -message= "File upload failed! "; the e.printstacktrace (); the the } theRequest.setattribute ("Message", message); -Request.getrequestdispatcher ("/message.jsp"). Forward (request, response); the } the the Public voidDoPost (httpservletrequest request, httpservletresponse response)94 throwsservletexception, IOException { the the doget (request, response); the }98}
Blog post Code test