??????????????????????????????????????????????????????????????????????????spring mvc 文件上傳??
一、單文件上傳
配置步驟:
步驟一、在配置文件中配置包掃描器(暫且這樣配,會出問題,我們下面說解決方案)
?
<?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:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd "><!--讓spring掃描包下所有的類,讓標注spring注解的類生效 --><context:component-scan base-package="cn.hmy.controller"/></beans>
?
步驟二,定制我們的文件上傳jsp頁面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>文件上傳</title></head><body><form action="${pageContext.request.contextPath }/list.do" method="post" enctype="multipart/form-data"><h1>文件上傳</h1>文件:<input type="file" name="fileUpLoad"/></br><input type="submit" value="上傳"/> </form></body> </html>
步驟三、書寫我們的處理器代碼
package cn.hmy.controller;import java.io.File; import java.io.IOException;import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.validation.annotation.Validated;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView;import cn.hmy.pojo.UserInfo;@Controller public class MyController{//處理器方法@RequestMapping(value="/list.do") public void doFirst(MultipartFile fileUpLoad,HttpSession session) throws Exception{//1.獲取文件名稱String filename = fileUpLoad.getOriginalFilename();//2.獲取文件的前半部分路徑String realPath = session.getServletContext().getRealPath("/images");//3.拼接成完整的路徑File file=new File(realPath,filename);//4.保存文件 fileUpLoad.transferTo(file); } }
此時我們啟動服務器,進行代碼上傳,會報如下錯誤
?
解決方案:更改我們的spring-servlet.xml配置文件
<?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:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd "><!--讓spring掃描包下所有的類,讓標注spring注解的類生效 --><context:component-scan base-package="cn.hmy.controller"/>
<!--配置復雜類型表達解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean></beans>
啟動發現還是會報上述錯誤?
解決方案:配置注解驅動
<?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:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd "><!--讓spring掃描包下所有的類,讓標注spring注解的類生效 --><context:component-scan base-package="cn.hmy.controller"/>
<!--配置復雜類型表達解析器--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
<!--配置注解驅動-->
<mvc:annotation-driven/> </beans>
如果在上述配置文件中缺少復雜類型解析器,會報如下錯誤
?
?
在解決了以上錯誤后,我們會發現如果我們上傳的文件中含有中文?? 會出現亂碼現象
解決亂碼
解決方案我們暫且提供以下兩種方式
方案一、
方案二、在web.xml中配置
<filter><filter-name>characterEncoding</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>characterEncoding</filter-name><url-pattern>/*</url-pattern></filter-mapping>
?
如何控制文件上傳大小
在spring-servlet.xml中配置如下
maxUploadSize為上傳的總文件的大小 5M
maxInMemorySize為上傳的單個文件的大小 1M
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="defaultEncoding" value="utf-8"></property><property name="maxUploadSize" value="5000000"></property><property name="maxInMemorySize" value="1000000"></property></bean>
如果超出所限制的大小? 會報如下錯誤
?
?
控制文件上傳的類型(通過后綴名進行控制)在處理器中進行判定
例如 只允許上傳.jpg?? .png?? .gif為后綴的文件
package cn.hmy.controller;import java.io.File; import java.io.IOException;import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.validation.annotation.Validated;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView;import cn.hmy.pojo.UserInfo;@Controller public class MyController{//處理器方法@RequestMapping(value="/list.do") public String doFirst(MultipartFile fileUpLoad,HttpSession session) throws Exception{//1.獲取文件名稱String filename = fileUpLoad.getOriginalFilename();//限定文件上傳的類型if(filename.endsWith("jpg")||filename.endsWith("png")||filename.endsWith("gif")){//2.獲取文件的前半部分路徑String realPath = session.getServletContext().getRealPath("/images");//3.拼接成完整的路徑File file=new File(realPath,filename);//4.保存文件 fileUpLoad.transferTo(file); }else{System.out.println("不支持上傳文件的類型");}return "/list.jsp";} }
?
?
?
如何判斷用戶沒用選上傳文件
?
?
多文件上傳
步驟一、
spring-servlet.xml配置文件
<?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:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation=" http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd "><!--讓spring掃描包下所有的類,讓標注spring注解的類生效 --><context:component-scan base-package="cn.hmy.controller"/><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="defaultEncoding" value="utf-8"></property><property name="maxUploadSize" value="5000000"></property><property name="maxInMemorySize" value="1000000"></property></bean><mvc:annotation-driven/> </beans>
?
步驟二、準備多文件上傳的jsp頁面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>文件上傳</title></head><body><form action="${pageContext.request.contextPath }/list.do" method="post" enctype="multipart/form-data"><h1>文件上傳</h1>文件1:<input type="file" name="fileUpLoad"/></br>文件2:<input type="file" name="fileUpLoad"/></br>文件3:<input type="file" name="fileUpLoad"/></br><input type="submit" value="上傳"/> </form></body> </html>
步驟三、編寫處理器的代碼
//多文件上傳@RequestMapping(value="/list.do") public String doFirst2(@RequestParam MultipartFile[] fileUpLoad,HttpSession session) throws Exception{for (MultipartFile item : fileUpLoad) {//1.獲取文件名稱String filename = item.getOriginalFilename();//限定文件上傳的類型if(filename.endsWith("jpg")||filename.endsWith("png")||filename.endsWith("gif")){//2.獲取文件的前半部分路徑String realPath = session.getServletContext().getRealPath("/images");//3.拼接成完整的路徑File file=new File(realPath,filename);//4.保存文件 item.transferTo(file); }else{System.out.println("不支持上傳文件的類型");}}return "/list.jsp";}
注意:在處理器方法中一定要對參數進行校對使用注解@RequestParam校正參數
丟到? 會報錯
即可實現多文件上傳
?