创建springMVC工程 SpringMVCFileUpload,导入springMVC相关jar以及commons-io.jar、commons-fileupload.jar;
创建springMVC默认配置文件 SpringMVCFileUpload-servlet.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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.bwy" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/> </beans>
创建文件模型 FileModel.java:
package com.bwy.fileupload; import org.springframework.web.multipart.MultipartFile; public class FileModel { private MultipartFile file; public MultipartFile getFile() { return file; } public void setFile(MultipartFile file) { this.file = file; } }
编写上传页面 fileUpload.jsp:
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Spring MVC 文件上传</title> </head> <body> <form:form action="/SpringMVCFileUpload/upload.do" method="post" modelAttribute="fileUpload" enctype="multipart/form-data"> 请选择一个文件上传:<input type="file" name="file" /> <input type="submit" value="提交上传" /> </form:form> </body> </html>
上传成功页面 success.jsp:
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> ${fileName},上传成功! </body> </html>
编写上传文件控制类:FileUploadController.java:
package com.bwy.fileupload; import java.io.File; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; /** * 文件上传控制类 * * @author Administrator * */ @Controller public class FileUploadController { @RequestMapping(value = "/uploadPage", method = RequestMethod.GET) public ModelAndView fileUploadPage() { return new ModelAndView("fileUpload", "command", new FileModel()); } @RequestMapping(value = "/upload.do", method = RequestMethod.POST) public String upload(FileModel file, ModelMap model, HttpServletRequest request) { // 定义文件上传目录 String fileUploadPath = request.getSession().getServletContext().getRealPath("") + File.separator + "upload" + File.separator; // 生成目录 File uploadFile = new File(fileUploadPath); if (!uploadFile.exists()) { uploadFile.mkdir(); } try { // 拷贝文件,假设上传后的文件名为:xx/xx/1.jpg FileCopyUtils.copy(file.getFile().getBytes(), new File(fileUploadPath + "1.jpg")); } catch (IOException e) { e.printStackTrace(); } model.addAttribute("fileName", file.getFile().getOriginalFilename()); return "success"; } }
启动tomcat 运行工程: