国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

關于SpringMVC的文件上傳

2019-11-14 22:12:33
字體:
來源:轉載
供稿:網友
關于SPRingMVC的文件上傳

關于文件的上傳,之前寫過2篇文章,基于Struts2框架,下面給出文章鏈接:

《關于Struts2的文件上傳》:http://m.survivalescaperooms.com/lichenwei/p/3927964.html

《關于Struts2的多文件上傳》:http://m.survivalescaperooms.com/lichenwei/p/3928200.html

其實文件上傳的原理都是一樣的,基于SpringMVC的文件上傳實現要比Struts2要來得簡單許多。

好了,廢話不多說,直接切入主題吧,關于上傳原理不了解的朋友,可以輕戳上面2篇文章的鏈接。

1、萬變不離其宗,要實現文件的上傳需要對應的JAR包:

1、commons-fileupload-1.2.2.jar

2、commons-io-2.0.1.jar

2、要實現SpringMVC的文件上傳,需要配置一下文件:

 1     <!-- SpringMVC上傳文件時,需要配置MultipartResolver處理器 --> 2     <bean id="multipartResolver" 3         class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 4         <property name="defaultEncoding" value="UTF-8" /> 5         <!-- 指定所上傳文件的總大小不能超過200KB。注意maxUploadSize屬性的限制不是針對單個文件,而是所有文件的容量之和 --> 6         <property name="maxUploadSize" value="-1" /> 7     </bean> 8  9     <!-- SpringMVC在超出上傳文件限制時,會拋出org.springframework.web.multipart.MaxUploadSizeExceededException -->10     <!-- 該異常是SpringMVC在檢查上傳的文件信息時拋出來的,而且此時還沒有進入到Controller方法中 -->11     <bean id="exceptionResolver"12         class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">13         <property name="exceptionMappings">14             <props>15                 <!-- 遇到MaxUploadSizeExceededException異常時,自動跳轉到XXX頁面 -->16                 <prop17                     key="org.springframework.web.multipart.MaxUploadSizeExceededException">跳轉XXX頁面</prop>18             </props>19         </property>20     </bean>

3、上傳頁面

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <% 4     String path = request.getContextPath(); 5     String basePath = request.getScheme() + "://" 6             + request.getServerName() + ":" + request.getServerPort() 7             + path + "/"; 8 %> 9 10 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">11 <html>12 <head>13 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">14 <title>上傳文件</title>15 </head>16 <body>17     <form action="<%=basePath%>upload.do" method="post"18         enctype="multipart/form-data">19         <input type="hidden" name="tuzi" value="tuzi">20         上傳文件:<input type="file" name="uploadfile">21          <input type="submit" value="上傳">22     </form>23 </body>24 </html>

