文件上传步骤
1.写一个文件上传的页面
2.写一个文件上传的控制器
注意:
1.method="post"
2.enctype="multipart/form-data"
3.文件类型上传组件 type="file"
4.接收文件参数需要使用MultipartFile 类型的参数
5.配置文件解析器,文件解析器 id 必须是 "multipartResolver"
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="upload.do" method="post" enctype="multipart/form-data"> 11 姓名:<input type="text" name="username"><br> 12 头像:<input type="file" name="mf"><br> 13 <input type="submit" value="上传"> 14 </form> 15 </body> 16 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h1>欢迎进入</h1> 11 <img alt="程序猿" src="${imgPath }"> 12 </body> 13 </html>
1 package com.xcz.controller; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 import javax.servlet.http.HttpServletRequest; 7 8 import org.springframework.stereotype.Controller; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.multipart.MultipartFile; 11 12 @Controller 13 public class UploadController { 14 @RequestMapping("/toUpload.do") 15 public String toUpload() { 16 return "upload"; 17 } 18 19 @RequestMapping("/upload.do") 20 public String upload(String username, MultipartFile mf, HttpServletRequest request) { 21 String path = request.getServletContext().getRealPath("images"); 22 String orname = mf.getOriginalFilename(); 23 String fileName = System.currentTimeMillis() + orname.substring(orname.lastIndexOf(".")); 24 String filePath = path + "/" + fileName; 25 File file = new File(filePath); 26 try { 27 mf.transferTo(file); 28 request.setAttribute("imgPath", "images/" + fileName); 29 } catch (IOException e) { 30 e.printStackTrace(); 31 } 32 return "show"; 33 } 34 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:lang="http://www.springframework.org/schema/lang" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xmlns:util="http://www.springframework.org/schema/util" 8 xmlns:task="http://www.springframework.org/schema/task" 9 xmlns:aop="http://www.springframework.org/schema/aop" 10 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 11 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd 12 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 13 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 14 http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd 15 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 16 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> 17 <!-- 开启组件扫描 --> 18 <context:component-scan base-package="com.xcz"></context:component-scan> 19 <!-- 开启mvc标注 --> 20 <mvc:annotation-driven></mvc:annotation-driven> 21 <!-- 配置视图处理器 --> 22 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 23 <property name="prefix" value="/WEB-INF/"></property> 24 <property name="suffix" value=".jsp"></property> 25 </bean> 26 <!-- 配置一个文件解析器 --> 27 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 28 <property name="maxUploadSize" value="102400"></property> 29 <property name="resolveLazily" value="true"></property> 30 </bean> 31 <!-- 引入外部db.properties --> 32 <context:property-placeholder location="classpath:db.properties"/> 33 <!-- 配置数据源 --> 34 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 35 <property name="driverClassName" value="${driverClassName}"></property> 36 <property name="url" value="${url}"></property> 37 <property name="username" value="${jdbc.username}"></property> 38 <property name="password" value="${jdbc.password}"></property> 39 </bean> 40 </beans>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 version="3.1"> 6 <display-name>SpringMVC-06</display-name> 7 <welcome-file-list> 8 <welcome-file>index.html</welcome-file> 9 <welcome-file>index.htm</welcome-file> 10 <welcome-file>index.jsp</welcome-file> 11 <welcome-file>default.html</welcome-file> 12 <welcome-file>default.htm</welcome-file> 13 <welcome-file>default.jsp</welcome-file> 14 </welcome-file-list> 15 <!-- 配置请求入口 --> 16 <servlet> 17 <servlet-name>SpringMVC</servlet-name> 18 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 19 <!-- 配置初始化参数 --> 20 <init-param> 21 <param-name>contextConfigLocation</param-name> 22 <param-value>classpath:mvc.xml</param-value> 23 </init-param> 24 <load-on-startup>1</load-on-startup> 25 </servlet> 26 <servlet-mapping> 27 <servlet-name>SpringMVC</servlet-name> 28 <url-pattern>*.do</url-pattern> 29 </servlet-mapping> 30 <!-- 配置编码过滤器 --> 31 <filter> 32 <filter-name>filterEncoding</filter-name> 33 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 34 <init-param> 35 <param-name>encoding</param-name> 36 <param-value>utf-8</param-value> 37 </init-param> 38 </filter> 39 <filter-mapping> 40 <filter-name>filterEncoding</filter-name> 41 <url-pattern>*.do</url-pattern> 42 </filter-mapping> 43 </web-app>