Spring MVC 监听文件上传进度
具体实现分三个步骤:
- 接管CommonsMultipartResolver,重写针对文件上传的请求。
- 在第一步中写入监听,以获取上传进度。
- 修改上传部分的配置文件。
重写CommonsMultipartResolver以监听文件上传进度
package com.xc.common.resolver;
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.xc.common.listener.XcProgressListener;
/**
* 重写CommonsMultipartResolver以监听文件上传进度
*/
public class XcCommonsMultipartResolver 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();
XcProgressListener uploadProgressListener = new XcProgressListener(session);
upload.setProgressListener(uploadProgressListener);
}
return upload;
}
public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException {
this.request = request;// 获取到request,要用到session
return super.resolveMultipart(request);
}
@Override
public MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException {
HttpSession session = request.getSession();
String encoding = "utf-8";
FileUpload fileUpload = prepareFileUpload(encoding);
XcProgressListener uploadProgressListener = new XcProgressListener(session);
fileUpload.setProgressListener(uploadProgressListener);
try {
List<FileItem> 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);
}
}
}
实现ProgressListener监听器
package com.xc.common.listener;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.ProgressListener;
import com.xc.entity.common.ProgressEntity;
public class XcProgressListener implements ProgressListener {
private HttpSession session;
public XcProgressListener() {
}
public XcProgressListener(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);
System.out.println(ps.toString());
// 更新
session.setAttribute("upload_ps", ps);
}
}
创建上传进度信息记录实体类
package com.xc.entity.common;
/**
* 文件上传进度信息
*/
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 + "]";
}
}
配置文件部分修改
springmvc-servlet.xml修改
<!-- 文件上传的配置 -->
<!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> -->
<bean id="multipartResolver" class="com.xc.common.resolver.XcCommonsMultipartResolver">
<property name="maxUploadSize" value="1048576" /><!-- 上传文件大小上限,单位为字节(1MB) -->
<property name="defaultEncoding" value="UTF-8" /><!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
</bean>