文件上传位置配置(在若依yml配置文件中):
1、文件上传
/**
* 文件上传
* @param file 文件
* @return
* @throws Exception
*/
public AjaxResult importFile(MultipartFile file) throws Exception {
try {
// 上传文件路径
String filePath = RuoYiConfig.getUploadPath();
// 上传并返回新文件名称
String fileName = FileUploadUtils.upload(filePath, file);
String url = serverConfig.getUrl() + fileName;
AjaxResult ajax = AjaxResult.success();
// 源文件名字。 例如:测试.pdf
ajax.put("originalFileName", file.getOriginalFilename());
// /profile/upload/2024/08/06/f17a614b-6456-4634-b53b-50aa26125430.txt
ajax.put("fileName", fileName);
// 文件具体地址: http://127.0.0.1:8802/profile/upload/2024/08/06/f17a614b-6456-4634-b53b-50aa26125430.txt
ajax.put("fileUrl", url);
return ajax;
}
catch (Exception e)
{
return AjaxResult.error(e.getMessage());
}
}
2、文件删除
/**
* 文件删除
* @param url 文件地址
* @return
*/
public AjaxResult deleteFile(String url) {
// 本地资源路径
String localPath = RuoYiConfig.getProfile();
boolean flag = false;
//删除本地文件
// 数据库资源地址
String downloadPath = localPath + StringUtils.substringAfter(url, Constants.RESOURCE_PREFIX);
File file = new File(downloadPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
if (flag) {
return AjaxResult.success("删除成功");
}
return AjaxResult.error("删除失败");
}