1、 接管 CommonsMultipartResolver,重写针对文件上传的请求。
packagecom.sinosoft.amoeba.fileupload;
importcom.sinosoft.amoeba.fileupload.listener.FileUploadProgressListener;
importorg.apache.commons.fileupload.*;
importorg.apache.commons.fileupload.servlet.ServletFileUpload;
importorg.springframework.web.multipart.MaxUploadSizeExceededException;
importorg.springframework.web.multipart.MultipartException;
importorg.springframework.web.multipart.MultipartHttpServletRequest;
importorg.springframework.web.multipart.commons.CommonsMultipartResolver;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpSession;
importjava.util.List;
public classCustomMultipartResolverextendsCommonsMultipartResolver {
privateHttpServletRequest request;
protectedFileUpload newFileUpload(FileItemFactory fileItemFactory) {
ServletFileUpload upload = newServletFileUpload(fileItemFactory);
upload.setSizeMax(-1);
if(request!= null) {
HttpSession session = request.getSession();
FileUploadProgressListenerprogressListener = newFileUploadProgressListener(session);
upload.setProgressListener(progressListener);
}
returnupload;
}
publicMultipartHttpServletRequest resolveMultipart(HttpServletRequestrequest) throwsMultipartException{
this.request=request;
returnsuper.resolveMultipart(request);
}
@Override
publicMultipartParsingResult parseRequest(HttpServletRequest request)throwsMultipartException {
HttpSession session =request.getSession();
String encoding =determineEncoding(request);
FileUpload fileUpload =prepareFileUpload(encoding);
FileUploadProgressListenerprogressListener = newFileUploadProgressListener(session);
fileUpload.setProgressListener(progressListener);
try{
List
returnparseFileItems(fileItems, encoding);
} catch(FileUploadBase.SizeLimitExceededException ex) {
thrownewMaxUploadSizeExceededException(fileUpload.getSizeMax(),ex);
} catch(FileUploadException ex) {
thrownewMultipartException("Could not parse multipart servlet request", ex);
}
}
}
2、 写入监听,获取记录上传进度
- package com.sinosoft.amoeba.fileupload.listener;
- import com.sinosoft.amoeba.fileupload.model.Progress;
- import org.apache.commons.fileupload.ProgressListener;
- import javax.servlet.http.HttpSession;
- public class FileUploadProgressListener implements ProgressListener {
- private HttpSession session;
- public FileUploadProgressListener() { }
- public FileUploadProgressListener(HttpSession session) {
- this.session=session;
- Progress status = new Progress();
- session.setAttribute("upload_ps", status);
- }
- private long old=0;
- public void update(long pBytesRead, long pContentLength, int pItems) {
- Progress status = (Progress) session.getAttribute("upload_ps");
- status.setpBytesRead(pBytesRead);
- status.setpContentLength(pContentLength);
- status.setpItems(pItems);
- if(old>pBytesRead) {
- String a="";
- }else {
- old=pBytesRead;
- }
- session.setAttribute("upload_ps", status);
- }
- }
3、 通过 Progress 类接收上传文件参数
- package com.sinosoft.amoeba.fileupload.model;
- public class Progress {
- 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 + "]";
- }
- }
4、 配置 XML 文件
- <bean id="multipartResolver" class="com.sinosoft.amoeba.fileupload.CustomMultipartResolver">
- <property name="defaultEncoding" value="UTF-8" />
- <property name="maxInMemorySize" value="1000000">
- </property>
- <property name="maxUploadSize" value="10147483648" />
- </bean>
5、 获取监听数据以及进度条展示
Jsp 代码:
- <label id="progress" style="position: relative;left: 10px;top:5px ">
- 保存进度:
- <div id="proBack" style="width: 400px;height: 5px;margin-top:-10px;margin-left:60px;background:gray;">
- <div id="proUp" style="width: 0px;height: 5px;background:red;">
- </div>
- </div>
- <label id="progressId" style="margin-left: 465px;position: relative;top: -11px">
- </label>
- </label>
Js 代码
- //放到上传文件时候
- setInterval(getProgress,100);
- // getProgress方法
- function () {
- $.ajax({
- url:+"/compact/getProgress",
- type:"get",
- dataType:"text",
- async: false,
- success:function(data) {
- var backGroundWidth = $("#proBack").width();
- var proUpWidth = (backGroundWidth*data.replace("%",""))/100;
- $("#proUp").width(proUpWidth);
- $("#progressId").text(data);
- if(data=='100%') {
- =false;
- }
- },
- error:function(data) {
- ("系统消息","出错了!");
- }
- });
- }
- java代码:
- Progress status = (Progress) request.getSession().getAttribute("upload_ps");
- //已上传字节数
- status.getpBytesRead();
- //总字节数
- status.getpContentLength()
- //根据需要返回上传百分比即可
- 但是这种情况对服务器访问次数太多,用户太多或者服务器性能不好这么做不太好。
- 另一种实现方式为做一个假的进度条或者转动的圆圈,当文件没有上传成功就一直转,上传成功,弹出保存成功,即可
来源: http://www.bubuko.com/infodetail-1869016.html