久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

SpringBoot實(shí)現(xiàn)本地存儲(chǔ)文件上傳及提供HTTP訪(fǎng)問(wèn)服務(wù)的方法

瀏覽:7日期:2023-05-03 09:45:44

筆者計(jì)劃為大家介紹分布式文件系統(tǒng),用于存儲(chǔ)應(yīng)用的圖片、word、excel、pdf等文件。在開(kāi)始介紹分布式文件系統(tǒng)之前,為大家介紹一下使用本機(jī)存儲(chǔ)來(lái)存放文件資源。二者的核心實(shí)現(xiàn)過(guò)程是一樣的:

上傳文件,保存文件(本節(jié)是本地磁盤(pán)) 返回文件HTTP訪(fǎng)問(wèn)服務(wù)路徑給前端,進(jìn)行上傳之后的效果展示

一、復(fù)習(xí)

服務(wù)端接收上傳的目的是提供文件的訪(fǎng)問(wèn)服務(wù),那么對(duì)于SpringBoot而言,有哪些可以提供文件訪(fǎng)問(wèn)的靜態(tài)資源目錄呢?

classpath:/META-INF/resources/ , classpath:/static/ , classpath:/public/ , classpath:/resources/

這是之前我們?yōu)榇蠹医榻B的內(nèi)容,從這里看出這里的靜態(tài)資源都在classpath下。那么就出現(xiàn)問(wèn)題:

應(yīng)用的文件資源不能和項(xiàng)目代碼分開(kāi)存儲(chǔ)(你見(jiàn)過(guò)往github上傳代碼,還附帶項(xiàng)目文件數(shù)據(jù)的么?) 項(xiàng)目打包困難,當(dāng)上傳的文件越來(lái)越多,項(xiàng)目的打包jar越來(lái)越大。 代碼與文件數(shù)據(jù)不能分開(kāi)存儲(chǔ),就意味著文件數(shù)據(jù)的備份將變得復(fù)雜

二、文件上傳目錄自定義配置

怎么解決上述問(wèn)題?別忘記了spring boot 為我們提供了使用spring.resources.static-locations配置自定義靜態(tài)文件的位置。

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為與項(xiàng)目代碼分離的靜態(tài)資源路徑,即:文件上傳保存根路徑 配置spring.resources.static-locations,除了帶上Spring Boot默認(rèn)的靜態(tài)資源路徑之外,加上file:${web.upload-path}指向外部的文件資源上傳路徑。該路徑下的靜態(tài)資源可以直接對(duì)外提供HTTP訪(fǎng)問(wèn)服務(wù)。

三、文件上傳的Controller實(shí)現(xiàn)

詳情看代碼注釋

@RestControllerpublic 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 文件夾中通過(guò)日期對(duì)上傳的文件歸類(lèi)保存 // 比如:/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(); } // 對(duì)上傳的文件重命名,避免文件重名 String oldName = uploadFile.getOriginalFilename(); String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf('.'), oldName.length()); try { // 文件保存 uploadFile.transferTo(new File(folder, newName)); // 返回上傳文件的訪(fǎng)問(wèn)路徑 String filePath = request.getScheme() + '://' + request.getServerName() + ':' + request.getServerPort() + format + newName; return filePath; } catch (IOException e) { throw new CustomException(CustomExceptionType.SYSTEM_ERROR); } }}

四、寫(xiě)一個(gè)模擬的文件上傳頁(yè)面,進(jìn)行測(cè)試

把該upload.html文件放到classpath:public目錄下,對(duì)外提供訪(fǎng)問(wèn)。

<!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='請(qǐng)選擇上傳文件'> <input type='submit' value='保存'></form></body></html>

訪(fǎng)問(wèn)測(cè)試、點(diǎn)擊“選擇文件”,之后保存

SpringBoot實(shí)現(xiàn)本地存儲(chǔ)文件上傳及提供HTTP訪(fǎng)問(wèn)服務(wù)的方法

文件被保存到服務(wù)端的web.upload-path指定的資源目錄下

SpringBoot實(shí)現(xiàn)本地存儲(chǔ)文件上傳及提供HTTP訪(fǎng)問(wèn)服務(wù)的方法

瀏覽器端響應(yīng)結(jié)果如下,返回一個(gè)文件HTTP訪(fǎng)問(wèn)路徑:

SpringBoot實(shí)現(xiàn)本地存儲(chǔ)文件上傳及提供HTTP訪(fǎng)問(wèn)服務(wù)的方法

使用該HTTP訪(fǎng)問(wèn)路徑,在瀏覽器端訪(fǎng)問(wèn)效果如下。證明我們的文件已經(jīng)成功上傳到服務(wù)端,以后需要訪(fǎng)問(wèn)該圖片就通過(guò)這個(gè)HTTP URL就可以了。

SpringBoot實(shí)現(xiàn)本地存儲(chǔ)文件上傳及提供HTTP訪(fǎng)問(wèn)服務(wù)的方法

到此這篇關(guān)于SpringBoot實(shí)現(xiàn)本地存儲(chǔ)文件上傳及提供HTTP訪(fǎng)問(wèn)服務(wù)的文章就介紹到這了,更多相關(guān)SpringBoot實(shí)現(xiàn)文件上傳和訪(fǎng)問(wèn)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 梁山县| 贡嘎县| 长泰县| 沾益县| 乾安县| 高密市| 周宁县| 天长市| 霞浦县| 仲巴县| 安宁市| 武夷山市| 高阳县| 塔城市| 玉林市| 土默特左旗| 平昌县| 宁海县| 宝兴县| 当雄县| 偏关县| 梧州市| 克拉玛依市| 娄底市| 临泽县| 罗城| 镶黄旗| 诏安县| 衡南县| 会泽县| 信宜市| 荥经县| 赣州市| 龙泉市| 三门县| 庆阳市| 宜兴市| 黔南| 嫩江县| 东丽区| 大埔区|