Spring MVC 监听文件上传进度,实现上传进度条

首先Spring是一个非常成熟的J2EE框架,其非入侵式的架构为系统的集成和扩展提供了最大的可能。所以Spring MVC下实现进度监听非常容易,甚至不需要改以前的上传业务代码,具体实现分三个步骤:1、接管CommonsMultipartResolver,重写 针对文件上传的请求。2、在第一步中写入监听,以获取上传进度。3、修改上传部分的配置文件。具体见代码1、 CommonsMultipartResolver代码

[java] view plain copy
  1. package com.van.utils.upload.handle;  
  2. import java.util.List;  
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpSession;  
  5. import org.apache.commons.fileupload.FileItem;  
  6. import org.apache.commons.fileupload.FileItemFactory;  
  7. import org.apache.commons.fileupload.FileUpload;  
  8. import org.apache.commons.fileupload.FileUploadBase;  
  9. import org.apache.commons.fileupload.FileUploadException;  
  10. import org.apache.commons.fileupload.servlet.ServletFileUpload;  
  11. import org.springframework.web.multipart.MaxUploadSizeExceededException;  
  12. import org.springframework.web.multipart.MultipartException;  
  13. import org.springframework.web.multipart.MultipartHttpServletRequest;  
  14. import org.springframework.web.multipart.commons.CommonsMultipartResolver;  
  15.   
  16. import com.van.utils.upload.listener.PJProgressListener;  
  17. /** 
  18.  * 重写CommonsMultipartResolver以监听文件上传进度 
  19.  * @author Van 
  20.  * @date 2012-12-12 
  21.  */  
  22. public class PJCommonsMultipartResolver extends CommonsMultipartResolver {  
  23.       
  24.     private HttpServletRequest request;  
  25.     protected FileUpload newFileUpload(FileItemFactory fileItemFactory) {  
  26.         ServletFileUpload upload = new ServletFileUpload(fileItemFactory);  
  27.         upload.setSizeMax(-1);  
  28.         if (request != null) {  
  29.             HttpSession session = request.getSession();  
  30.             PJProgressListener uploadProgressListener = new PJProgressListener(session);  
  31.             upload.setProgressListener(uploadProgressListener);  
  32.         }  
  33.         return upload;  
  34.     }  
  35.     public MultipartHttpServletRequest resolveMultipart(  
  36.             HttpServletRequest request) throws MultipartException {  
  37.         this.request = request;// 获取到request,要用到session  
  38.         return super.resolveMultipart(request);  
  39.     }  
  40.       
  41.       
  42.     @SuppressWarnings("unchecked")  
  43.     @Override  
  44.     public MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException {  
  45.           
  46.         HttpSession session = request.getSession();  
  47.           
  48.         String encoding = "utf-8";  
  49.         FileUpload fileUpload = prepareFileUpload(encoding);  
  50.          
  51.         PJProgressListener uploadProgressListener = new PJProgressListener(session);  
  52.         fileUpload.setProgressListener(uploadProgressListener);  
  53.         try {  
  54.             List<fileitem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);  
  55.             return parseFileItems(fileItems, encoding);  
  56.         }  
  57.         catch (FileUploadBase.SizeLimitExceededException ex) {  
  58.             throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);  
  59.         }  
  60.         catch (FileUploadException ex) {  
  61.             throw new MultipartException("Could not parse multipart servlet request", ex);  
  62.         }  
  63.     }  
  64.   
  65. }  
  66.   
  67. </fileitem>  

PJProgressListener 代码:

[java] view plain copy
  1. package com.van.utils.upload.listener;  
  2. import javax.servlet.http.HttpSession;  
  3. import org.apache.commons.fileupload.ProgressListener;  
  4. import com.van.utils.upload.entity.ProgressEntity;  
  5.   
  6. public class PJProgressListener implements ProgressListener{  
  7.     private HttpSession session;  
  8.     public PJProgressListener() {  
  9.     }  
  10.     public PJProgressListener(HttpSession _session) {  
  11.         session=_session;  
  12.         ProgressEntity ps = new ProgressEntity();  
  13.         session.setAttribute("upload_ps", ps);  
  14.     }  
  15.     public void update(long pBytesRead, long pContentLength, int pItems) {  
  16.         ProgressEntity ps = (ProgressEntity) session.getAttribute("upload_ps");  
  17.         ps.setpBytesRead(pBytesRead);  
  18.         ps.setpContentLength(pContentLength);  
  19.         ps.setpItems(pItems);  
  20.         //更新  
  21.         session.setAttribute("upload_ps", ps);  
  22.     }  
  23.   
  24. }  

上传进度信息记录实体类:

[java] view plain copy
  1. package com.van.utils.upload.entity;  
  2.   
  3. /** 
  4.  * 文件上传进度信息 
  5.  * @author Van 
  6.  * 
  7.  */  
  8. public class ProgressEntity {  
  9.       
  10.       
  11.     private long pBytesRead = 0L;  
  12.     private long pContentLength = 0L;  
  13.     private int pItems;  
  14.     public long getpBytesRead() {  
  15.         return pBytesRead;  
  16.     }  
  17.     public void setpBytesRead(long pBytesRead) {  
  18.         this.pBytesRead = pBytesRead;  
  19.     }  
  20.     public long getpContentLength() {  
  21.         return pContentLength;  
  22.     }  
  23.     public void setpContentLength(long pContentLength) {  
  24.         this.pContentLength = pContentLength;  
  25.     }  
  26.     public int getpItems() {  
  27.         return pItems;  
  28.     }  
  29.     public void setpItems(int pItems) {  
  30.         this.pItems = pItems;  
  31.     }  
  32.     @Override  
  33.     public String toString() {  
  34.         return "ProgressEntity [pBytesRead=" + pBytesRead + ", pContentLength="  
  35.                 + pContentLength + ", pItems=" + pItems + "]";  
  36.     }  
  37.       
  38.       
  39.       
  40.   
  41. }  

配置文件部分修改:

[java] view plain copy
  1. <!-- 文件上传支持  -->  
  2.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  3.         <property name="defaultEncoding" value="UTF-8">  
  4.         <!-- 内存缓存1M -->  
  5.         <property name="maxInMemorySize" value="1000000"></property>  
  6.         <!-- 最大2GB -->  
  7.         <property name="maxUploadSize" value="2000000000"></property>  
  8.     </property></bean>  
  9.   
  10. 将org.springframework.web.multipart.commons.CommonsMultipartResolver替换成:com.van.utils.upload.handle.PJCommonsMultipartResolver  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值