1 PackageCom.itheima.upload;2 3 ImportJava.io.File;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 ImportJava.io.InputStream;7 ImportJava.io.OutputStream;8 9 Importjavax.servlet.ServletException;Ten ImportJavax.servlet.annotation.MultipartConfig; One ImportJavax.servlet.annotation.WebServlet; A ImportJavax.servlet.http.HttpServlet; - Importjavax.servlet.http.HttpServletRequest; - ImportJavax.servlet.http.HttpServletResponse; the ImportJavax.servlet.http.Part; - - Importcom.itheima.utils.UUIDUtils; - Importcom.itheima.utils.UploadUtils; + /** - * File Upload servlet + * Form items: Divided into two categories is the File Table item category is non-file table items (normal form items, do not have to set enctype= "Mutipart/form-data") A */ at @MultipartConfig -@WebServlet ("/uploadservlet") - Public classUploadservletextendsHttpServlet { - Private Static Final LongSerialversionuid = 1L; - protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { - //Accept normal data non-file form items that are normal form items do not have to set enctype= "Multipart/form-data" inRequest.setcharacterencoding ("UTF-8"); -String Filedesc = Request.getparameter ("Filedesc"); toSYSTEM.OUT.PRINTLN ("File Description:" +filedesc); + //The Accept File file form item must meet the three-factor post form enctype= "Multipart/form-data" - //get the part object directly from the Name property of the form file control (<input type= "file" name= "upload" >) thePart part = Request.getpart ("Upload"); * //Get File Size $ LongSize =part.getsize ();Panax NotoginsengSYSTEM.OUT.PRINTLN ("File size:" +size); -String name =part.getname (); theSystem.out.println (the value of the file upload form's Name property is: "+name); + //Get header information AString Header = Part.getheader ("Content-disposition"); the System.out.println (header); + //get file name \ "For Escape - intLastIndexOf = Header.lastindexof ("filename=\" "); $String filename = header.substring (lastindexof+10, Header.length ()-1); $System.out.println ("File name:" +filename); - //Get file Content -InputStream ips =Part.getinputstream (); the //get the folder path to upload -String Realpath = This. Getservletcontext (). Getrealpath ("/upload");Wuyi System.out.println (realpath); the //get a unique file name -String Uniname =uuidutils.getuuidfilename (filename); Wu //get a new upload path -String Fenglipath = Realpath +Uploadutils.getpath (uniname); About System.out.println (fenglipath); $ //create file object for upload path -File File =NewFile (fenglipath); - //if the upload path does not exist, create - if(!file.exists ()) { A file.mkdirs (); + } the //Create a byte output stream with the parameter "path + file name" -OutputStream Ops =NewFileOutputStream (fenglipath+ "/" +uniname); $ //IO Stream read/write the byte[] bytes =New byte[1024]; the intLen; the while((Len=ips.read (bytes))!=-1){ theOps.write (Bytes, 0, Len); - } in //off-Stream the ips.close (); the ops.close (); About the the } the protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { + doget (request, response); - } the Bayi}
Servlet3.0 Uploading Files