Spring 实现文件上传的进度条

1、实现org.apache.commons.fileupload.ProgressListener接口

import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.ProgressListener;
import org.springframework.stereotype.Component;

import com.chinalife.cip.web.multipart.model.Progress;

@Component
public class FileUploadProgressListener implements ProgressListener {
	private HttpSession session;

	public void setSession(HttpSession session){
		this.session=session;
		Progress status = new Progress();
		session.setAttribute("status", status);
	}

	/*
	 * pBytesRead 到目前为止读取文件的比特数 pContentLength 文件总大小 pItems 目前正在读取第几个文件
	 */
	public void update(long pBytesRead, long pContentLength, int pItems) {
		Progress status = (Progress) session.getAttribute("status");
		status.setpBytesRead(pBytesRead);
		status.setpContentLength(pContentLength);
		status.setpItems(pItems);
	}

}

  

2、扩展org.springframework.web.multipart.commons.CommonsMultipartResolver类

      重写public MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException方法

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
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.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.chinalife.cip.web.multipart.listener.FileUploadProgressListener;

public class CustomMultipartResolver extends CommonsMultipartResolver {
	@Autowired
	private FileUploadProgressListener progressListener;
	
	public void setFileUploadProgressListener(FileUploadProgressListener progressListener){
		this.progressListener=progressListener;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public MultipartParsingResult parseRequest(HttpServletRequest request)
			throws MultipartException {
		String encoding = determineEncoding(request);
		FileUpload fileUpload = prepareFileUpload(encoding);
		progressListener.setSession(request.getSession());
		fileUpload.setProgressListener(progressListener);
		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);
		}
	}
} 

  

  1. <bean id="multipartResolver" class="com.chinalife.cip.web.multipart.CustomMultipartResolver">  
  2.     <property name="maxUploadSize" value="50000000" />  
  3. </bean>  

 

 

  1. 文件上传controller编写
  2. @RequestMapping(...)  
  3. public String uploadFile(@RequestParam(value = "file") MultipartFile... files) throws IOException {  
  4.     for (MultipartFile f : files) {  
  5.         if (f.getSize() > 0) {  
  6.             File targetFile = new File("目标文件路径及文件名");  
  7.             f.transferTo(targetFile);//写入目标文件  
  8.         }  
  9.     }  
  10.     return "...";  
  11. }    

 

  1. 文件进度controller编写

  2. import java.util.Map;  
  3.   
  4. import org.springframework.stereotype.Controller;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6. import org.springframework.web.bind.annotation.RequestMethod;  
  7. import org.springframework.web.bind.annotation.ResponseBody;  
  8. import org.springframework.web.bind.annotation.SessionAttributes;  
  9.   
  10. import com.chinalife.cip.web.multipart.model.Progress;  
  11.   
  12. @Controller  
  13. @SessionAttributes("status")  
  14. public class ProgressController {  
  15.   
  16.     @RequestMapping(value = "/upfile/progress"method = RequestMethod.POST )  
  17.     @ResponseBody  
  18.     public String initCreateInfo(Map<String, Object> model) {  
  19.         Progress status = (Progress) model.get("status");  
  20.         if(status==null){  
  21.             return "{}";  
  22.         }  
  23.         return status.toString();  
  24.     }  
  25. }  

 

 

 

 

  1. import javax.servlet.http.HttpSession;  
  2.   
  3. import org.apache.commons.fileupload.ProgressListener;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. import com.chinalife.cip.web.multipart.model.Progress;  
  7.   
  8. @Component  
  9. public class FileUploadProgressListener implements ProgressListener {  
  10.     private HttpSession session;  
  11.   
  12.     public void setSession(HttpSession session){  
  13.         this.session=session;  
  14.         Progress status = new Progress();  
  15.         session.setAttribute("status", status);  
  16.     }  
  17.   
  18.     /*  
  19.      * pBytesRead 到目前为止读取文件的比特数 pContentLength 文件总大小 pItems 目前正在读取第几个文件  
  20.      */  
  21.     public void update(long pBytesRead, long pContentLength, int pItems) {  
  22.         Progress status = (Progress) session.getAttribute("status");  
  23.         status.setpBytesRead(pBytesRead);  
  24.         status.setpContentLength(pContentLength);  
  25.         status.setpItems(pItems);  
  26.     }  
  27.   
  28. }
posted @ 2017-12-21 22:26  依米熊  阅读(2686)  评论(0编辑  收藏  举报