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"。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>springMVC文件上传</title> </head> <body> <h1>文件上传</h1> <form action="upload" enctype="multipart/form-data" method="post"> <table> <tr> <td>文件描述:</td> <td><input type="text" name="description"></td> </tr> <tr> <td>请选择文件</td> <td><input type="file" name="file"/></td> </tr> <tr> <td> <input type="submit" value="上传"/> </td> </tr> </table> </form> </body> </html>
2.编写Controller
package com.cn.controller; import java.io.File; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; @Controller public class FileUploadController { @RequestMapping(value="uploadForm") public String uploadForm() { return "uploadForm"; } @RequestMapping(value="upload",method=RequestMethod.POST) public String upload(HttpServletRequest request, @RequestParam("description") String description, @RequestParam("file") MultipartFile file) throws Exception{ System.out.println(description); if(!file.isEmpty()) { //上传文件的路径 String path = request.getServletContext().getRealPath("/images/"); //上传文件名 String fileName = file.getOriginalFilename(); File filePath = new File(path,fileName); if(!filePath.getParentFile().exists()) { filePath.getParentFile().mkdirs(); } //将文件保存到目标文件中 file.transferTo(new File(path + File.separator + fileName)); return "success"; }else{ return "error"; } } }
3.配置springmvc-config.xml
这里需要配置MultipartResolver。需要注意的是:请求的编码格式必须和jsp中的pageEncoding属性一致,以便正确读取表单的内容,默认值为“iso8859-1”。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd "> <!-- Spring 可以自动去扫描base-pack下面的包或者子包下面的java文件 --> <!-- 如果扫描到spring相关的注解类,将其注册为spring的bean --> <context:component-scan base-package="com.cn.controller" /> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/content/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize"> <value>10485760</value> </property> <property name="defaultEncoding"> <value>UTF-8</value> </property> </bean> </beans>
4.配置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>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> <!-- 定义Spring MVC前端控制器 --> <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> <!-- 让Spring MVC的前端控制器拦截所有请求 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
本文出自 “五小猪” 博客,请务必保留此出处http://wuxiaozhu.blog.51cto.com/7942143/1920257
原文地址:http://wuxiaozhu.blog.51cto.com/7942143/1920257