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

首頁(yè) > 開(kāi)發(fā) > Java > 正文

MultipartResolver實(shí)現(xiàn)文件上傳功能

2024-07-14 08:41:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

springMVC默認(rèn)的解析器里面是沒(méi)有加入對(duì)文件上傳的解析的,,使用springmvc對(duì)文件上傳的解析器來(lái)處理文件上傳的時(shí)需要用springmvc提供的MultipartResolver的申明,又因?yàn)镃ommonsMultipartResolver實(shí)現(xiàn)了MultipartResolver接口,所以我們可以在springmvc配置文件中這樣配置:

 <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> 

 首先引入文件上傳所需要的包,commons-logging-*.jar commons-io-*.jar  commons-fileupload-*.jar

新建一個(gè)JSP頁(yè)面.

<%@ page language="java" contentType="text/html; charset=UTF-8"   pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>文件上傳</title> </head> <body>   <%--<form action="user/fileUpload" method="post" enctype="multipart/form-data">--%>   <form action="user/fileUpload" method="post" enctype="multipart/form-data">     <input type="file" name="fileUpload" />     <input type="submit" value="上傳" />   </form> </body> </html> 

springmvc上傳文件的形式有很多,這里我介紹兩種.

第一種,看Controller

package gd.hz.springmvc.controller;  import java.io.File; import java.io.IOException;  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.commons.CommonsMultipartFile; import org.springframework.web.servlet.ModelAndView;  @Controller("userController") @RequestMapping("user") public class UserController {    // 處理文件上傳一   @RequestMapping(value = "fileUpload", method = RequestMethod.POST)   public ModelAndView fileUpload(       @RequestParam("fileUpload") CommonsMultipartFile file) {     // 獲取文件類型     System.out.println(file.getContentType());     // 獲取文件大小     System.out.println(file.getSize());     // 獲取文件名稱     System.out.println(file.getOriginalFilename());      // 判斷文件是否存在     if (!file.isEmpty()) {       String path = "D:/" + file.getOriginalFilename();       File localFile = new File(path);       try {         file.transferTo(localFile);       } catch (IllegalStateException e) {         e.printStackTrace();       } catch (IOException e) {         e.printStackTrace();       }     }     return new ModelAndView("dataSuccess");   } } 

類CommonsMultipartFile為我們提供了許多對(duì)文件處理的方法.例如文件大小,上傳文件名稱,文件類型,具體用法可以查看spring的文檔.transferTo就是將文件輸出到指定地方. 

文件上傳的第二種方法,這種方法比較常用:

package gd.hz.springmvc.controller;  import java.io.File; import java.io.IOException; import java.util.Iterator;  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.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartResolver;  @Controller("userController") @RequestMapping("user") public class UserController {    // 處理文件上傳二   @RequestMapping(value = "fileUpload2", method = RequestMethod.POST)   public String fileUpload2(HttpServletRequest request)       throws IllegalStateException, IOException {     // 設(shè)置上下方文     CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(         request.getSession().getServletContext());      // 檢查form是否有enctype="multipart/form-data"     if (multipartResolver.isMultipart(request)) {       MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;        Iterator<String> iter = multiRequest.getFileNames();       while (iter.hasNext()) {          // 由CommonsMultipartFile繼承而來(lái),擁有上面的方法.         MultipartFile file = multiRequest.getFile(iter.next());         if (file != null) {           String fileName = "demoUpload" + file.getOriginalFilename();           String path = "D:/" + fileName;            File localFile = new File(path);           file.transferTo(localFile);         }        }     }     return "dataSuccess";   } } 

 MultipartHttpServletRequest提供了更加靈活的方法,可以獲取多個(gè)文件和文件名,可以遍歷獲得每個(gè)文件.

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 绿春县| 林周县| 尼玛县| 亳州市| 无极县| 三门县| 栖霞市| 南阳市| 祁东县| 白银市| 镇宁| 剑阁县| 家居| 阳谷县| 徐汇区| 营口市| 怀仁县| 印江| 松原市| 和政县| 曲水县| 崇文区| 安远县| 大方县| 德格县| 富川| 呼伦贝尔市| 清远市| 特克斯县| 伊春市| 芮城县| 阿拉善盟| 开鲁县| 如皋市| 淄博市| 新余市| 江北区| 闽侯县| 余庆县| 板桥市| 盐山县|