注意点 :
- 需要配置
multipartResolver
解析器进行文件上传 - springmvc默认使用commons-fileupload上传文件,需要引入commons-fileupload 和 commons-io 两个包
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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<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>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
spring-mvc.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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://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.yc.us.web" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" /> <!-- 给视图加上后缀 如:视图是success,返回是success.jsp -->
</bean>
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸1024字节=1K -->
<property name="maxUploadSize">
<value>1024000</value>
</property>
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean>
</beans>
Handler
@RequestMapping(path="/modify",method=RequestMethod.POST)
@ResponseBody
public boolean modify(@RequestParam(name="pic",required=false)MultipartFile file,User user,HttpServletRequest request){
System.out.println(file);
if(file!=null&&!file.isEmpty()){
String path = new File(request.getSession().getServletContext().getRealPath("/")).getParent()+"\\upload\\"+file.getOriginalFilename();
System.out.println(path);
File saveTargetFile = new File(path);
if(!saveTargetFile.exists()){
try {
saveTargetFile.createNewFile();
} catch (IOException e) {
return false;
}
}
//保存
try {
file.transferTo(saveTargetFile);
} catch (IllegalStateException | IOException e) {
return false;
}
}
System.out.println(user);
return true;
}
Form
<form id="modifyForm" method="post" enctype="multipart/form-data">
<p><input id="uid" name="id" readonly="readonly"></p>
<p><input id="uname" name="name"></p>
<p><input id="ubirthday" name="birthday"></p>
<p><input id="ugender" name="gender"></p>
<p><input id="ucareer" name="career"></p>
<p><input id="uaddress" name="address"></p>
<p><input id="umobile" name="mobile"></p>
<p><input type="file" id="upic" name="pic" onChange='changePic(this)'><br>
<img src="image/not_pic.jpg" id=pic>
</p>
<p><a class='closebtn' href='javascript:void(0)'>关闭</a>
<a class='updatebtn' href='javascript:void(0)'>修改</a>
</p>
</form>