引入依赖
<?xml version="1.0" encoding="UTF-8"?> < project xmlns = " http://maven.apache.org/POM/4.0.0" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion> 4.0.0</ modelVersion> < groupId> org.example</ groupId> < artifactId> TestSpringMVC5</ artifactId> < version> 1.0-SNAPSHOT</ version> < packaging> war</ packaging> < name> TestSpringMVC5 Maven Webapp</ name> < url> http://www.example.com</ url> < properties> < project.build.sourceEncoding> UTF-8</ project.build.sourceEncoding> < maven.compiler.source> 1.7</ maven.compiler.source> < maven.compiler.target> 1.7</ maven.compiler.target> </ properties> < dependencies> < dependency> < groupId> junit</ groupId> < artifactId> junit</ artifactId> < version> 4.11</ version> < scope> test</ scope> </ dependency> < dependency> < groupId> org.springframework</ groupId> < artifactId> spring-context</ artifactId> < version> 4.3.18.RELEASE</ version> </ dependency> < dependency> < groupId> org.springframework</ groupId> < artifactId> spring-core</ artifactId> < version> 4.3.18.RELEASE</ version> </ dependency> < dependency> < groupId> org.springframework</ groupId> < artifactId> spring-beans</ artifactId> < version> 4.3.18.RELEASE</ version> </ dependency> < dependency> < groupId> org.springframework</ groupId> < artifactId> spring-context-support</ artifactId> < version> 4.3.18.RELEASE</ version> </ dependency> < dependency> < groupId> org.springframework</ groupId> < artifactId> spring-webmvc</ artifactId> < version> 4.3.18.RELEASE</ version> </ dependency> < dependency> < groupId> org.springframework</ groupId> < artifactId> spring-web</ artifactId> < version> 4.3.18.RELEASE</ version> </ dependency> < dependency> < groupId> javax.servlet</ groupId> < artifactId> javax.servlet-api</ artifactId> < version> 3.1.0</ version> < scope> provided</ scope> </ dependency> < dependency> < groupId> javax.servlet</ groupId> < artifactId> jstl</ artifactId> < version> 1.2</ version> </ dependency> < dependency> < groupId> javax.servlet.jsp</ groupId> < artifactId> jsp-api</ artifactId> < version> 2.2</ version> < scope> provided</ scope> </ dependency> < dependency> < groupId> com.google.code.gson</ groupId> < artifactId> gson</ artifactId> < version> 2.2.4</ version> </ dependency> < dependency> < groupId> com.alibaba</ groupId> < artifactId> fastjson</ artifactId> < version> 1.2.47</ version> </ dependency> < dependency> < groupId> commons-fileupload</ groupId> < artifactId> commons-fileupload</ artifactId> < version> 1.3.1</ version> </ dependency> < dependency> < groupId> commons-io</ groupId> < artifactId> commons-io</ artifactId> < version> 2.4</ version> </ dependency> </ dependencies> < build> < finalName> TestSpringMVC5</ finalName> < pluginManagement> < plugins> < plugin> < artifactId> maven-clean-plugin</ artifactId> < version> 3.1.0</ version> </ plugin> < plugin> < artifactId> maven-resources-plugin</ artifactId> < version> 3.0.2</ version> </ plugin> < plugin> < artifactId> maven-compiler-plugin</ artifactId> < version> 3.8.0</ version> </ plugin> < plugin> < artifactId> maven-surefire-plugin</ artifactId> < version> 2.22.1</ version> </ plugin> < plugin> < artifactId> maven-war-plugin</ artifactId> < version> 3.2.2</ version> </ plugin> < plugin> < artifactId> maven-install-plugin</ artifactId> < version> 2.5.2</ version> </ plugin> < plugin> < artifactId> maven-deploy-plugin</ artifactId> < version> 2.8.2</ version> </ plugin> </ plugins> </ pluginManagement> </ build>
</ project>
修改配置文件
<?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: context= " http://www.springframework.org/schema/context" xmlns: mvc= " http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" > < context: component-scan base-package = " com.test" > </ context: component-scan> < bean class = " org.springframework.web.servlet.view.InternalResourceViewResolver" > < property name = " prefix" value = " /WEB-INF/" /> < property name = " suffix" value = " .jsp" /> </ bean> < mvc: annotation-driven> </ mvc: annotation-driven> < mvc: resources mapping = " /static/**" location = " /static/" />
< bean id = " multipartResolver" class = " org.springframework.web.multipart.commons.CommonsMultipartResolver" p: defaultEncoding= " UTF-8" p: maxUploadSize= " 5242880" p: uploadTempDir= " file:/e:/file/temp" /> </ beans>
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html>
< body>
< h2> Hello World!</ h2> < form action = " users/uploadFile" method = " post" enctype = " multipart/form-data" > 上传图片:< input type = " file" name = " myfile" /> < input type = " submit" name = " sub" value = " 提交" /> </ form> </ body>
</ html>
java
package com. test. controller ; import org. springframework. stereotype. Controller ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RequestParam ;
import org. springframework. web. multipart. MultipartFile ; import javax. servlet. http. HttpServletRequest ;
import java. io. File ;
import java. io. IOException ; @Controller
@RequestMapping ( "/users" )
public class UsersController { @RequestMapping ( "/uploadFile" ) public String uploadFile ( @RequestParam ( "myfile" ) MultipartFile myfile, HttpServletRequest request) { String filename= myfile. getOriginalFilename ( ) ; System . out. println ( filename) ; try { String path= request. getSession ( ) . getServletContext ( ) . getRealPath ( "/static/images" ) ; System . out. println ( path) ; myfile. transferTo ( new File ( path+ "/" + filename) ) ; } catch ( IOException e) { e. printStackTrace ( ) ; } return "success" ; } }
web.xml
<?xml version="1.0" encoding="UTF-8"?>
< web-app xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xmlns = " http://java.sun.com/xml/ns/javaee" xsi: schemaLocation= " http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id = " WebApp_ID" version = " 3.0" > < display-name> Archetype Created Web Application</ display-name> < filter> < filter-name> CharacterEncodingFilter</ 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> forceResponseEncoding</ param-name> < param-value> true</ param-value> </ init-param> </ filter> < filter-mapping> < filter-name> CharacterEncodingFilter</ filter-name> < url-pattern> /*</ url-pattern> </ filter-mapping> < servlet> < servlet-name> dispatcherServlet</ servlet-name> < servlet-class> org.springframework.web.servlet.DispatcherServlet</ servlet-class> < init-param> < param-name> contextConfigLocation</ param-name> < param-value> classpath:springmvc.xml</ param-value> </ init-param> < load-on-startup> 1</ load-on-startup> </ servlet> < servlet-mapping> < servlet-name> dispatcherServlet</ servlet-name> < url-pattern> /</ url-pattern> </ servlet-mapping> </ web-app>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%String baseurl=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();pageContext.setAttribute("baseurl",baseurl);
%>
< html>
< base href = " ${baseurl}" />
< head> < title> Title</ title>
</ head>
< body>
success
< img src = " ${baseurl}/static/images/3.jpg" />
</ body>
</ html>
项目结构