Java upload file progress bar method of of with demo source download

  • 2020-04-01 04:33:00
  • OfStack

This article illustrates the implementation of the Java upload file progress bar. Share with you for your reference, as follows:

It's simple, mostly using commons-fileupload, and it has a progressListener interface that updates the size of uploaded files in real time.

Here is 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);
  }
  
  public void update(long pBytesRead, long pContentLength, int pItems) {
    // TODO Auto-generated method stub
    fileUploadStatus status = (fileUploadStatus) session.getAttribute("status");
    status.setPBytesRead(pBytesRead);
    status.setPContentLength(pContentLength);
    status.setPItems(pItems);
  }
}

You can then add such a snippet to the uploaded servlet or action to add a custom progressListener


myProgressListener getBarListener = new myProgressListener(req);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setProgressListener(getBarListener);

Finally, js is used to continuously access another servlet to return the upload status in real time. I will not post the code due to the limitation of space. Interested readers can download it by themselves.

Complete sample code click here (link: http://xiazai.jb51.net/201512/yuanma/java-upload-Progress-style-codes (jb51.net). Rar).

I hope this article has been helpful to you in Java programming.


Related articles: