springboot 實現本地文件存儲
實現過程
- 上傳文件
- 保存文件(本地磁盤)
- 返回文件HTTP訪問服務器路徑給前端,進行效果展示
存儲
- 服務端接收上傳的目的是提供文件的訪問服務,對于SpringBoot而言,其對靜態資源訪問提供了很好的支持,使用其提供的基本默認配置可以滿足開發需求,同時,又支持開發人員進行自定義配置。
SpringBoot默認將 / 所有訪問映射到以下目錄:**
- classpath:/META-INF/resources
- classpath:/static
- classpath:/public
- classpath:/resources
SpringBoot默認會挨個從pubic、resources、static里面找是否存在相應的資源,如果有則直接返回。
問題
- 如果都放在classpath目錄下打包的文件就會很大
- 代碼與文件數據不能分開存儲,就意味著文件數據的備份將變得復雜
解決方法
springboot提供了 spring.resources.static-locations
配置自定義靜態文件的位置:
注:該配置有問題,在下面以解決
spring:web:resources:static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${demo.web.upload-path}
# 設置Http能訪問的本地資源路徑
demo:web:upload-path: D:/MineFile/zuoye/xm/equipment-management-system/qhjdata/
- 配置 demo.web.upload-path 為與項目代碼分離的靜態資源路徑,即:文件上傳保存根路徑
- 配置 spring.web.resources.static-locations 除了帶上SpringBoot默認的靜態資源路徑之外,加上file:${demo.web.upload-path}指向外部的文件資源上傳路徑,即:該路徑下的靜態資源可以直接對外提供HTTP訪問服務
/*** 本地上傳* @param file* @param request* @return*/@RequestMapping("/file")public R fileSave(MultipartFile file, HttpServletRequest request) {if (file == null) {throw new RRException("參數為空");}// 在 uploadPath 文件夾中通過日期對上傳的文件歸類保存// 例如:/2022/02/22/df9a66f1-760b-4f95-9faf-b5a216966718.pngString format = sdf.format(new Date());File folder = new File(uploadPath + format);if (!folder.isDirectory()) {folder.mkdirs();}// 對上傳的文件重命名, 避免文件重名String oldName = file.getOriginalFilename();String newName = UUID.randomUUID().toString()+ oldName.substring(oldName.lastIndexOf("."), oldName.length());try {// 文件保存file.transferTo(new File(folder, newName));// 添加日志輸出logger.info("文件保存成功:" + folder.getPath() + File.separator + newName);// 返回上傳文件的訪問路徑// 例如:http://localhost:9999/2022/02/22/df9a66f1-760b-4f95-9faf-b5a216966718.pngString filePath = request.getScheme() + "://" + request.getServerName()+ ":" + request.getServerPort() + request.getContextPath() + "/" + format + newName;return R.ok().put("filePath", filePath);} catch (IOException e) {throw new RRException("系統錯誤");}}
前端
參考:spring boot 整合 minio存儲 【使用篇】
返回得到一個地址,即可訪問
問題
- 已解決
由于該項目涉及token,訪問鏈接報錯
但我看網絡里又有token
已解決
通過訪問路徑解決token問題
<el-uploadclass="upload-demo"ref="upload"dragaction="#":on-change="handleChangeSelect":on-exceed="handleExceed":file-list="fileList":limit="1"multiple:auto-upload="false"><i class="el-icon-upload"></i><div class="el-upload__text">將文件拖到此處,或<em>點擊上傳</em></div><divclass="el-upload__tip"slot="tip">只能上傳jpg/png文件,且不超過500kb</div><div class="el-upload__tip" slot="tip">訪問路徑:<a :href="`${filePath}?token=${token}`" target="_blank">點擊跳轉{{ filePath }}</a>
</div></el-upload>
創建token變量獲取
this.token = this.$cookie.get('token')
訪問路徑404問題
修改yml配置文件
修改前
spring:web:resources:static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${demo.web.upload-path}
修改后
spring:resources:static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${demo.web.upload-path}
# 設置Http能訪問的本地資源路徑
demo:web:upload-path: D:/MineFile/zuoye/xm/equipment-management-system/qhjdata/
參考
SpringBoot實現本地文件存儲及預覽