Implementing file uploads using a servlet

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.