1、配置
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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 使用注解的包,包括子集 --> <context:component-scan base-package="com.fx"/> <!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp"></property> </bean> <!-- 文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"/> <property name="maxUploadSize" value="10000000"/> </bean> </beans>
2、添加controller
FileUploadController.java
package com.fx.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.RequestParam; import org.springframework.web.multipart.MultipartFile; /** * 控制器 * @author fx * */ @Controller public class FileUploadController { /** * 文件上传 * @param file1 * @param request * @return * @throws Exception */ @RequestMapping("/upload") public String uploadFile(@RequestParam("file1") MultipartFile file1,HttpServletRequest request)throws Exception{ //获取项目路径 String filePath=request.getServletContext().getRealPath("/"); System.out.println(filePath); //保存文件 file1.transferTo(new File(filePath+"upload/"+file1.getOriginalFilename())); return "redirect:success.html"; } /** * 多文件上传 * @param files * @param request * @return * @throws Exception */ @RequestMapping("/upload2") public String uploadFiles(@RequestParam("file") MultipartFile[] files,HttpServletRequest request)throws Exception{ //获取项目路径 String filePath=request.getServletContext().getRealPath("/"); System.out.println(filePath); //遍历文件列表 for(MultipartFile file:files){ //保存文件 file.transferTo(new File(filePath+"upload/"+file.getOriginalFilename())); } return "redirect:success.html"; } }
3、配置web.xml
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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SpringMvc04</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>springmvc</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>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
4、添加jar
com.springsource.org.apache.commons.io-1.4.0.jar
com.springsource.org.apache.commons.fileupload-1.2.0.jar