学习springmvc文件上传可谓是一波三折。这里我就分享下我的踩坑之路吧
首先是代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>上传文件</title> </head> <body> <form action="/upfile.do" method="post" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit" value="上传" /> </form> </body> </html>
controller
@RequestMapping(value = "/upfile",method= RequestMethod.POST) public String fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) { // 判断文件是否为空 if (!file.isEmpty()) { try { // 文件保存路径 String filePath = request.getSession().getServletContext().getRealPath("/") + "update/" + file.getOriginalFilename(); // 转存文件 file.transferTo(new File(filePath)); } catch (Exception e) { e.printStackTrace(); } } // 重定向 return "success"; }
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.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-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <mvc:annotation-driven></mvc:annotation-driven> <context:annotation-config/> <context:component-scan base-package="Controller"/> <context:component-scan base-package="dao"></context:component-scan> <context:component-scan base-package="Service"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"> </property> <property name="suffix" value=".jsp"></property> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property> <property name="maxUploadSize" value="104857600000"></property> <property name="maxInMemorySize" value="40960"></property> </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"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <session-config> <session-timeout>20</session-timeout> </session-config> <!--spring 字符过滤器--> <filter> <filter-name>filterEncoding</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>filterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <servlet> <servlet-name>Dispatcher</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>Dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
以上是通过的代码。错误的我就不拿出来丢人了。主要是说几点注意
1.<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">必须设置id。否则会报
2.表单必须设置method=“post”和enctype=“multipart/form-data”.不然会报
3.接收文件使用multipartFile。
暂时就先学到这里。以上只是个人的解决方法。如果有什么错了,还望指出