Spring MVC为文件的上传提供了直接的支持,但需要依赖Apache提供Commons FileUpload组件jar包
文件上传
表单的method设置为POST,并将enctype设置为multipart/form-data 。 浏览器才会把用户选择的文件以二进制数据发送给服务器 。
Spring MVC使用Apache Commons FileUpload技术实现了一个MultipartResolver实现类:CommonsMultipartResolver。因此,SpringMVC的文件上传还需要依赖Apache Commons FileUpload的组件
页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
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=UTF-8">
<title>文件上传</title>
</head>
<body>
<h2>文件上传</h2>
<form action="${pageContext.request.contextPath}/res/upload.do" enctype="multipart/form-data" method="post">
<table>
<tr>
<td>文件描述:</td>
<td><input type="text" name="description"></input></td>
</tr>
<tr>
<td>请选择文件</td>
<td><input type="file" name="file"></input></td>
</tr>
<tr>
<td><input type="submit" value="上传"/></td>
</tr>
</table>
</form>
</body>
</html>
Controller:
@ResponseBody
@RequestMapping(value = "/upload.do", 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 path = "E:\\images";
// 上传文件名
String filename = file.getOriginalFilename();
File filepath = new File(path, filename);
System.out.println(filename);
System.out.println(filepath.getParentFile());
// 判断路径是否存在,如果不存在就创建一个
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();
}
// 将上传文件保存到一个目标文件当中
file.transferTo(new File(path + File.separator + filename));
return "success";
} else {
return "error";
}
}
Spring MVC会将上传的文件绑定到MultipartFile对象中。MultipartFile提供了获取上传文件内容、文件名等方法。通过transferTo()方法还可以将文件存储到硬件中
MultipartFile对象中的常用方法 :
- byte[] getBytes():获取文件数据
- String getContentType[]:获取文件MIME类型,如image/jpeg等
- InputStream getInputStream():获取文件流
- String getName():获取表单中文件组件的名字
- String getOriginalFilename():获取上传文件的原名
- Long getSize():获取文件的字节大小,单位为byte
- boolean isEmpty():是否有上传文件
- void transferTo(File dest):将上传文件保存到一个目录文件中
SpringMVC上下文中默认没有装配MultipartResolver,因此默认情况下其不能处理文件上传工作。如果想使用Spring的文件上传功能,则需要在上下文中配置MultipartResolver。在spring-mvc.xml进行配置
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上传文件大小上限,单位为字节(10MB) -->
<property name="maxUploadSize">
<value>10485760</value>
</property>
<!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean>
多个文件上传
表单中使用相同的名称,然后Controller中将接受到的MultipartFile参数类定义为数组就可以
页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" 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=UTF-8"> <title>多个文件上传</title> </head> <body> <h2>多个文件上传</h2> <form action="${pageContext.request.contextPath}/res/uploads.do" enctype="multipart/form-data" method="post"> <table> <tr> <td>文件描述:</td> <td><input type="text" name="description"></input></td> </tr> </table> <table> <tr> <td>请选择文件</td> </tr> <tr><td><input type="file" name="files"></input></td></tr> <tr><td><input type="file" name="files"></input></td></tr> <tr><td><input type="file" name="files"></input></td></tr> <tr><td><input type="file" name="files"></input></td></tr> <tr> <td><input type="submit" value="上传"/></td> </tr> </table> </form> </body> </html>
Controller:
@ResponseBody
@RequestMapping(value = "/uploads.do", method = RequestMethod.POST)
public String uploads(HttpServletRequest request, @RequestParam("description") String description,
@RequestParam("files") MultipartFile[] files) throws Exception {
System.out.println(description);
if (files != null && files.length > 0) {
for (MultipartFile file : files) {
if (!file.isEmpty()) {
// 上传文件路径
// String path =
// request.getServletContext().getRealPath("/images/");
String path = "E:\\images";
// 上传文件名
String filename = file.getOriginalFilename();
File filepath = new File(path, filename);
System.out.println(filename);
// 判断路径是否存在,如果不存在就创建一个
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();
}
// 将上传文件保存到一个目标文件当中
file.transferTo(new File(path + File.separator + filename));
}
}
return "success";
}
return "error";
}
文件下载
直接在页面给出了一个超链接,该链接href的属性等于要下载文件的文件名,就可以实现文件下载了
SpringMVC提供了一个ResponseEntity类型,使用它可以很方便地定义返回的HttpHeaders和HttpStatus
@RequestMapping(value = "/download.do")
public ResponseEntity<byte[]> download(HttpServletRequest request, @RequestParam("filename") String filename,
Model model) throws Exception {
// 下载文件路径
// String path = request.getServletContext().getRealPath("/images/");
String path = "E:\\images";
File file = new File(path + File.separator + filename);
HttpHeaders headers = new HttpHeaders();
// 下载显示的文件名,解决中文名称乱码问题
String downloadFielName = new String(filename.getBytes("UTF-8"), "iso-8859-1");
// 通知浏览器以attachment(下载方式)打开图片
headers.setContentDispositionFormData("attachment", downloadFielName);
// application/octet-stream : 二进制流数据(最常见的文件下载)。
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
}
download处理方法接收页面传递的文件名filename后,使用Apache Commons FileUpload组件的FileUtils读取项目的上传文件,并将其构建成ResponseEntity对象返回客户端下载
使用ResponseEntity对象,可以很方便的定义返回的HttpHeaders和HttpStatus。上面代码中的MediaType,代表的是Internet Media Type,即互联网媒体类型,也叫做MIME类型。在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息。HttpStatus类型代表的是Http协议中的状态
下载的页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
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=UTF-8">
<title>文件下载</title>
</head>
<body>
<h3>文件下载</h3>
<a href="${pageContext.request.contextPath}/res/download.do?filename=${requestScope.user.image.originalFilename}">
${requestScope.user.image.originalFilename }
</a>
</body>
</html>