1. 文件上傳
1.1 上傳文件大小限制修改
SpringBoot 2.1版本默認單次上傳文件總大小最大爲10MB,單個文件最大爲1MB,在很多應用場景中可能滿足不了我們的需求,所以要修改它的默認值。
修改起來也很簡單:
在application.properties文件中添加以下代碼
#上傳的總文件的最大大小
spring.servlet.multipart.max-request-size=50MB
#單個文件的最大大小
spring.servlet.multipart.max-file-size=10MB
1.2 上傳單文件
編寫Thymeleaf表單頁面fileUpload.html如下
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>文件上傳</title>
</head>
<body>
<form th:method="POST" th:action="@{/upload}" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
編寫Controller如下:
@Controller
public class FileController {
@GetMapping("/fileupload")
public String upload()
{
return "fileUpload";
}
@PostMapping("/upload")
public String postUpload(@RequestParam("file") MultipartFile multipartFile, Map<String,Object> map)
{
if(multipartFile.isEmpty()){
System.out.println("上傳失敗,未選擇文件");
map.put("msg","上傳失敗,未選擇文件");
return "message";
}
//獲得文件名
String fileName = multipartFile.getOriginalFilename();
System.out.println("fileName = " + fileName);
String filePath = "/Users/yky/uploadFiles/";
File file = new File(filePath + fileName);
try {
multipartFile.transferTo(file);
System.out.println("上傳成功");
map.put("msg","上傳成功");
} catch (IOException e) {
e.printStackTrace();
System.out.println("上傳失敗");
map.put("msg","上傳失敗");
}
return "message";
}
}
上傳文件
成功
1.3 上傳多文件
上傳多文件和上傳但文件基本一樣,要改動的地方有以下幾個:
1.在html多添加幾個file標籤
2.把Controller裏的MultipartFile改成MultipartFile[]
3.遍歷MultipartFile數組
fileUpload.html代碼
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>文件上傳</title>
</head>
<body>
<form th:method="POST" th:action="@{/upload}" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="file" name="file"><br>
<input type="file" name="file"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
Controller代碼
@Controller
public class FileController {
@GetMapping("/fileupload")
public String upload()
{
return "fileUpload";
}
@PostMapping("/upload")
public String postUpload(@RequestParam("file") MultipartFile[] multipartFiles, Map<String,Object> map)
{
for(MultipartFile multipartFile : multipartFiles)
{
if(multipartFile.isEmpty()){
System.out.println("上傳失敗,未選擇文件");
map.put("msg","上傳失敗,未選擇文件");
return "message";
}
//獲得文件名
String fileName = multipartFile.getOriginalFilename();
System.out.println("fileName = " + fileName);
String filePath = "/Users/yky/uploadFiles/";
File file = new File(filePath + fileName);
try {
multipartFile.transferTo(file);
System.out.println("上傳成功");
} catch (IOException e) {
e.printStackTrace();
System.out.println("上傳失敗");
map.put("msg","上傳失敗");
return "message";
}
}
map.put("msg","上傳成功");
return "message";
}
}
其實後面寫的上傳多個文件的Controller代碼也可以用戶處理單個文件的上傳,是不是很簡單啊。
2. 文件下載
在文件上傳那一節中,我們將文件上傳到了/Users/yky/uploadFiles目錄下,現在我們再來實現一下從這個目錄下載吧。
2.1 列出可供下載的文件
編寫fileList.html,代碼如下
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>文件列表</title>
</head>
<body>
<table border="1px">
<tr>
<td>文件名</td>
<td>操作</td>
</tr>
<tr th:each="file : ${files}">
<td th:text="${file}"></td>
<td>
<a th:href="@{/download(fileName=${file})}">下載</a>
</td>
</tr>
</table>
</body>
</html>
編寫Controller,列出目錄下的所有文件
@GetMapping("/files")
public String files(Map<String,Object> map)
{
String filePath = "/Users/yky/uploadFiles/";
File files = new File(filePath);
System.out.println(Arrays.toString(files.list()));
map.put("files",files.list());
return "fileList";
}
效果
2.2 實現文件下載
@GetMapping("/download")
public String download(@RequestParam("fileName") String fileName, Map<String,Object> map, HttpServletResponse response)
{
String filePath = "/Users/yky/uploadFiles/";
File file = new File(filePath + fileName);
if(file.exists())
{
//配置文件下載
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
try {
//正常顯示下載文件的中文名
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName,"UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
map.put("msg","設置編碼錯誤");
return "message";
}
byte[] buffer = new byte[1024];
OutputStream outputStream = null;
try (
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);)
{
outputStream = response.getOutputStream();
int ret = bufferedInputStream.read(buffer);
while (ret != -1)
{
outputStream.write(buffer,0, ret);
ret = bufferedInputStream.read(buffer);
}
} catch (IOException e) {
e.printStackTrace();
map.put("msg","內部錯誤");
return "message";
}
}
else
{
map.put("msg","文件不存在");
return "message";
}
map.put("msg",fileName + "下載成功");
return "message";
}
對比一下md5,看數據有沒有錯誤:
文件下載的程序邏輯也很簡單,大家可以自己分析分析,文件上傳下載的知識點就講到這兒了。