SpringBoot+ruoyi框架文件上傳和下載的實(shí)現(xiàn)
第一次接觸ruoyi框架,碰到文件上傳和下載問(wèn)題,今天來(lái)總結(jié)一下。使用若依框架文件上傳下載首先配置文件路徑要配好。
文件下載:
application.yml若依配置
# 項(xiàng)目相關(guān)配置 ruoyi: # 名稱 name: RuoYi # 版本 version: 3.6.0 # 版權(quán)年份 copyrightYear: 2021 # 實(shí)例演示開關(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 # 驗(yàn)證碼類型 math 數(shù)組計(jì)算 char 字符驗(yàn)證 captchaType: math

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

前端方法:`
// 通用下載方法
export function download(fileName) {
window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
}后端通用方法:
單獨(dú)寫一個(gè)下載啊文件的請(qǐng)求
@GetMapping("/downloadTemplate")
public AjaxResult importTemplate() throws IOException {
return AjaxResult.success("hnxTemplate.xlsx");
}/**
* 通用下載請(qǐng)求
*
* @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; //注意這里的路徑要和你下載的路徑對(duì)應(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同樣的路徑配置不變頁(yè)面:

前端代碼:
<el-form-item label="場(chǎng)景圖片:" 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框架圖片上傳和文件下載的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot ruoy圖片上傳和下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot實(shí)現(xiàn)文件的上傳、下載和預(yù)覽功能
- SpringBoot實(shí)現(xiàn)文件下載的限速功能
- SpringBoot上傳下載文件+oss實(shí)例
- SpringBoot中實(shí)現(xiàn)文件上傳、下載、刪除功能的步驟
- SpringBoot實(shí)現(xiàn)文件下載的四種方式
- Vue2+SpringBoot實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出到csv文件并下載的使用示例
- SpringBoot+MinIO實(shí)現(xiàn)文件上傳、讀取、下載、刪除的使用示例
- SpringBoot返回文件使前端下載的幾種方式小結(jié)
相關(guān)文章
spring注解如何為bean指定InitMethod和DestroyMethod
這篇文章主要介紹了spring注解如何為bean指定InitMethod和DestroyMethod,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
mybatis3.4.0不支持LocalDateTime的解決方法(No typehandler f
本文主要介紹了mybatis3.4.0不支持LocalDateTime的解決方法(No typehandler found for property time),具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03
Java中基于maven實(shí)現(xiàn)zxing二維碼功能
這篇文章主要介紹了Java中基于maven實(shí)現(xiàn)zxing二維碼功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
Java中l(wèi)ogback?自動(dòng)刷新不生效的問(wèn)題解決
本文主要介紹了Java中l(wèi)ogback?自動(dòng)刷新不生效的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
java實(shí)現(xiàn)的海盜算法優(yōu)化版
這篇文章主要介紹了java實(shí)現(xiàn)的海盜算法優(yōu)化版,結(jié)合實(shí)例形式分析了java海盜算法的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-07-07
使用eclipse快速新建spirngboot項(xiàng)目的方法
本篇文章主要介紹了使用eclipse快速新建spirngboot項(xiàng)目的方法,具有一定的參考價(jià)值,有興趣的可以了解一下2017-04-04
如何對(duì)spring框架的搭建進(jìn)行封裝--springboot
這篇文章主要介紹了如何對(duì)spring框架的搭建進(jìn)行封裝--springboot,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03

