Java Upload file progress bar implementation method (with demo source download) _java

Source: Internet
Author: User

This article describes the Java upload file progress bar implementation method. Share to everyone for your reference, specific as follows:

Something is very simple, mainly used to Commons-fileupload, which has a Progresslistener interface, the interface can be implemented in real time to update the size of uploaded files, with this also say what?

Here's the code:

 package lc.progress; import javax.servlet.http.HttpServletRequest; import
Javax.servlet.http.HttpSession;
Import Lc.progress.vo.fileUploadStatus;
Import Org.apache.commons.fileupload.ProgressListener;
  public class Myprogresslistener implements Progresslistener {private HttpSession session;
    Public Myprogresslistener (HttpServletRequest req) {session=req.getsession ();
    Fileuploadstatus status = new Fileuploadstatus ();
  Session.setattribute ("status", status); }/* Pbytesread the number of bits read to date * pcontentlength file Total size * Pitems is currently reading the first few files * just save the status of the file uploads in the session (Here I use Fileuplo Adstatus class to encapsulate) */public void update (long pbytesread, long pcontentlength, int pitems) {//TODO auto-generated Me
    Thod stub fileuploadstatus status = (fileuploadstatus) session.getattribute ("status");
    Status.setpbytesread (Pbytesread);
    Status.setpcontentlength (pcontentlength);
  Status.setpitems (Pitems); }
}

You can then add the custom Progresslistener by adding such a piece of code to the servlet or action you upload.

Myprogresslistener Getbarlistener = new Myprogresslistener (req);
Servletfileupload upload = new Servletfileupload (factory);
Upload.setprogresslistener (Getbarlistener);

The last is through JS to continuously access another servlet to real-time return to the status of the upload can be, limited to the length of my code no longer, interested readers can download their own look.

Full instance code click here to download the site.

I hope this article will help you with Java programming.

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.