4、文件處理類:

 1 package lcw.controller; 2  3 import java.io.File; 4 import java.io.IOException; 5  6 import javax.servlet.http.HttpServletRequest; 7  8 import org.apache.commons.io.FileUtils; 9 import org.springframework.stereotype.Controller;10 import org.springframework.web.bind.annotation.RequestMapping;11 import org.springframework.web.bind.annotation.RequestParam;12 import org.springframework.web.multipart.commons.CommonsMultipartFile;13 14 /**15  * 16  * 文件上傳處理類17  *18  */19 @Controller20 public class FileController {21 22     //單文件上傳23     @RequestMapping(value = "/upload.do")24     public String queryFileData(25             @RequestParam("uploadfile") CommonsMultipartFile file,26             HttpServletRequest request) {27         // MultipartFile是對當前上傳的文件的封裝,當要同時上傳多個文件時,可以給定多個MultipartFile參數(數組)28         if (!file.isEmpty()) {29             String type = file.getOriginalFilename().substring(30                     file.getOriginalFilename().indexOf("."));// 取文件格式后綴名31             String filename = System.currentTimeMillis() + type;// 取當前時間戳作為文件名32             String path = request.getsession().getServletContext()33                     .getRealPath("/upload/" + filename);// 存放位置34             File destFile = new File(path);35             try {36                 // FileUtils.copyInputStreamToFile()這個方法里對IO進行了自動操作,不需要額外的再去關閉IO流37                 FileUtils38                         .copyInputStreamToFile(file.getInputStream(), destFile);// 復制臨時文件到指定目錄下39             } catch (IOException e) {40                 e.printStackTrace();41             }42             return "redirect:upload_ok.jsp";43         } else {44             return "redirect:upload_error.jsp";45         }46     }47 }

5、看一下實現效果圖:

6、再來看下關于多文件上傳,其實原理還是一樣,只不過是把CommonsMultipartFile類對象換成一個數組,然后用一個for循環去遍歷這個數組,并分別存入。

 1 package lcw.controller; 2  3 import java.io.File; 4 import java.io.IOException; 5  6 import javax.servlet.http.HttpServletRequest; 7  8 import org.apache.commons.io.FileUtils; 9 import org.springframework.stereotype.Controller;10 import org.springframework.web.bind.annotation.RequestMapping;11 import org.springframework.web.bind.annotation.RequestParam;12 import org.springframework.web.multipart.commons.CommonsMultipartFile;13 14 /**15  * 16  * 文件上傳處理類17  *18  */19 @Controller20 public class FileController {21 22     //單文件上傳23     @RequestMapping(value = "/upload.do")24     public String queryFileData(25             @RequestParam("uploadfile") CommonsMultipartFile file,26             HttpServletRequest request) {27         // MultipartFile是對當前上傳的文件的封裝,當要同時上傳多個文件時,可以給定多個MultipartFile參數(數組)28         if (!file.isEmpty()) {29             String type = file.getOriginalFilename().substring(30                     file.getOriginalFilename().indexOf("."));// 取文件格式后綴名31             String filename = System.currentTimeMillis() + type;// 取當前時間戳作為文件名32             String path = request.getSession().getServletContext()33                     .getRealPath("/upload/" + filename);// 存放位置34             File destFile = new File(path);35             try {36                 // FileUtils.copyInputStreamToFile()這個方法里對IO進行了自動操作,不需要額外的再去關閉IO流37                 FileUtils38                         .copyInputStreamToFile(file.getInputStream(), destFile);// 復制臨時文件到指定目錄下39             } catch (IOException e) {40                 e.printStackTrace();41             }42             return "redirect:upload_ok.jsp";43         } else {44             return "redirect:upload_error.jsp";45         }46     }47 48     //多文件上傳49     @RequestMapping(value = "/uploads.do")50     public String queryFileDatas(51             @RequestParam("uploadfile") CommonsMultipartFile[] files,52             HttpServletRequest request) {53         if (files != null) {54             for (int i = 0; i < files.length; i++) {55                 String type = files[i].getOriginalFilename().substring(56                         files[i].getOriginalFilename().indexOf("."));// 取文件格式后綴名57                 String filename = System.currentTimeMillis() + type;// 取當前時間戳作為文件名58                 String path = request.getSession().getServletContext()59                         .getRealPath("/upload/" + filename);// 存放位置60                 File destFile = new File(path);61                 try {62                     FileUtils.copyInputStreamToFile(files[i].getInputStream(),63                             destFile);// 復制臨時文件到指定目錄下64                 } catch (IOException e) {65                     e.printStackTrace();66                 }67             }68             return "redirect:upload_ok.jsp";69         } else {70             return "redirect:upload_error.jsp";71         }72 73     }74 75 }

7、看下效果圖:

作者:Balla_兔子出處:http://m.survivalescaperooms.com/lichenwei/本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。正在看本人博客的這位童鞋,我看你氣度不凡,談吐間隱隱有王者之氣,日后必有一番作為!旁邊有“推薦”二字,你就順手把它點了吧,相得準,我分文不收;相不準,你也好回來找我!


上一篇:Criterion &amp; DetachedCriteria

下一篇:ActiveMQ

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 前郭尔| 墨竹工卡县| 遵义县| 建湖县| 黄龙县| 武清区| 英吉沙县| 台东市| 尼木县| 慈溪市| 莎车县| 崇义县| 潮州市| 筠连县| 吉隆县| 平遥县| 涪陵区| 中方县| 商城县| 汤原县| 哈密市| 固安县| 襄城县| 商都县| 天长市| 新民市| 揭东县| 浦县| 文安县| 大埔区| 晋城| 兴城市| 北川| 双辽市| 定州市| 麦盖提县| 镇平县| 宜兰市| 岳阳市| 琼结县| 平乡县|