SpringMVC 中,文件的上傳,是經過 MultipartResolver 實現的。 因此,若是要實現文件的上傳,只要在 spring-mvc.xml 中註冊相應的 MultipartResolver 便可。html
MultipartResolver 的實現類有兩個:java
兩個的區別:web
第一個使用步驟:spring
/*CommonsMultipartResolver 上傳用到的兩個包*/
"commons-fileupload:commons-fileupload:1.3.1",
"commons-io:commons-io:2.4"
Spring_mvc.xmlspring-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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 開啓mvc--> <mvc:annotation-driven/> <!-- 配置掃描發現全部具備 @Controller 註解的類,加載到容器 --> <context:component-scan base-package="text1"/> <bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp"/> </bean> <!--1 CommonsMultipartResolver 第一個須要使用 Apache 的 commons-fileupload 等 jar 包支持, 但它能在比較舊的 servlet 版本中使用--> <!-- SpringMVC上傳文件時,須要配置MultipartResolver處理器 --> <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>
web.xmlmvc
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:Spring_mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
imgTest .java
javaapp
package text1; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; @Controller @RequestMapping("/create") public class imgTest { @Autowired private HttpServletRequest request; @RequestMapping("/jq") public String jq() { System.out.println("oooo"); return "index"; } @RequestMapping("/upload") public String upload(MultipartFile[] mfile) throws IOException { if (mfile !=null && mfile.length>0) { for (int i = 0;i<mfile.length;i++){ long start = System.currentTimeMillis(); System.out.println("-------------------------------------------------"); System.out.println("獲取文件流"+mfile[i].getInputStream()); System.out.println("獲取文件的字節大小【byte】"+mfile[i].getSize()); System.out.println("文件類型"+mfile[i].getContentType()); System.out.println("獲取文件數據"+mfile[i].getBytes()); System.out.println("文件名字"+mfile[i].getName()); System.out.println("獲取上傳文件的原名"+mfile[i].getOriginalFilename()); System.out.println("-------------------------------------------------"); try { String filePath = request.getSession().getServletContext() .getRealPath("/") + "assets/" +start+ mfile[i].getOriginalFilename(); //轉存文件 mfile[i].transferTo(new File(filePath)); }catch (Exception e){ e.printStackTrace(); } //mfile[i].transferTo(new File("D:/ideas/"+mfile[i].getOriginalFilename()+ mfile[i].getOriginalFilename().substring( // mfile[i].getOriginalFilename().lastIndexOf(".")))); // System.out.println(mfile.getOriginalFilename()); // mfile[i].transferTo(new File("/assets" + mfile[i].getOriginalFilename())); } return "cg"; } else { System.out.println("失敗"); return "sb"; } } }
html:jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form:form action="/create/upload" enctype="multipart/form-data"> <input type="file" name="mfile" id="img" /><br> <input type="file" name="mfile" id="img2"/> <%--<img src="#" id="ser" >--%> <input type="submit" value="上傳圖片" /> </form:form> </body> </html>
第二個使用步驟: 這個就不要導包---建議用這個 ide
Spring_mvc.xmlurl
<?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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 開啓mvc--> <mvc:annotation-driven/> <!-- 配置掃描發現全部具備 @Controller 註解的類,加載到容器 --> <context:component-scan base-package="text1"/> <bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp"/> </bean> <!--2 註冊上傳 StandardServletMultipartResolver 第二個不須要第三方 jar 包支持,它使用 servlet 內置的上傳功能, 可是隻能在 Servlet 3 以上的版本使用。 --> <bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:Spring_mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <multipart-config> <!-- 上傳文件的大小限制,好比下面表示 5 M --> <max-file-size>5242880</max-file-size> <!-- 一次表單提交中文件的大小限制,必須下面表明 10 M --> <max-request-size>10485760</max-request-size> <!-- 多大的文件會被自動保存到硬盤上。0 表明全部 --> <file-size-threshold>0</file-size-threshold> </multipart-config> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
其餘的都是同樣的。
imgTest .java 和html 這個代碼都是和上面的不變!