SpringMVC上传文件DEMO

SpringMVC为文件上传提供了直接的支持,这种支持是即插即用的MultipartResolver实现的。SpringMVC使用Apache Commons FileUpload技术实现了一个MultipartResolver实现类:CommonsMultipartResolver.因此SpringMVC的文件上传需要依赖ApacheCommons FileUpload的组件(commons-io-2.5.jar以及commons-fileupload-1.3.2.jar)。

1.编写jsp

需要注意的点:表单的method必须为post方式,负责上传文件的表单编码类型必须是“multipart/form-data"。

<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtml><html><head><metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"><title>springMVC文件上传</title></head><body><h2>文件上传</h2><formaction="upload"enctype="multipart/form-data"method="post"><table><tr><td>文件描述:</td><td><inputtype="text"name="description"></td></tr><tr><td>请选择文件</td><td><inputtype="file"name="file"/></td></tr><tr><td><inputtype="submit"value="上传"/></td></tr></table></form></body></html>

2.编写Controller


packagecom.cn.controller;importjava.io.File;importjavax.servlet.http.HttpServletRequest;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.multipart.MultipartFile;@ControllerpublicclassFileUploadController{@RequestMapping(value="uploadForm")publicStringuploadForm(){return"uploadForm";}@RequestMapping(value="upload",method=RequestMethod.POST)publicStringupload(HttpServletRequestrequest,@RequestParam("description")Stringdescription,@RequestParam("file")MultipartFilefile)throwsException{System.out.println(description);if(!file.isEmpty()){//上传文件的路径Stringpath=request.getServletContext().getRealPath("/p_w_picpaths/");//上传文件名StringfileName=file.getOriginalFilename();FilefilePath=newFile(path,fileName);if(!filePath.getParentFile().exists()){filePath.getParentFile().mkdirs();}//将文件保存到目标文件中file.transferTo(newFile(path+File.separator+fileName));return"success";}else{return"error";}}}

3.配置springmvc-config.xml

这里需要配置MultipartResolver。需要注意的是:请求的编码格式必须和jsp中的pageEncoding属性一致,以便正确读取表单的内容,默认值为“iso8859-1”。

<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"><!--Spring可以自动去扫描base-pack下面的包或者子包下面的java文件--><!--如果扫描到spring相关的注解类,将其注册为spring的bean--><context:component-scanbase-package="com.cn.controller"/><beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/><beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><propertyname="prefix"><value>/WEB-INF/content/</value></property><propertyname="suffix"><value>.jsp</value></property></bean><beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><propertyname="maxUploadSize"><value>10485760</value></property><propertyname="defaultEncoding"><value>UTF-8</value></property></bean></beans>

4.配置web.xml

<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0"><display-name>SpringMVCTest</display-name><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><!--定义SpringMVC前端控制器--><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/springmvc-config.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><!--让SpringMVC的前端控制器拦截所有请求--><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>