Springmvc Upload File Progress display

Source: Internet
Author: User


Fileuploadcontroller.java

Import Java.io.file;import java.util.List;  Import Javax.servlet.http.HttpServletRequest;  Import Javax.servlet.http.HttpServletResponse;    Import javax.servlet.http.HttpSession;  Import Org.apache.commons.fileupload.fileitem;import org.apache.commons.fileupload.FileItemFactory;  Import Org.apache.commons.fileupload.ProgressListener;  Import Org.apache.commons.fileupload.disk.DiskFileItemFactory;  Import Org.apache.commons.fileupload.servlet.ServletFileUpload;  Import Org.apache.log4j.Logger;  Import Org.springframework.stereotype.Controller;  Import org.springframework.web.bind.annotation.RequestMapping;  Import Org.springframework.web.bind.annotation.RequestMethod;  Import Org.springframework.web.bind.annotation.ResponseBody;    Import Org.springframework.web.servlet.ModelAndView;            @Controller public class Fileuploadcontroller {Logger log = Logger.getlogger (Fileuploadcontroller.class); @RequestMapping (value = "/touploadfile", method = Requestmethod.get) public Modelandview Toupload (httpservletrequest request, httpservletresponse response) throws Exception{return new        Modelandview ("Upload2");      }/** * Upload upload file * @param request * @param response * @return * @throws Exception */ @RequestMapping (value = "/uploadfile2", method = requestmethod.post) public Modelandview upload (httpservletreque St request, httpservletresponse Response) throws Exception {final HttpSession hs = Request.getsessi          On ();        Modelandview mv = new Modelandview ();          Boolean ismultipart = servletfileupload.ismultipartcontent (request);          if (!ismultipart) {return MV;            }//Create a factory for disk-based file items Fileitemfactory factory = new Diskfileitemfactory ();          Create a new file upload handler servletfileupload upload = new Servletfileupload (factory); Upload.setprogresslistener (New Progresslistener ({public void update (long pbytesread, long pcontentlength, int pitems) {ProcessInfo                     pri = new ProcessInfo ();                     Pri.itemnum = Pitems;                     Pri.readsize = Pbytesread;                     Pri.totalsize = Pcontentlength;                     Pri.show = pbytesread+ "/" +pcontentlength+ "byte";                     Pri.rate = Math.Round (new float (pbytesread)/new float (pcontentlength) *100);                 Hs.setattribute ("Proinfo", PRI);          }              });        list<fileitem> items = upload.parserequest (request);        String path = Request.getsession (). Getservletcontext (). Getrealpath ("Upload");        SYSTEM.OUT.PRINTLN (path); for (Fileitem item:items) {if (Item.isformfield ()) {}else{system.out.println (path + "/" + item.getfieldname ()); File TargetFile = new file (path + "/" + Item.getname ()), if (!targetfile.exists ()) {targetfile.createnewfile ();}        Item.write (TargetFile);}} System.out.prinTLN ("Number of uploaded files:" + items.size ());    return MV; }/** * Process get progress * @param request * @param response * @return * @throws exce Ption */@RequestMapping (value = "/process", method = Requestmethod.get) @ResponseBody public Object p Rocess (HttpServletRequest request, httpservletresponse response) throws Exception {return (Process      Info) request.getsession (). getattribute ("Proinfo");          } class processinfo{public long totalsize = 1;          public long readsize = 0;          Public String show = "";          public int itemnum = 0;      public int rate = 0;   }        }

upload.jsp

<! DOCTYPE html><%@ page contenttype= "Text/html;charset=utf-8"%> <%@ taglib prefix= "C" uri= "/HTTP/ Java.sun.com/jsp/jstl/core "%> 

Springmvc Upload File Progress display

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.