開始需要在pom.xml加入幾個jar,分别是
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
接下來,在Springmvc的配置加入上傳檔案的配置(PS:我把springmvc的完整配置都展現出來):
一、 單檔案上傳
當然在一個表單中,需要添加enctype="multipart/form-data",一個表單有檔案域,肯定也有基本的文本框,可以一次性送出,springmvc能給我們差別出來,來做不同的處理。首先看下普通的model
package com.ztz.springmvc.model;public classUsers {privateString name;privateString password;//省略get set方法//重寫toString()友善測試
@OverridepublicString toString() {return "Users [name=" + name + ", password=" + password + "]";
}
}
這個是表單的JSP頁面:
String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request.setAttribute("basePath", basePath);%>
FileUpload
使用者名:
密 碼:
頭 像
上傳成功跳轉的JSP頁面,并且顯示出上傳圖檔:
String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request.setAttribute("basePath", basePath);%>
頭像
最後是Controller:
package com.ztz.springmvc.controller;
import java.io.File;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.ztz.springmvc.model.Users;
@Controller
@RequestMapping("/file")public classFileUploadController {
@RequestMapping(value="/upload",method=RequestMethod.POST)private String fildUpload(Users users ,@RequestParam(value="file",required=false) MultipartFile file,
HttpServletRequest request)throws Exception{//基本表單
System.out.println(users.toString());//獲得實體路徑webapp所在路徑
String pathRoot = request.getSession().getServletContext().getRealPath("");
String path="";if(!file.isEmpty()){//生成uuid作為檔案名稱
String uuid = UUID.randomUUID().toString().replaceAll("-","");//獲得檔案類型(可以判斷如果不是圖檔,禁止上傳)
String contentType=file.getContentType();//獲得檔案字尾名稱
String imageName=contentType.substring(contentType.indexOf("/")+1);
path="/static/images/"+uuid+"."+imageName;
file.transferTo(new File(pathRoot+path));
}
System.out.println(path);
request.setAttribute("imagesPath", path);return "success";
}//因為我的JSP在WEB-INF目錄下面,浏覽器無法直接通路
@RequestMapping(value="/forward")privateString forward(){return "index";
}
}
點選送出控制台輸出:
Users [name=fileupload, password=test]
二、 多圖檔上傳
springmvc實作多圖檔上傳也很簡單,我們把剛才的例子修改下,在加一個檔案域,name的值還是相同
使用者名:
密 碼:
頭 像1
頭 像2
展示圖檔來個循環,以便顯示多張圖檔
控制層代碼如下:
package com.ztz.springmvc.controller;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.ztz.springmvc.model.Users;
@Controller
@RequestMapping("/file")public classFileUploadController {
@RequestMapping(value="/upload",method=RequestMethod.POST)private String fildUpload(Users users ,@RequestParam(value="file",required=false) MultipartFile[] file,
HttpServletRequest request)throws Exception{//基本表單
System.out.println(users.toString());//獲得實體路徑webapp所在路徑
String pathRoot = request.getSession().getServletContext().getRealPath("");
String path="";
List listImagePath=new ArrayList();for(MultipartFile mf : file) {if(!mf.isEmpty()){//生成uuid作為檔案名稱
String uuid = UUID.randomUUID().toString().replaceAll("-","");//獲得檔案類型(可以判斷如果不是圖檔,禁止上傳)
String contentType=mf.getContentType();//獲得檔案字尾名稱
String imageName=contentType.substring(contentType.indexOf("/")+1);
path="/static/images/"+uuid+"."+imageName;
mf.transferTo(new File(pathRoot+path));
listImagePath.add(path);
}
}
System.out.println(path);
request.setAttribute("imagesPathList", listImagePath);return "success";
}//因為我的JSP在WEB-INF目錄下面,浏覽器無法直接通路
@RequestMapping(value="/forward")privateString forward(){return "index";
}
}