SpringMVC上傳文件的兩種方法
在該示例中,闡述了SpringMVC如何上傳文件。
1、上傳頁面upload.jsp
<body> <form action="/TestSpringMVC3/data/uploadfile" enctype="multipart/form-data" method="post"> file:<input type="file" name="file"><br> <input type="submit" value="upload file"> </form> </body>
2、controller配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 使Spring支持自動檢測組件,如注解的Controller --> <context:component-scan base-package="cn.com.yy.controller"/> <!-- 開啟注解配置 --> <mvc:annotation-driven/> <!-- 支持JSP JSTL的解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 配置文件上傳解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"/> <property name="maxUploadSize" value="10485760000"/> <property name="maxInMemorySize" value="40960"/> </bean> </beans>
主要是添加了文件上傳的解析器,配置了默認(rèn)編碼,最大的上傳大小以及緩存大小等參數(shù)。
3、Controller
package cn.com.yy.controller; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.commons.CommonsMultipartFile; @Controller @RequestMapping("/data") public class FileUploadController { /** * method1:通過參數(shù)CommonsMultipartFile進(jìn)行解析 * @RequestParam("file")中的file對應(yīng)于upload.jsp中的file類型的name對應(yīng)的名稱 * @param file * @return * @throws IOException */ @RequestMapping(value="/uploadfile") public String upload1(@RequestParam("file") CommonsMultipartFile file) throws IOException{ //獲取文件名稱 String fileName = file.getOriginalFilename(); //寫入本地磁盤 InputStream is = file.getInputStream(); byte[] bs = new byte[1024]; int len; OutputStream os = new FileOutputStream(new File("D:/temp/" + fileName)); while ((len = is.read(bs)) != -1) { os.write(bs, 0, len); } os.close(); is.close(); return "upload_success"; } @RequestMapping("/upload") public String toPage(){ return "upload"; } }
4、返回頁面upload_success.jsp
<body> upload file success!! </body>
5、測試
訪問 http://localhost:8080/TestSpringMVC3/data/upload 跳轉(zhuǎn)到上傳頁面
選擇文件上傳
點擊上傳會跳轉(zhuǎn)到上傳成功頁面。
上述方法只是簡單的講解了SpringMVC如何上傳文件。
第二種方法:使用SpringMVC封裝的方法進(jìn)行文件上傳
/** * 使用SpringMVC封裝好的方法進(jìn)行文件上傳 * @param request * @param response * @throws IllegalStateException * @throws IOException */ @RequestMapping("/uploadfile2") public void upload2(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException{ //獲取解析器 CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext()); //判斷是否是文件 if(resolver.isMultipart(request)){ //進(jìn)行轉(zhuǎn)換 MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)(request); //獲取所有文件名稱 Iterator<String> it = multiRequest.getFileNames(); while(it.hasNext()){ //根據(jù)文件名稱取文件 MultipartFile file = multiRequest.getFile(it.next()); String fileName = file.getOriginalFilename(); String localPath = "D:/temp/" + fileName; File newFile = new File(localPath); //上傳的文件寫入到指定的文件中 file.transferTo(newFile); } } }
該方法上傳文件效率更優(yōu)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Hibernate實現(xiàn)many-to-many的映射關(guān)系
今天小編就為大家分享一篇關(guān)于Hibernate實現(xiàn)many-to-many的映射關(guān)系,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03java集合類ArrayList和Vector的區(qū)別面試精講
這篇文章主要為大家介紹了java集合類ArrayList和Vector的區(qū)別面試全面講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10Spring Cloud Gateway全局異常處理的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring Cloud Gateway全局異常處理的相關(guān)資料,需要的朋友可以參考下2018-10-10IDEA 中創(chuàng)建并部署 JavaWeb 程序的方法步驟(圖文)
本文主要介紹了IDEA 中創(chuàng)建并部署 JavaWeb 程序的方法步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02Spring中allowedOriginPatterns和allowedOrigins方法有何不同詳解
這篇文章主要給大家介紹了關(guān)于Spring中allowedOriginPatterns和allowedOrigins方法有何不同,allowedOriginPatterns和allowedOrigins都是用來設(shè)置允許跨域請求的來源,需要的朋友可以參考下2023-10-10elasticsearch索引創(chuàng)建create?index集群matedata更新
這篇文章主要介紹了elasticsearch索引創(chuàng)建create?index及集群matedata更新,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04MyBatis-Plus 動態(tài)表名SQL解析器的實現(xiàn)
這篇文章主要介紹了MyBatis-Plus 動態(tài)表名SQL解析器的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08