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.