First, Spring is a very mature J2EE framework, and its non-invasive architecture provides the greatest possibilities for system integration and extension. Therefore, it is very easy to implement progress monitoring under Spring MVC, and there is no need to change the previous upload business code. The specific implementation is divided into three steps: 1. Take over CommonsMultipartResolver and rewrite the request for file upload. 2. Write listening in the first step to get the upload progress. 3. Modify the uploaded configuration file. See code 1, CommonsMultipartResolver code

package com.van.utils.upload.handle;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;

import org.apache.commons.fileupload.FileItemFactory;

import org.apache.commons.fileupload.FileUpload;

import org.apache.commons.fileupload.FileUploadBase;

import org.apache.commons.fileupload.FileUploadException;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.springframework.web.multipart.MaxUploadSizeExceededException;

import org.springframework.web.multipart.MultipartException;

import org.springframework.web.multipart.MultipartHttpServletRequest;

import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.van.utils.upload.listener.PJProgressListener;

/ * *

* Rewrite CommonsMultipartResolver to listen for file upload progress

* @author Van

* @date 2012-12-12

* /

public class PJCommonsMultipartResolver extends CommonsMultipartResolver {

private HttpServletRequest request;

protected FileUpload newFileUpload(FileItemFactory fileItemFactory) {

ServletFileUpload upload = new ServletFileUpload(fileItemFactory);

upload.setSizeMax(-1);

if (request ! = null) {

HttpSession session = request.getSession();

PJProgressListener uploadProgressListener = new PJProgressListener(session);

upload.setProgressListener(uploadProgressListener);

}

return upload;

}

public MultipartHttpServletRequest resolveMultipart(

HttpServletRequest request) throws MultipartException {

this.request = request; // To get the request, use session

return super.resolveMultipart(request);

}

@SuppressWarnings(unchecked)

@Override

public MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException {

HttpSession session = request.getSession();

String encoding = utf-8;

FileUpload fileUpload = prepareFileUpload(encoding);

PJProgressListener uploadProgressListener = new PJProgressListener(session);

fileUpload.setProgressListener(uploadProgressListener);

try {

List fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);

return parseFileItems(fileItems, encoding);

}

catch (FileUploadBase.SizeLimitExceededException ex) {

throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);

}

catch (FileUploadException ex) {

throw new MultipartException(Could not parse multipart servlet request, ex);

}

}

}

PJProgressListener code:

package com.van.utils.upload.listener;

import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.ProgressListener;

import com.van.utils.upload.entity.ProgressEntity;

public class PJProgressListener implements ProgressListener{

private HttpSession session;

public PJProgressListener() {

}

public PJProgressListener(HttpSession _session) {

session=_session;

ProgressEntity ps = new ProgressEntity();

session.setAttribute(upload_ps, ps);

}

public void update(long pBytesRead, long pContentLength, int pItems) {

ProgressEntity ps = (ProgressEntity) session.getAttribute(upload_ps);

ps.setpBytesRead(pBytesRead);

ps.setpContentLength(pContentLength);

ps.setpItems(pItems);

/ / update

session.setAttribute(upload_ps, ps);

}

}

Upload progress record entity class:

package com.van.utils.upload.entity;

/ * *

* File upload progress

*

* /

public class ProgressEntity {

private long pBytesRead = 0L;

private long pContentLength = 0L;

private int pItems;

public long getpBytesRead() {

return pBytesRead;

}

public void setpBytesRead(long pBytesRead) {

this.pBytesRead = pBytesRead;

}

public long getpContentLength() {

return pContentLength;

}

public void setpContentLength(long pContentLength) {

this.pContentLength = pContentLength;

}

public int getpItems() {

return pItems;

}

public void setpItems(int pItems) {

this.pItems = pItems;

}

@Override

public String toString() {

return ProgressEntity [pBytesRead= + pBytesRead + , pContentLength=

+ pContentLength + , pItems= + pItems + ];

}

}

Configuration file partial modification:

Will org.springframework.web.multipart.commons.Com monsMultipartResolver replace: com.van.utils.upload.handle.PJCommonsMultipartResolver