一枣氧、簡述
一個javaWeb項(xiàng)目中网缝,文件上傳功能幾乎是必不可少的,本人在項(xiàng)目開發(fā)中也時常會遇到疫稿,以前也沒怎么去理它培他,今天有空學(xué)習(xí)了一下這方面的知識,于是便將本人學(xué)到的SpringMVC中單文件與多文件上傳這部分知識做下筆記遗座。
二舀凛、單文件上傳
1、頁面
這里以一個簡單的表單提交為例子途蒋,文件上傳需要將表單的提交方法設(shè)置為post猛遍,將enctype的值設(shè)置為"multipart/form-data"。
<form action="${pageContext.request.contextPath}/test/upload.do" method="post" enctype="multipart/form-data">
<input type="file" name="img"><br />
<input type="submit" name="提交">
</form>
2号坡、控制器
在Controller的處理方法中懊烤,使用MultipartFile對象作為參數(shù)接收前端上傳過來的文件,具體說明請看代碼注釋宽堆。
@Controller
@RequestMapping("/test")
public class MyController {
@RequestMapping(value = "/upload.do", method = RequestMethod.POST)
// 這里的MultipartFile對象變量名跟表單中的file類型的input標(biāo)簽的name相同腌紧,所以框架會自動用MultipartFile對象來接收上傳過來的文件,當(dāng)然也可以使用@RequestParam("img")指定其對應(yīng)的參數(shù)名稱
public String upload(MultipartFile img, HttpSession session)
throws Exception {
// 如果沒有文件上傳畜隶,MultipartFile也不會為null壁肋,可以通過調(diào)用getSize()方法獲取文件的大小來判斷是否有上傳文件
if (img.getSize() > 0) {
// 得到項(xiàng)目在服務(wù)器的真實(shí)根路徑,如:/home/tomcat/webapp/項(xiàng)目名/images
String path = session.getServletContext().getRealPath("images");
// 得到文件的原始名稱籽慢,如:美女.png
String fileName = img.getOriginalFilename();
// 通過文件的原始名稱浸遗,可以對上傳文件類型做限制,如:只能上傳jpg和png的圖片文件
if (fileName.endsWith("jpg") || fileName.endsWith("png")) {
File file = new File(path, fileName);
img.transferTo(file);
return "/success.jsp";
}
}
return "/error.jsp";
}
}
3箱亿、springmvc.xml配置
使用MultipartFile對象接收前端上傳過來的文件跛锌,還需要在springmvc的配置文件中進(jìn)行如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
...
<!-- 注意:CommonsMultipartResolver的id是固定不變的,一定是multipartResolver极景,不可修改 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 如果上傳后出現(xiàn)文件名中文亂碼可以使用該屬性解決 -->
<property name="defaultEncoding" value="utf-8"/>
<!-- 單位是字節(jié)察净,不設(shè)置默認(rèn)不限制總的上傳文件大小,這里設(shè)置總的上傳文件大小不超過1M(1*1024*1024) -->
<property name="maxUploadSize" value="1048576"/>
<!-- 跟maxUploadSize差不多盼樟,不過maxUploadSizePerFile是限制每個上傳文件的大小氢卡,而maxUploadSize是限制總的上傳文件大小 -->
<property name="maxUploadSizePerFile" value="1048576"/>
</bean>
<!-- 設(shè)置一個簡單的異常解析器,當(dāng)文件上傳超過大小限制時跳轉(zhuǎn) -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="/error.jsp"/>
</bean>
</beans>
上面配置文件中的CommonsMultipartResolver下的屬性值配置不是必須的晨缴,你也可以全部不寫译秦。到這里就可以實(shí)現(xiàn)單個文件上傳了,下面來看看多文件上傳。
三筑悴、多文件上傳
其實(shí)多文件上傳也很簡單们拙,單文件上傳是在Controller的處理方法中使用MultipartFile對象作為參數(shù)接收前端上傳過來的文件,而多文件上傳則使用MultipartFile對象數(shù)組來接收阁吝。
1砚婆、頁面
該頁面中有幾個name值一樣的file類型的input標(biāo)簽,其他跟單文件上傳的頁面沒差突勇。
<form action="${pageContext.request.contextPath}/test/upload.do" method="post" enctype="multipart/form-data">
file 1 : <input type="file" name="imgs"><br />
file 2 : <input type="file" name="imgs"><br />
file 3 : <input type="file" name="imgs"><br />
<input type="submit" name="提交">
</form>
2装盯、控制器
控制器中的處理方法使用MultipartFile[]數(shù)組作為接收參數(shù),并不能直接使用甲馋,需要校正參數(shù)埂奈,具體說明請看代碼注釋。
@Controller
@RequestMapping("/test")
public class MyController {
@RequestMapping(value = "/upload.do", method = RequestMethod.POST)
// 這里的MultipartFile[] imgs表示前端頁面上傳過來的多個文件定躏,imgs對應(yīng)頁面中多個file類型的input標(biāo)簽的name账磺,但框架只會將一個文件封裝進(jìn)一個MultipartFile對象,
// 并不會將多個文件封裝進(jìn)一個MultipartFile[]數(shù)組痊远,直接使用會報(bào)[Lorg.springframework.web.multipart.MultipartFile;.<init>()錯誤垮抗,
// 所以需要用@RequestParam校正參數(shù)(參數(shù)名與MultipartFile對象名一致),當(dāng)然也可以這么寫:@RequestParam("imgs") MultipartFile[] files碧聪。
public String upload(@RequestParam MultipartFile[] imgs, HttpSession session)
throws Exception {
for (MultipartFile img : imgs) {
if (img.getSize() > 0) {
String path = session.getServletContext().getRealPath("images");
String fileName = img.getOriginalFilename();
if (fileName.endsWith("jpg") || fileName.endsWith("png")) {
File file = new File(path, fileName);
img.transferTo(file);
}
}
}
return "/success.jsp";
}
}
同樣的借宵,使用MultipartFile數(shù)組接收前端上傳過來的多個文件,也需要在springmvc的配置文件進(jìn)行配置矾削,具體配置與上述單文件上傳的springmvc.xml配置沒差,直接拷貝過來就行豁护。這樣哼凯,就可以進(jìn)行多文件上傳了。
四楚里、多種文件上傳情景綜合
當(dāng)然断部,項(xiàng)目開發(fā)中,場景可能并不是這么簡單班缎,上述的多文件上傳是一個個文件選擇后一起上傳(即多個name相同的input標(biāo)簽)蝴光,那要是我項(xiàng)目中只要一個input標(biāo)簽就可以一次性多個文件呢?又或者一個頁面中既要一個個選擇的多文件上傳达址,又要一次性選擇的多文件上傳蔑祟,還要有單文件上傳呢?沒問題沉唠,MultipartFile[]通吃疆虚,代碼也很easy,下面直接上代碼。
1径簿、頁面
這里的 “一次選擇多個文件的多文件上傳” 只是在input標(biāo)簽中加上了multiple屬性而已罢屈。
<form action="${pageContext.request.contextPath}/test/upload.do" method="post" enctype="multipart/form-data">
一次選擇多個文件的多文件上傳 : <br />
<input type="file" name="imgs1" multiple><br /> <br />
一次選擇一個文件的多文件上傳 : <br />
<input type="file" name="imgs2"><br />
<input type="file" name="imgs2"><br /><br />
單文件上傳 : <br />
<input type="file" name="imgs3"><br /><br />
<input type="submit" name="提交">
</form>
2、控制器
@Controller
@RequestMapping("/test")
public class MyController {
@RequestMapping(value = "/upload.do", method = RequestMethod.POST)
public String upload(@RequestParam MultipartFile[] imgs1,@RequestParam MultipartFile[] imgs2,@RequestParam MultipartFile[] imgs3, HttpSession session)
throws Exception {
String path = session.getServletContext().getRealPath("images");
for (MultipartFile img : imgs1) {
uploadFile(path, img);
}
for (MultipartFile img : imgs2) {
uploadFile(path, img);
}
for (MultipartFile img : imgs3) {
uploadFile(path, img);
}
return "/success.jsp";
}
private void uploadFile(String path, MultipartFile img) throws IOException {
if (img.getSize() > 0) {
String fileName = img.getOriginalFilename();
if (fileName.endsWith("jpg") || fileName.endsWith("png")) {
File file = new File(path, fileName);
img.transferTo(file);
}
}
}
}
MultipartFile[]就是如此強(qiáng)大篇亭,不管單個多個缠捌,邏輯處理一樣,所以建議在項(xiàng)目開發(fā)中使用MultipartFile[]作為文件的接收參數(shù)译蒂。
五曼月、拓展
1、MultipartFile類常用的一些方法:
String getContentType()//獲取文件MIME類型
InputStream getInputStream()//獲取文件流
String getName() //獲取表單中文件組件的名字
String getOriginalFilename() //獲取上傳文件的原名
long getSize() //獲取文件的字節(jié)大小蹂随,單位byte
boolean isEmpty() //是否為空
void transferTo(File dest)
2十嘿、CommonsMultipartResolver的屬性解析
defaultEncoding:表示用來解析request請求的默認(rèn)編碼格式,當(dāng)沒有指定的時候根據(jù)Servlet規(guī)范會使用默認(rèn)值ISO-8859-1岳锁。當(dāng)request自己指明了它的編碼格式的時候就會忽略這里指定的defaultEncoding绩衷。
uploadTempDir:設(shè)置上傳文件時的臨時目錄,默認(rèn)是Servlet容器的臨時目錄激率。
maxUploadSize:設(shè)置允許上傳的總的最大文件大小咳燕,以字節(jié)為單位計(jì)算。當(dāng)設(shè)為-1時表示無限制乒躺,默認(rèn)是-1招盲。
maxUploadSizePerFile:跟maxUploadSize差不多,不過maxUploadSizePerFile是限制每個上傳文件的大小嘉冒,而maxUploadSize是限制總的上傳文件大小曹货。
maxInMemorySize:設(shè)置在文件上傳時允許寫到內(nèi)存中的最大值,以字節(jié)為單位計(jì)算讳推,默認(rèn)是10240顶籽。
resolveLazily:為true時,啟用推遲文件解析银觅,以便在UploadAction中捕獲文件大小異常礼饱。
六、注意
- 在開發(fā)過程中究驴,建議把配置文件中的異常解析器(SimpleMappingExceptionResolver)先注釋掉镊绪,方便我們查看錯誤。
- 有時候上傳出錯洒忧,是因?yàn)槲覀冊谂渲梦募邢拗屏松蟼魑募拇笮『拢憧梢圆患舆@個限制,但個人建議這個限制最好還是加上跑慕,具體文件大小限制請根據(jù)公司項(xiàng)目情況而定万皿。
- SpringMVC中使用MultipartFile接收上傳文件需要依賴兩個jar包摧找,分別是:commons-fileupload-1.3.3.jar、commons-io-2.5.jar牢硅。