SpringBoot+ruoyi框架文件上傳和下載的實現(xiàn)
第一次接觸ruoyi框架,碰到文件上傳和下載問題,今天來總結(jié)一下。使用若依框架文件上傳下載首先配置文件路徑要配好。
文件下載:
application.yml若依配置
# 項目相關(guān)配置 ruoyi: # 名稱 name: RuoYi # 版本 version: 3.6.0 # 版權(quán)年份 copyrightYear: 2021 # 實例演示開關(guān) demoEnabled: true # 文件路徑 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath) # profile: /home/admin2409/fn/uploadPath profile: D:/.code/uploadPath # 獲取ip地址開關(guān) addressEnabled: false # 驗證碼類型 math 數(shù)組計算 char 字符驗證 captchaType: math

首先是文件下載,在若依框架下載上傳文件工具已經(jīng)寫好了頁面:

前端方法:`
// 通用下載方法
export function download(fileName) {
window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
}后端通用方法:
單獨寫一個下載啊文件的請求
@GetMapping("/downloadTemplate")
public AjaxResult importTemplate() throws IOException {
return AjaxResult.success("hnxTemplate.xlsx");
}/**
* 通用下載請求
*
* @param fileName 文件名稱
* @param delete 是否刪除
*/
@GetMapping("common/download")
public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
try {
if (!FileUtils.checkAllowDownload(fileName)) {
throw new Exception(StringUtils.format("文件名稱({})非法,不允許下載。 " , fileName));
}
String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
String filePath = RuoYiConfig.getDownloadPath() + fileName; //注意這里的路徑要和你下載的路徑對應(yīng)
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
FileUtils.setAttachmentResponseHeader(response, realFileName);
FileUtils.writeBytes(filePath, response.getOutputStream());
if (delete) {
FileUtils.deleteFile(filePath);
}
} catch (Exception e) {
log.error("下載文件失敗" , e);
}
}RuoYiConfig.getDownloadPath()如果和你的路徑不一樣,改成一樣的
/**
* 獲取下載路徑
*/
public static String getDownloadPath() {
return getProfile() + "/download/";
}

這樣就文件就可以下載了

文件上傳:
application.yml同樣的路徑配置不變頁面:

前端代碼:
<el-form-item label="場景圖片:" prop="sceneImgurl">
<el-upload
action=""
ref="uploadImport"
:http-request="httpRequest"
list-type="picture-card"
:limit="1"
:file-list="fileList"
accept=".jpg, .jpeg, .png, .gif"
:auto-upload="false"
:before-remove="removeImg"
>
<i class="el-icon-plus"></i>
</el-upload>
</el-form-item> httpRequest(param) {
let params = new FormData();
params.append('avatarfile', param.file); // 傳文件
uploadPlanImg(params).then(res => {
if(res.code!==200) return
this.form.sceneImgurl = res.imgUrl
});
},后端上傳代碼
httpRequest(param) {
let params = new FormData();
params.append('avatarfile', param.file); // 傳文件
uploadPlanImg(params).then(res => {
if(res.code!==200) return
this.form.sceneImgurl = res.imgUrl
});
},String url= RuoYiConfig.getUploadPath();//配置自己的路徑
/**
* 獲取上傳路徑
*/
public static String getUploadPath() {
return getProfile() + "/upload";
}上傳成功:

到此這篇關(guān)于SpringBoot+ruoyi框架圖片上傳和文件下載的實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot ruoy圖片上傳和下載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java如何利用線程池和Redis實現(xiàn)高效數(shù)據(jù)入庫
文章介紹了如何利用線程池和Redis在高并發(fā)環(huán)境中實現(xiàn)高效的數(shù)據(jù)入庫,通過將數(shù)據(jù)首先存儲在Redis緩存中,然后利用線程池定期批量入庫處理,確保系統(tǒng)的性能和穩(wěn)定性,主要組件包括BatchDataStorageService、CacheService和RedisUtils等2025-02-02
SpringBoot+Flying Saucer+Thymeleaf實現(xiàn)PDF生成的完整指南
在實際開發(fā)中,PDF生成是常見需求,本文將詳細(xì)講解如何基于 Spring Boot + Flying Saucer + Thymeleaf 實現(xiàn) PDF 生成,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2026-01-01
SpringBoot整合ZXing實現(xiàn)二維碼和條形碼的創(chuàng)建
如今我們越來越多的東西需要用到二維碼或者條形碼,商品的條形碼,付款的二維碼等等,所以本文小編給大家介紹了SpringBoot整合ZXing實現(xiàn)二維碼和條形碼的創(chuàng)建,文章通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
SpringBoot中SpringSecurity安全框架的基本配置與使用方式
Spring Security是Spring框架的安全性解決方案,提供了身份驗證、授權(quán)、認(rèn)證流程安全控制等全面安全功能,廣泛應(yīng)用于Java應(yīng)用程序中2026-03-03
SpringBoot項目中JDK動態(tài)代理和CGLIB動態(tài)代理的使用詳解
JDK動態(tài)代理和CGLIB動態(tài)代理都是SpringBoot中實現(xiàn)AOP的重要技術(shù),JDK動態(tài)代理通過反射生成代理類,適用于目標(biāo)類實現(xiàn)了接口的場景,性能較好,易用性高,但必須實現(xiàn)接口且不能代理final方法,CGLIB動態(tài)代理通過生成子類實現(xiàn)代理2025-03-03
在lambda的foreach遍歷中break退出操作(lambda foreach break)
這篇文章主要介紹了在lambda的foreach遍歷中break退出操作(lambda foreach break),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

