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); } } }
- <bean id="multipartResolver" class="com.chinalife.cip.web.multipart.CustomMultipartResolver">
- <property name="maxUploadSize" value="50000000" />
- </bean>
- 文件上传controller编写
- @RequestMapping(...)
- public String uploadFile(@RequestParam(value = "file") MultipartFile... files) throws IOException {
- for (MultipartFile f : files) {
- if (f.getSize() > 0) {
- File targetFile = new File("目标文件路径及文件名");
- f.transferTo(targetFile);//写入目标文件
- }
- }
- return "...";
- }
- 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);
- }
- }