SpringBoot上传下载文件

1 配置文件

1
2
3
4
5
6
7
########## 文件上传配置 #########
spring:
servlet:
multipart:
enabled: true # 开启多文件上传
max-file-size: 5MB
max-request-size: 5MB

2 Controller编写

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.pibigstar.web;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.pibigstar.common.Constant;

/**
* 文件上传Controller
* @author pibigstar
*
*/
@Controller()
@RequestMapping("/file")
public class FileController {

private final String upload_path = Constant.DEFAULT_FILE_UPLOAD_PATH;

@GetMapping("toUpload")
public String index() {
return "upload";
}

/**
* 上传
* @param file
* @param redirectAttributes
* @return
*/
@PostMapping("upload")
public String singleFileUpload(@RequestParam("file") MultipartFile file,RedirectAttributes redirectAttributes) {
if (file.isEmpty()) {
redirectAttributes.addFlashAttribute("message", "请选择文件进行上传");
return "redirect:/file/uploadStatus";
}

try {
byte[] bytes = file.getBytes();
Path path = Paths.get(upload_path+file.getOriginalFilename());
Files.write(path, bytes);

redirectAttributes.addFlashAttribute("message","成功上传文件: '" + file.getOriginalFilename() + "'");

} catch (IOException e) {
e.printStackTrace();
}

return "redirect:/file/uploadStatus";
}

/**
* 下载
* @param res
* @throws IOException
*/
@GetMapping("download")
public void download(HttpServletResponse res) throws IOException {

String fileName = "Main.jpg";
res.setHeader("content-type", "application/octet-stream");
res.setContentType("application/octet-stream");
res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = res.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(new File("E://temp/"+ fileName)));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("success");
}

@GetMapping("uploadStatus")
public String uploadStatus() {
return "uploadStatus";
}
}

3 前台

upload.html

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>

<h1>文件上传示例</h1>

<form method="POST" action="/file/upload" enctype="multipart/form-data">
<input type="file" name="file" /><br/><br/>
<input type="submit" value="Submit" />
</form>

</body>
</html>

uploadStatus.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>

<h1>上传成功</h1>

<div th:if="${message}">
<h2 th:text="${message}"/>
</div>

<div th:if="${path}">
<h2 th:text="${path}"/>
</div>

</body>
</html>
-------------本文结束感谢您的阅读-------------