第一次接觸ruoyi框架,碰到文件上傳和下載問題,今天來總結一下。
使用若依框架文件上傳下載首先配置文件路徑要配好。
文件下載:
application.yml若依配置
# 項目相關配置
ruoyi:
# 名稱
name: RuoYi
# 版本
version: 3.6.0
# 版權年份
copyrightYear: 2021
# 實例演示開關
demoEnabled: true
# 文件路徑 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)
# profile: /home/admin2409/fn/uploadPath
profile: D:/.code/uploadPath
# 獲取ip地址開關
addressEnabled: false
# 驗證碼類型 math 數組計算 char 字符驗證
captchaType: math
首先是文件下載,在若依框架下載上傳文件工具已經寫好了
頁面:
前端方法:`
// 通用下載方法
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; //注意這里的路徑要和你下載的路徑對應
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
});
},
后端上傳代碼
/**
* 上傳平面圖
*/
@Log(title = "上傳平面圖" , businessType = BusinessType.UPDATE)
@PostMapping("/uplaodImg")
public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException {
if (!file.isEmpty()) {
String url= RuoYiConfig.getUploadPath();//配置自己的路徑
String avatar = FileUploadUtils.upload(url, file);
AjaxResult ajax = AjaxResult.success();
ajax.put("imgUrl" , avatar);
return ajax;
}
return AjaxResult.error("上傳圖片異常,請聯系管理員");
}
String url= RuoYiConfig.getUploadPath();//配置自己的路徑文章來源:http://www.zghlxwxcb.cn/news/detail-706002.html
/**
* 獲取上傳路徑
*/
public static String getUploadPath() {
return getProfile() + "/upload";
}
上傳成功:文章來源地址http://www.zghlxwxcb.cn/news/detail-706002.html
到了這里,關于SpringBoot+ruoyi框架圖片上傳和文件下載的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!