spring mvc 文件上传
jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="sf" uri="http://www.springframework.org/tags/form"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'add.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <sf:form method="post" modelAttribute="user" enctype="multipart/form-data"> file:<input type="file" name="files" /> <br /> file:<input type="file" name="files" /> <br /> file:<input type="file" name="files" /> <br /> <input type="submit" /> </sf:form> </body> </html>
springmvc.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> ... <!-- 文件上传配置 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="50000000" /> </bean> ... </beans>
controller:
package com.stone.controller; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Random; import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; import org.apache.commons.io.FileUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.PathVariable; 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.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import org.springframework.web.servlet.view.InternalResourceViewResolver; import com.stone.model.User; @Controller @RequestMapping("/user") @SessionAttributes("loginUser") public class UserController { private final static Map<String, User> users = new HashMap<String, User>(); public UserController() { users.put("ldh", new User("ldh", "刘德华", "123", "123@123.com")); users.put("zxy", new User("zxy", "张学友", "123", "123@123.com")); users.put("gfc", new User("gfc", "郭富城", "123", "123@123.com")); users.put("lm", new User("lm", "黎明", "123", "123@123.com")); } @RequestMapping("/users") public String list(Model model) { model.addAttribute("users", users); return "user/list"; } @RequestMapping(value = "/add", method = RequestMethod.GET) public String add(Model model) { model.addAttribute(new User()); return "user/add"; } @RequestMapping(value = "/add", method = RequestMethod.POST) public String add(@RequestParam(required = false) MultipartFile[] files, HttpServletRequest req) throws IOException { for (MultipartFile file : files) { System.out.println(file.getContentType() + "," + file.getName() + "," + file.getOriginalFilename()); String realpath = req.getSession().getServletContext() .getRealPath("/resources/upload/"); System.out.println(realpath); if (!file.isEmpty()) { FileUtils.copyInputStreamToFile( file.getInputStream(), new File(realpath + File.separator + new Random().nextInt())); } } return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/user/users"; }
//如果是一个文件的话,使用这样的方式
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add( MultipartFile files,
HttpServletRequest req) throws IOException {
System.out.println(file.getContentType() + "," + file.getName()
+ "," + file.getOriginalFilename());
String realpath = req.getSession().getServletContext()
.getRealPath("/resources/upload/");
System.out.println(realpath);
if (!file.isEmpty()) {
FileUtils.copyInputStreamToFile(
file.getInputStream(),
new File(realpath + File.separator
+ new Random().nextInt()));
}
return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/user/users";
}
}