SpringMVC--上传下载文件

  1. servlet 3.0(最常用 适合spring3.1版本以后,不依赖jar包)
  2. 依赖于Apache 需要依赖于第三方jar包

下面关于servlet 3.0我们展开介绍

  • 配置web.xml
1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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-appxsd">

这里有一点需要注意 web.xml版本一定要大于等于3.0 否则servlet没有\标签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<!--基于servlet 3.0的文件上传-->
<multipart-config>
<location>/Users/mintaoyu/Desktop/springsm</location>
<max-file-size>10485760</max-file-size>
<max-request-size>104857600</max-request-size>
<file-size-threshold>102400</file-size-threshold>
</multipart-config>
</servlet>
  • 书写表单
1
2
3
4
5
6
7
8
9
10
11
12
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上传文件</title>
</head>
<body>
<form method="post" action="/uploadfile" enctype="multipart/form-data"> //这里必须注意,上传文件必须为multipart/form-data类型
<input type="file" name="file" value="请选择上传的文件">
<input type="submit" value="提交">
</form>
</body>
</html>
  • 编写controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public ModelAndView upload(MultipartFile file) {
//定义json视图
ModelAndView mv = new ModelAndView();
mv.setView(new MappingJackson2JsonView());
//获取原始文件名
String filename = file.getOriginalFilename();
file.getContentType();
//目标文件
File dest = new File(filename);
try {
//保存文件
file.transferTo(dest);
mv.addObject("success", true);
mv.addObject("msg", "上传文件成功");
} catch (IllegalStateException | IOException e) {
mv.addObject("success", false);
mv.addObject("msg", false);
e.printStackTrace();
}
return mv;
}

多文件上传

  • form表单中添加<input type="file" name="files" value="请选择上传的文件" multiple="multiple">
  • 将controller中的public ModelAndView upload(MultipartFile file)参数改为MultipartFile[] files
    这里要注意我们参数名称改了,所以前面的form表单中的name也需要改变
  • 在controller中添加foreach循环迭代出 file对象 (idea中foreach快捷键为iter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
for (MultipartFile file : files) {
String filename = file.getOriginalFilename();
file.getContentType();
//目标文件
File dest = new File(filename);
try {
//保存文件
file.transferTo(dest);
mv.addObject("success", true);
mv.addObject("msg", "上传文件成功");
} catch (IllegalStateException | IOException e) {
mv.addObject("success", false);
mv.addObject("msg", false);
e.printStackTrace();
}
}
```

## springmvc下载文件有2种方法

>1. 使用request方法
>2. springmvc自带封装方法(常用)

- 编写controller

```java
//下载文件
@Autowired
private ServletContext servletContext;

@RequestMapping("/download")
public ResponseEntity<byte[]> download(String fileName) throws Exception {
String dir = servletContext.getRealPath("/WEB-INF/down");
//HttpHeaders不要导错包,是Spring包里面
HttpHeaders headers = new HttpHeaders();
//设置响应头:下载文件
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//设置建议保存名称
headers.setContentDispositionFormData("attachment", fileName);
//FileUtils需要使用commons-io jar包
return new ResponseEntity<>(FileUtils.readFileToByteArray(new File(dir, fileName)), headers, HttpStatus.CREATED);
}
  • 链接下载

    <a href="/download?fileName=springboot笔记与资料.zip">springboot笔记与资料.zip</a>
    

最终结果

最终结果

赏个🍗吧
0%