1 PackageCom.zhanghaobo.fileupload;2 3 ImportJava.io.File;4 Importjava.io.IOException;5 Importjava.util.List;6 7 Importjavax.servlet.ServletException;8 ImportJavax.servlet.http.HttpServlet;9 Importjavax.servlet.http.HttpServletRequest;Ten ImportJavax.servlet.http.HttpServletResponse; One A ImportOrg.apache.commons.fileupload.FileItem; - Importorg.apache.commons.fileupload.disk.DiskFileItemFactory; - ImportOrg.apache.commons.fileupload.servlet.ServletFileUpload; the - Public classUploadservletextendsHttpServlet { - @Override - protected voidDoPost (httpservletrequest req, HttpServletResponse resp) + throwsservletexception, IOException { -Diskfileitemfactory factory=Newdiskfileitemfactory (); + AString Realpath=req.getrealpath ("/upload"); at - //Physical Cache storage location -Factory.setrepository (NewFile (Realpath)); - - //set Memory storage size -Factory.setsizethreshold (1024*1024); in -Servletfileupload upload=Newservletfileupload (factory); to //Fileitem can indicate that a file or a text field (form Item) has a Isformfield method to determine whether the form + Try{ -List<fileitem> list= (list<fileitem>) upload.parserequest (req); the for(Fileitem item:list) { *String name=item.getfieldname (); $ if(Item.isformfield ()) {Panax NotoginsengString value=item.getstring (); - Req.setattribute (name, value); the}Else{ +String value=item.getname (); A intStart=value.lastindexof ("\ \"); theString filename=value.substring (start+1); + Req.setattribute (name, fileName); - //upload a file to the repository $Item.write (NewFile (Realpath,filename)); $ } - } -}Catch(Exception e) { the e.printstacktrace (); - }Wuyi theReq.getrequestdispatcher ("showresult.jsp"). Forward (req, resp); - } Wu}
1 Public classUploadservletextendsHttpServlet {2 @Override3 protected voidDoPost (httpservletrequest req, HttpServletResponse resp)4 throwsservletexception, IOException {5Diskfileitemfactory factory=Newdiskfileitemfactory ();6 7String Realpath=req.getrealpath ("/upload");8 9 //Physical Cache storage locationTenFactory.setrepository (NewFile (Realpath)); One A //set Memory storage size -Factory.setsizethreshold (1024*1024); - theServletfileupload upload=Newservletfileupload (factory); - //Fileitem can indicate that a file or a text field (form Item) has a Isformfield method to determine whether the form - Try{ -List<fileitem> list= (list<fileitem>) upload.parserequest (req); + for(Fileitem item:list) { -String name=item.getfieldname (); + if(Item.isformfield ()) { AString value=item.getstring (); at Req.setattribute (name, value); -}Else{ -String value=item.getname (); - intStart=value.lastindexof ("\ \"); -String filename=value.substring (start+1); - Req.setattribute (name, fileName); in - //upload a file to the repository to //Item.write (New File (Realpath,filename)); + -InputStream is=Item.getinputstream (); the * LongAll=item.getsize (); $ Panax NotoginsengOutputStream os=NewFileOutputStream (NewFile (Realpath,filename)); - the byte[] buffer=New byte[400]; + A intLength=0; the intL=0; +DecimalFormat df=NewDecimalFormat ("#.00"); - while( -1!= (length=is.read (buffer))) { $Os.write (buffer,0, length); $l+=length; -SYSTEM.OUT.PRINTLN ("Total size" +all+ "" + "Progress:" + (Df.format (Double) + l/all*100) + "%" + "size already read" +l); - } the - is.close ();Wuyi os.close (); the - } Wu } -}Catch(Exception e) { About e.printstacktrace (); $ } - -Req.getrequestdispatcher ("showresult.jsp"). Forward (req, resp); - } A}
Implementing file uploads using a servlet