筆者計劃為大家介紹分布式檔案系統,用于存盤應用的圖片、word、excel、pdf等檔案,在開始介紹分布式檔案系統之前,為大家介紹一下使用本機存盤來存放檔案資源,
二者的核心實作程序是一樣的:
- 上傳檔案,保存檔案(本節是本地磁盤)
- 回傳檔案HTTP訪問服務路徑給前端,進行上傳之后的效果展示
一、復習
服務端接收上傳的目的是提供檔案的訪問服務,那么對于SpringBoot而言,有哪些可以提供檔案訪問的靜態資源目錄呢?
classpath:/META-INF/resources/,classpath:/static/,classpath:/public/,classpath:/resources/
這是之前我們為大家介紹的內容,從這里看出這里的靜態資源都在classpath下,那么就出現問題:
- 應用的檔案資源不能和專案代碼分開存盤(你見過往github上傳代碼,還附帶專案檔案資料的么?)
- 專案打包困難,當上傳的檔案越來越多,專案的打包jar越來越大,
- 代碼與檔案資料不能分開存盤,就意味著檔案資料的備份將變得復雜
二、檔案上傳目錄自定義配置
怎么解決上述問題?別忘記了spring boot 為我們提供了使用spring.resources.static-locations配置自定義靜態檔案的位置,
web:
upload-path: D:/data/
spring:
resources:
static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path}
- 配置
web.upload-path為與專案代碼分離的靜態資源路徑,即:檔案上傳保存根路徑 - 配置
spring.resources.static-locations,除了帶上Spring Boot默認的靜態資源路徑之外,加上file:${web.upload-path}指向外部的檔案資源上傳路徑,該路徑下的靜態資源可以直接對外提供HTTP訪問服務,
三、檔案上傳的Controller實作
詳情看代碼注釋
@RestController
public class FileUploadController {
//系結檔案上傳路徑到uploadPath
@Value("${web.upload-path}")
private String uploadPath;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
@PostMapping("/upload")
public String upload(MultipartFile uploadFile,
HttpServletRequest request) {
// 在 uploadPath 檔案夾中通過日期對上傳的檔案歸類保存
// 比如:/2019/06/06/cf13891e-4b95-4000-81eb-b6d70ae44930.png
String format = sdf.format(new Date());
File folder = new File(uploadPath + format);
if (!folder.isDirectory()) {
folder.mkdirs();
}
// 對上傳的檔案重命名,避免檔案重名
String oldName = uploadFile.getOriginalFilename();
String newName = UUID.randomUUID().toString()
+ oldName.substring(oldName.lastIndexOf("."), oldName.length());
try {
// 檔案保存
uploadFile.transferTo(new File(folder, newName));
// 回傳上傳檔案的訪問路徑
String filePath = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + format + newName;
return filePath;
} catch (IOException e) {
throw new CustomException(CustomExceptionType.SYSTEM_ERROR);
}
}
}
四、寫一個模擬的檔案上傳頁面,進行測驗
把該upload.html檔案放到classpath:public目錄下,對外提供訪問,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFile" value=https://www.cnblogs.com/zimug/p/"請選擇上傳檔案">
