首先咱們來看下Spring mvc 中文件上傳的配置java
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8" /> <property name="maxUploadSize" value="2000000000" /> </bean>
再來看看Controller中使用web
public void upload2(HttpServletRequest request) { // 轉型爲MultipartHttpRequest try { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; List<MultipartFile> fileList = multipartRequest.getFiles("file"); for (MultipartFile mf : fileList) { if(!mf.isEmpty()){ } } } catch (Exception e) { e.printStackTrace(); } }
方式二spring
public String upload(HttpServletRequest request, @RequestParam(value = "file") MultipartFile[] files) { try { for (MultipartFile mf : files) { if(!mf.isEmpty()){ } } } catch (Exception e) { e.printStackTrace(); } return "upload"; }
這裏springMVC 都爲咱們封裝好成本身的文件對象了,轉換的過程就在咱們所配置的CommonsMultipartResolver這個轉換器裏面下面再來看看它的源碼apache
他的轉換器裏面就是調用common-fileupload的方式解析,而後再使用parseFileItems()方法封裝成本身的文件對象 . mvc
List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);
你們應該發現了上面的這句代碼,已經使用過fileUpload解析過request了,你在Controller裏面接收到的request都已是解析過的,你再次使用upload進行解析獲取到的確定是空,這個就是問題的所在(你們能夠在servlet裏面實驗,看看第二次解析後能不能獲取到數據,固然是不能的)ide
1)刪除Spring MVC文件上傳配置spa
<!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8" /> <property name="maxUploadSize" value="2000000000" /> </bean> -->
在控制器裏面本身完成request的解析(固然上面spring MVC提供的兩種方法是不能用的,全部上傳的地方都須要本身作處理)code
public void upload3(HttpServletRequest request) { DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { List<FileItem> list = upload.parseRequest(request); for(FileItem item : list){ if(item.isFormField()){ }else{ //item.write(new File("")); } } } catch (FileUploadException e) { e.printStackTrace(); } }
2)若是是須要使用的ProgressListener監聽器咱們能夠重寫 CommonsMultipartResolver的parseRequest方法orm
package com.lwp.spring.ext; 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.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.multipart.MultipartException; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import com.lwp.listener.FileUploadListener; public class CommonsMultipartResolverExt extends CommonsMultipartResolver { @Override protected MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException { FileUploadListener listener = new FileUploadListener(); String encoding = determineEncoding(request); FileUpload fileUpload = prepareFileUpload(encoding); fileUpload.setProgressListener(listener); 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); } } }
監聽器方法 xml
import org.apache.commons.fileupload.ProgressListener; public class FileUploadListener implements ProgressListener { @Override public void update(long arg0, long arg1, int arg2) { //arg0 已經上傳多少字節 //arg1 一共多少字節 //arg2 正在上傳第幾個文件 System.out.println(arg0 +"\t" + arg1 +"\t" + arg2); } }
配置文件改成咱們本身的(這種方式的缺陷是,全部文件上傳都須要使用到Listener)
<bean id="multipartResolver" class="com.lwp.spring.ext.CommonsMultipartResolverExt"> <property name="defaultEncoding" value="UTF-8" /> <property name="maxUploadSize" value="2000000000" /> </bean>
注: 綜上所述,若是隻是普通的文件上傳spring MVC 徹底能夠完成,若是須要使用進度條的listener前段能夠使用假的進度條或者是上面的兩種方式.