(由于字数限制请看项目里代码,下面省略部分不重要的代码)
首先是web.xml<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
upload
org.springframework.web.servlet.DispatcherServlet
1
upload
/
SpringCharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
SpringCharacterEncodingFilter
/*
/WEB-INF/jsp/user/add.jsp
接下来是SpringMVC的配置文件upload-servlet.xml<?xml version="1.0" encoding="UTF-8"?>
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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
error_fileupload
下面是用于上传的表单页面//WEB-INF//jsp//user//add.jsp(由于字数限制请看项目里代码)
下面是上传成功后打印用户信息的页面//WEB-INF//jsp//user//list.jsp(由于字数限制请看项目里代码)
下面是上传文件内容过大时的提示页面//WEB-INF//jsp//error_fileupload.jsp(由于字数限制请看项目里代码)
接下来是用到的实体类User.javapublic class User {
private String username;
private String nickname;
private String password;
private String email;
//{此处get.set省略}
public User() {
}
public User(String username, String nickname, String password, String email) {
this.username = username;
this.nickname = nickname;
this.password = password;
this.email = email;
}
}
最后是核心的UserController.java@Controller
@RequestMapping("/user")
public class UserController {
private final static Map users = new HashMap();
// 模拟数据源,构造初始数据
public UserController() {
users.put("张起灵", new User("张起灵", "闷油瓶", "02200059",
"[email protected]"));
users.put("李寻欢", new User("李寻欢", "李探花", "08866659",
"[email protected]"));
users.put("拓拔野", new User("拓拔野", "搜神记", "05577759",
"[email protected]"));
users.put("孙悟空", new User("孙悟空", "美猴王", "03311159",
"[email protected]"));
}
@RequestMapping("/list")
public String list(Model model) {
model.addAttribute("users", users);
return "user/list";
}
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String addUser() {
return "user/add";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addUser(User user, @RequestParam MultipartFile[] myfiles,
HttpServletRequest request) throws IOException {
// 如果只是上传一个文件,则只需要MultipartFile类型接收文件即可,而且无需显式指定@RequestParam注解
// 如果想上传多个文件,那么这里就要用MultipartFile[]类型来接收文件,并且还要指定@RequestParam注解
// 并且上传多个文件时,前台表单中的所有
// type="file"/>的name都应该是myfiles,否则参数里的myfiles无法获取到所有上传的文件
for (MultipartFile myfile : myfiles) {
// if (myfile.isEmpty()) {
// System.out.println("文件未上传");
// } else {
System.out.println("文件长度: " + myfile.getSize());
System.out.println("文件类型: " + myfile.getContentType());
System.out.println("文件名称: " + myfile.getName());
System.out.println("文件原名: " + myfile.getOriginalFilename());
System.out.println("========================================");
// 如果用的是Tomcat服务器,则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中
// 这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉,我是看它的源码才知道的
// String realPath =
// request.getSession().getServletContext().getRealPath("/FileTest/WebContent/WEB-INF/upload");
String realPath = request.getSession().getServletContext()
.getRealPath("/WEB-INF/upload");
System.out.println(realPath);
FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(
realPath, myfile.getOriginalFilename()));
// }
}
users.put(user.getUsername(), user);
return "redirect:/user/list";
}
}
补充:记得建立这个目录,用于存放上传的文件,即//WEB-INF//upload//
下面是程序运行截图:
添加成功后: