首先我們來看下Spring mvc 中檔案上傳的配置
- <bean id="multipartResolver"
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <property name="defaultEncoding" value="UTF-8" />
- <property name="maxUploadSize" value="2000000000" />
- </bean>
再來看看Controller中使用
- 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();
- }
- }
方式二
- 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這個轉換器裡面下面再來看看它的原始碼
他的轉換器裡面就是呼叫common-fileupload的方式解析,然後再使用parseFileItems()方法封裝成自己的檔案物件 .
- List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);
大家應該發現 了上面的這句程式碼,已經使用過fileUpload解析過request了,你在Controller裡面接收到的request都已經是解析過的,你再 次使用upload進行解析獲取到的肯定是空,這個就是問題的所在(大家可以在servlet裡面實驗,看看第二次解析後能不能獲取到資料,當然是不能 的)
解決方案
1)刪除Spring MVC檔案上傳配置
- <!--
- <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提供的兩種方法是不能用的,所有上傳的地方都需要自己做處理)
- 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方法
- 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);
- }
- }
- }
監聽器方法
- 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前段可以使用假的進度條或者是上面的兩種方式.