SpringMVC 單文件上傳與多文件上傳實(shí)例
一、簡述
一個javaWeb項目中,文件上傳功能幾乎是必不可少的,本人在項目開發(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) { // 得到項目在服務(wù)器的真實(shí)根路徑,如:/home/tomcat/webapp/項目名/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ù)組,直接使用會報[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)然,項目開發(fā)中,場景可能并不是這么簡單,上述的多文件上傳是一個個文件選擇后一起上傳(即多個name相同的input標(biāo)簽),那要是我項目中只要一個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)大,不管單個多個,邏輯處理一樣,所以建議在項目開發(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é)為單位計算。當(dāng)設(shè)為-1時表示無限制,默認(rèn)是-1。
- maxUploadSizePerFile:跟maxUploadSize差不多,不過maxUploadSizePerFile是限制每個上傳文件的大小,而maxUploadSize是限制總的上傳文件大小。
- maxInMemorySize:設(shè)置在文件上傳時允許寫到內(nèi)存中的最大值,以字節(jié)為單位計算,默認(rèn)是10240。
- resolveLazily:為true時,啟用推遲文件解析,以便在UploadAction中捕獲文件大小異常。
六、注意
- 在開發(fā)過程中,建議把配置文件中的異常解析器(SimpleMappingExceptionResolver)先注釋掉,方便我們查看錯誤。
- 有時候上傳出錯,是因?yàn)槲覀冊谂渲梦募邢拗屏松蟼魑募拇笮。憧梢圆患舆@個限制,但個人建議這個限制最好還是加上,具體文件大小限制請根據(jù)公司項目情況而定。
- SpringMVC中使用MultipartFile接收上傳文件需要依賴兩個jar包,分別是:commons-fileupload-1.3.3.jar、commons-io-2.5.jar。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring之底層架構(gòu)核心概念Environment及用法詳解
這篇文章主要介紹了Spring之底層架構(gòu)核心概念-Environment,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12SpringBoot war包部署到Tomcat服務(wù)器
這篇文章主要介紹了SpringBoot war包部署到Tomcat服務(wù)器,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03Java 實(shí)戰(zhàn)練手項目之醫(yī)院預(yù)約掛號系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+Maven+Vue+mysql實(shí)現(xiàn)一個醫(yī)院預(yù)約掛號系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11java求最大公約數(shù)與最小公倍數(shù)的方法示例
這篇文章主要介紹了java求最大公約數(shù)與最小公倍數(shù)的方法,涉及java數(shù)值運(yùn)算的相關(guān)操作技巧,并附帶分析了eclipse環(huán)境下設(shè)置運(yùn)行輸入?yún)?shù)的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Spring MVC學(xué)習(xí)之DispatcherServlet請求處理詳析
這篇文章主要給大家介紹了關(guān)于Spring MVC學(xué)習(xí)教程之DispatcherServlet請求處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11Java8日期類LocalDate、LocalTime和LocalDateTime使用方法詳解
這篇文章主要給大家介紹了關(guān)于Java8日期類LocalDate、LocalTime和LocalDateTime使用方法的相關(guān)資料,LocalDateTime是JDK1.8出現(xiàn)的新特性,解決線程不安全的問題,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11解決Aop @AfterReturning因返回類型不一致導(dǎo)致無法執(zhí)行切面代碼
這篇文章主要介紹了解決Aop @AfterReturning因返回類型不一致導(dǎo)致無法執(zhí)行切面代碼問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07