Commons fileupload+servlet+jquery Implementation file upload progress bar

Source: Internet
Author: User
Tags file upload progress bar

Implementation of file dynamic upload, the general idea is this:

First of all, using the Progresslistener interface of common fileupload to talk about the percentage of file uploads in real time,

Then, implement a servlet that reads the session, using AJAX at the Web end to request this servlet for a certain amount of time.

Finally, display the file upload percentage on the web side and make a progress bar.

1. Realize Progresslistener

Servletfileupload upload = new Servletfileupload (upload_factory);
		Class Uploadprogresslistener implements progresslistener{
			private httpservletrequest request;
			Private DecimalFormat df = new DecimalFormat ("#00.0");
			Uploadprogresslistener (HttpServletRequest request) {
				this.request = Request;
			}
			@Override public
			void Update (Long bytesread, long bytestotal, int items) {
				double percent= (double) bytesread*100 /(double) bytestotal;
				SYSTEM.OUT.PRINTLN (Df.format (percent));
				Request.getsession (). setattribute ("Upload_percentage", Df.format (percent));
			}
		Upload.setprogresslistener (Request) (new Uploadprogresslistener);
2. Implementation of the servlet reading session

HttpSession session = Request.getsession ();
		Object Is_begin = Session.getattribute ("Is_upload_begin");
		if (is_begin==null) return;
		if ("0". Equals (Is_begin.tostring ()) return;
		PrintWriter out = Response.getwriter ();
		Object upload_percentage = Session.getattribute ("Upload_percentage");
		Out.write ("{percentage:") +upload_percentage.tostring () + "'}");
		Out.flush ();
Ajax requests for 3.web-end-time intervals

function Beginupload () {
		$ ("#progress_bar"). Show ();
		SetInterval ("Getuploadmeter ()", 1000);
	}
	function Getuploadmeter () {
		$.post ("Uploadmeter", function (data) {
			var json = eval ("+data+"));
			JQuery ("#progress"). CSS ("width", json.percentage/100*200+ "px");
			JQuery ("#msg"). CSS ("padding", "5px"). html (json.percentage+ "%");}
	






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.