From 21c77f76dba6ef0232f4d3ec047dd3f638f81b1b Mon Sep 17 00:00:00 2001 From: xd <844539747@qq.com> Date: Sun, 24 Mar 2024 08:58:04 +0800 Subject: [PATCH] '123' --- .../controller/common/CommonController.java | 8 +-- .../system/SysProfileController.java | 2 +- .../src/main/resources/application.yml | 2 +- .../common/utils/file/FileUploadUtils.java | 54 +++++++++++++++---- .../ruoyi/common/utils/file/MinioUtil.java | 19 +++++-- .../mapper/system/SysNoticeMapper.xml | 26 ++++----- ruoyi-ui/src/components/Editor/index.vue | 5 +- ruoyi-ui/src/components/FileUpload/index.vue | 2 +- ruoyi-ui/src/components/ImageUpload/index.vue | 2 +- 9 files changed, 81 insertions(+), 39 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java index 6def5d9..e06ee50 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java @@ -83,7 +83,7 @@ public class CommonController // 上传文件路径 String filePath = RuoYiConfig.getUploadPath(); // 上传并返回新文件名称 - String fileName = FileUploadUtils.upload(filePath, file); + String fileName = FileUploadUtils.upload(filePath,"", file); String url = serverConfig.getUrl() + fileName; AjaxResult ajax = AjaxResult.success(); ajax.put("url", url); @@ -115,7 +115,7 @@ public class CommonController for (MultipartFile file : files) { // 上传并返回新文件名称 - String fileName = FileUploadUtils.upload(filePath, file); + String fileName = FileUploadUtils.upload(filePath,"", file); String url = serverConfig.getUrl() + fileName; urls.add(url); fileNames.add(fileName); @@ -168,7 +168,7 @@ public class CommonController * Minio 服务器上传请求(单文件上传) */ @PostMapping("/minio-upload") - public AjaxResult uploadFileMinio(String dir,MultipartFile file) throws Exception + public AjaxResult uploadFileMinio(MultipartFile file) throws Exception { if(file == null){ return AjaxResult.error("不能上传空文件"); @@ -176,7 +176,7 @@ public class CommonController try { // 上传并返回新文件名称 - String fileName = FileUploadUtils.uploadMinio(dir,file); + String fileName = FileUploadUtils.uploadMinio(file); AjaxResult ajax = AjaxResult.success(); ajax.put("url", fileName); ajax.put("fileName", fileName); diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java index d60384a..f0c3c20 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java @@ -127,7 +127,7 @@ public class SysProfileController extends BaseController } /** - * 头像上传 + * 头像上传SysNoticeMapper */ @Log(title = "用户头像", businessType = BusinessType.UPDATE) @PostMapping("/avatar") diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index 7f97f81..2243b78 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -201,7 +201,7 @@ magic-api: # Minio配置-分布式存储 minio: useEnable: true - url: http://192.168.9.134:3336 + url: http://localhost:3336 accessKey: minio-admin secretKey: minio-jndlitzx bucketName: system diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java index 14a44f2..d38dc95 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java @@ -43,6 +43,11 @@ public class FileUploadUtils */ private static final String BUCKET_NAME = MinioConfig.getBucketName(); + /** + * 本地默认分类目录 + */ + private static String DIR = ""; + public static void setDefaultBaseDir(String defaultBaseDir) { FileUploadUtils.defaultBaseDir = defaultBaseDir; @@ -60,16 +65,26 @@ public class FileUploadUtils /** * 以默认配置进行文件上传 - * * @param file 上传的文件 * @return 文件名称 * @throws IOException 写入异常 */ - public static String upload(MultipartFile file) throws IOException + public static String upload(MultipartFile file) throws IOException { + return upload(file,DIR); + } + + /** + * 以默认配置进行文件上传 + * @param dir 自定义分类目录 + * @param file 上传的文件 + * @return 文件名称 + * @throws IOException 写入异常 + */ + public static String upload(MultipartFile file,String dir) throws IOException { try { - return upload(getDefaultBaseDir(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); + return upload(getDefaultBaseDir(), dir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); } catch (Exception e) { @@ -81,15 +96,16 @@ public class FileUploadUtils * 根据文件路径上传 * * @param baseDir 相对应用的基目录 + * @param dir 自定义分类目录 * @param file 上传的文件 * @return 文件名称 * @throws IOException 写入异常 */ - public static String upload(String baseDir, MultipartFile file) throws IOException + public static String upload(String baseDir, String dir, MultipartFile file) throws IOException { try { - return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); + return upload(baseDir,dir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); } catch (Exception e) { @@ -101,6 +117,7 @@ public class FileUploadUtils * 文件上传 * * @param baseDir 相对应用的基目录 + * @param dir 自定义分类目录 * @param file 上传的文件 * @param allowedExtension 上传文件类型 * @return 返回上传成功的文件名 @@ -109,7 +126,7 @@ public class FileUploadUtils * @throws IOException 比如读写文件出错时 * @throws InvalidExtensionException 文件校验异常 */ - public static String upload(String baseDir, MultipartFile file, String[] allowedExtension) throws Exception + public static String upload(String baseDir, String dir, MultipartFile file, String[] allowedExtension) throws Exception { int fileNameLength = Objects.requireNonNull(file.getOriginalFilename()).length(); if (fileNameLength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) @@ -119,13 +136,22 @@ public class FileUploadUtils assertAllowed(file, allowedExtension); - String fileName = extractFilename(file); + String fileName = extractFilename(dir,file); String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath(); file.transferTo(Paths.get(absPath)); return getPathFileName(baseDir, fileName); } + /** + * 以默认BucketName配置上传到Minio服务器 + * @param file 上传的文件 + * @return 文件名称 + * @throws IOException 写入异常 + */ + public static String uploadMinio(MultipartFile file) throws IOException { + return uploadMinio(DIR,file); + } /** * 以默认BucketName配置上传到Minio服务器 * @@ -175,8 +201,8 @@ public class FileUploadUtils assertAllowed(file, allowedExtension); try { - String fileName = extractFilename(file); - return MinioUtil.uploadFile(bucketName,dir, fileName, file); + String fileName = extractFilename(dir,file); + return MinioUtil.uploadFile(bucketName,fileName, file); } catch (Exception e) { @@ -187,9 +213,15 @@ public class FileUploadUtils /** * 编码文件名 */ - public static String extractFilename(MultipartFile file) + public static String extractFilename(String dir,MultipartFile file) { - return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(), + String template = "{}/{}_{}.{}"; + if(StringUtils.isNotEmpty(dir)){ + template = "{}/{}/{}_{}.{}"; + return StringUtils.format(template, dir,DateUtils.datePath(), + FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file)); + } + return StringUtils.format(template, DateUtils.datePath(), FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file)); } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MinioUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MinioUtil.java index 5829a0f..2c85c25 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MinioUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MinioUtil.java @@ -37,19 +37,18 @@ public class MinioUtil * 上传文件 * * @param bucketName 桶名称 - * @param dir 分类目录 * @param fileName 文件名 * @throws IOException 写入异常 */ - public static String uploadFile(String bucketName,String dir, String fileName, MultipartFile multipartFile) throws IOException + public static String uploadFile(String bucketName,String fileName, MultipartFile multipartFile) throws IOException { String url = ""; try (InputStream inputStream = multipartFile.getInputStream()) { boolean exist = existBucket(bucketName); if(exist){ - MINIO_CLIENT.putObject(PutObjectArgs.builder().bucket(bucketName).object(dir+"/"+fileName).stream(inputStream, multipartFile.getSize(), -1).contentType(multipartFile.getContentType()).build()); - url = MINIO_CLIENT.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(dir+"/"+fileName).method(Method.GET).build()); + MINIO_CLIENT.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).stream(inputStream, multipartFile.getSize(), -1).contentType(multipartFile.getContentType()).build()); + url = MINIO_CLIENT.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(fileName).method(Method.GET).build()); url = url.substring(0, url.indexOf('?')); } return ServletUtils.urlDecode(url); @@ -112,12 +111,22 @@ public class MinioUtil return true; } + /** * @description 上传文件 * @param multipartFile 文件资源 * @return java.lang.String */ public static List> upload(MultipartFile[] multipartFile) { + return upload(BUCKET_NAME,multipartFile); + } + + /** + * @description 上传文件 + * @param multipartFile 文件资源 + * @return java.lang.String + */ + public static List> upload(String bucketName,MultipartFile[] multipartFile) { List> res = new ArrayList<>(multipartFile.length); for (MultipartFile file : multipartFile) { String fileName = file.getOriginalFilename(); @@ -132,7 +141,7 @@ public class MinioUtil in = file.getInputStream(); MINIO_CLIENT.putObject( PutObjectArgs.builder() - .bucket(BUCKET_NAME).object(fileName).stream(in, in.available(), -1) + .bucket(bucketName).object(fileName).stream(in, in.available(), -1) .contentType(file.getContentType()) .build()); } catch (Exception e) { diff --git a/ruoyi-system/src/main/resources/mapper/system/SysNoticeMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysNoticeMapper.xml index a23e823..3e4a7c0 100644 --- a/ruoyi-system/src/main/resources/mapper/system/SysNoticeMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/SysNoticeMapper.xml @@ -3,7 +3,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> - + @@ -16,17 +16,17 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - + select notice_id, notice_title, notice_type, notice_content, status, create_by, create_time, update_by, update_time, remark from sys_notice - + - + - + insert into sys_notice ( notice_title, @@ -54,16 +54,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" )values( #{noticeTitle}, #{noticeType}, - notice_content = #{noticeContentBit}, + #{noticeContentBit}, #{status}, #{remark}, #{createBy}, getdate() ) - + - update sys_notice + update sys_notice notice_title = #{noticeTitle}, notice_type = #{noticeType}, @@ -74,16 +74,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" where notice_id = #{noticeId} - + delete from sys_notice where notice_id = #{noticeId} - + - delete from sys_notice where notice_id in + delete from sys_notice where notice_id in #{noticeId} - - \ No newline at end of file + + diff --git a/ruoyi-ui/src/components/Editor/index.vue b/ruoyi-ui/src/components/Editor/index.vue index 8981d76..4a494f4 100644 --- a/ruoyi-ui/src/components/Editor/index.vue +++ b/ruoyi-ui/src/components/Editor/index.vue @@ -60,7 +60,7 @@ export default { }, data() { return { - uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址 + uploadUrl: process.env.VUE_APP_BASE_API + "/common/minio-upload", // 上传的图片服务器地址 headers: { Authorization: "Bearer " + getToken() }, @@ -182,7 +182,8 @@ export default { // 获取光标所在位置 let length = quill.getSelection().index; // 插入图片 res.url为服务器返回的图片地址 - quill.insertEmbed(length, "image", process.env.VUE_APP_BASE_API + res.fileName); + //quill.insertEmbed(length, "image", process.env.VUE_APP_BASE_API + res.fileName); + quill.insertEmbed(length, "image", res.fileName); // 调整光标到最后 quill.setSelection(length + 1); } else { diff --git a/ruoyi-ui/src/components/FileUpload/index.vue b/ruoyi-ui/src/components/FileUpload/index.vue index c7f6b0a..6cc06a6 100644 --- a/ruoyi-ui/src/components/FileUpload/index.vue +++ b/ruoyi-ui/src/components/FileUpload/index.vue @@ -73,7 +73,7 @@ export default { number: 0, uploadList: [], baseUrl: process.env.VUE_APP_BASE_API, - uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址 + uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/minio-upload", // 上传文件服务器地址 headers: { Authorization: "Bearer " + getToken(), }, diff --git a/ruoyi-ui/src/components/ImageUpload/index.vue b/ruoyi-ui/src/components/ImageUpload/index.vue index 8211376..f749a43 100644 --- a/ruoyi-ui/src/components/ImageUpload/index.vue +++ b/ruoyi-ui/src/components/ImageUpload/index.vue @@ -77,7 +77,7 @@ export default { dialogVisible: false, hideUpload: false, baseUrl: process.env.VUE_APP_BASE_API, - uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址 + uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/minio-upload", // 上传的图片服务器地址 headers: { Authorization: "Bearer " + getToken(), },