From 4a5ee4306dc97fb23f2d54f91e4907891a03dcf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Thu, 11 Jun 2026 13:59:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=8A=E4=BC=A0=20OSS=20=E9=99=84?= =?UTF-8?q?=E4=BB=B6=E6=94=AF=E6=8C=81=E6=89=A9=E5=B1=95=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resource/controller/SysOssController.java | 6 +++-- .../resource/service/ISysOssService.java | 27 +++++++++++++++++-- .../service/impl/SysOssServiceImpl.java | 14 +++++----- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/controller/SysOssController.java b/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/controller/SysOssController.java index e74d8acf3..b65122679 100644 --- a/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/controller/SysOssController.java +++ b/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/controller/SysOssController.java @@ -7,10 +7,12 @@ import lombok.RequiredArgsConstructor; import org.dromara.common.core.domain.PageResult; import org.dromara.common.core.domain.R; import org.dromara.common.core.validate.QueryGroup; +import org.dromara.common.json.utils.JsonUtils; import org.dromara.common.log.annotation.Log; import org.dromara.common.log.enums.BusinessType; import org.dromara.common.mybatis.core.page.PageQuery; import org.dromara.common.web.core.BaseController; +import org.dromara.resource.domain.SysOssExt; import org.dromara.resource.domain.bo.SysOssBo; import org.dromara.resource.domain.vo.SysOssUploadVo; import org.dromara.resource.domain.vo.SysOssVo; @@ -67,8 +69,8 @@ public class SysOssController extends BaseController { @SaCheckPermission("system:oss:upload") @Log(title = "OSS对象存储", businessType = BusinessType.INSERT) @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - public R upload(@RequestPart("file") MultipartFile file) { - SysOssVo oss = ossService.upload(file); + public R upload(@RequestPart("file") MultipartFile file, @RequestParam(value = "ossExt", required = false) String ossExtJson) { + SysOssVo oss = ossService.upload(file, JsonUtils.parseObject(ossExtJson, SysOssExt.class)); SysOssUploadVo uploadVo = new SysOssUploadVo(); uploadVo.setUrl(oss.getUrl()); uploadVo.setFileName(oss.getOriginalName()); diff --git a/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/service/ISysOssService.java b/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/service/ISysOssService.java index 7c311188a..378e94238 100644 --- a/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/service/ISysOssService.java +++ b/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/service/ISysOssService.java @@ -2,6 +2,7 @@ package org.dromara.resource.service; import org.dromara.common.core.domain.PageResult; import org.dromara.common.mybatis.core.page.PageQuery; +import org.dromara.resource.domain.SysOssExt; import org.dromara.resource.domain.bo.SysOssBo; import org.dromara.resource.domain.vo.SysOssVo; import org.springframework.http.ResponseEntity; @@ -57,7 +58,18 @@ public interface ISysOssService { * @param file 要上传的 MultipartFile 对象 * @return 上传成功后的 SysOssVo 对象,包含文件信息 */ - SysOssVo upload(MultipartFile file); + default SysOssVo upload(MultipartFile file) { + return upload(file, null); + } + + /** + * 上传 MultipartFile 到对象存储服务,并保存文件信息到数据库 + * + * @param file 要上传的 MultipartFile 对象 + * @param ossExt 扩展信息 + * @return 上传成功后的 SysOssVo 对象,包含文件信息 + */ + SysOssVo upload(MultipartFile file, SysOssExt ossExt); /** * 上传文件到对象存储服务,并保存文件信息到数据库 @@ -65,7 +77,18 @@ public interface ISysOssService { * @param file 要上传的文件对象 * @return 上传成功后的 SysOssVo 对象,包含文件信息 */ - SysOssVo upload(File file); + default SysOssVo upload(File file) { + return upload(file, null); + } + + /** + * 上传文件到对象存储服务,并保存文件信息到数据库 + * + * @param file 要上传的文件对象 + * @param ossExt 扩展信息 + * @return 上传成功后的 SysOssVo 对象,包含文件信息 + */ + SysOssVo upload(File file, SysOssExt ossExt); /** * 新增OSS对象存储 diff --git a/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/service/impl/SysOssServiceImpl.java b/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/service/impl/SysOssServiceImpl.java index 230291476..da2fa4ac9 100644 --- a/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/service/impl/SysOssServiceImpl.java +++ b/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/service/impl/SysOssServiceImpl.java @@ -196,7 +196,7 @@ public class SysOssServiceImpl implements ISysOssService { * @throws ServiceException 如果上传过程中发生异常,则抛出 ServiceException 异常 */ @Override - public SysOssVo upload(MultipartFile file) { + public SysOssVo upload(MultipartFile file, SysOssExt ossExt) { if (ObjectUtil.isNull(file) || file.isEmpty()) { throw new ServiceException("上传文件不能为空"); } @@ -206,11 +206,11 @@ public class SysOssServiceImpl implements ISysOssService { String pathKey = instance.buildPathKey(originalfileName); try (InputStream inputStream = file.getInputStream()) { PutObjectResult result = instance.upload(pathKey, inputStream, file.getSize(), Options.builder().setContentType(file.getContentType())); - SysOssExt ext1 = new SysOssExt(); - ext1.setFileSize(file.getSize()); - ext1.setContentType(file.getContentType()); + ossExt = ossExt == null ? new SysOssExt() : ossExt; + ossExt.setFileSize(file.getSize()); + ossExt.setContentType(file.getContentType()); // 保存文件信息 - return buildResultEntity(originalfileName, suffix, instance.clientId(), result, ext1); + return buildResultEntity(originalfileName, suffix, instance.clientId(), result, ossExt); } catch (IOException e) { throw new ServiceException(e.getMessage()); } @@ -223,7 +223,7 @@ public class SysOssServiceImpl implements ISysOssService { * @return 上传成功后的 SysOssVo 对象,包含文件信息 */ @Override - public SysOssVo upload(File file) { + public SysOssVo upload(File file, SysOssExt ossExt) { if (ObjectUtil.isNull(file) || !file.isFile() || file.length() <= 0) { throw new ServiceException("上传文件不能为空"); } @@ -232,7 +232,7 @@ public class SysOssServiceImpl implements ISysOssService { OssClient instance = OssFactory.instance(); String pathKey = instance.buildPathKey(originalfileName); PutObjectResult result = instance.upload(pathKey, file, Options.builder().setContentType(FileUtils.getMimeType(file.toPath()))); - SysOssExt ext1 = new SysOssExt(); + SysOssExt ext1 = ossExt == null ? new SysOssExt() : ossExt; ext1.setFileSize(result.size()); // 保存文件信息 return buildResultEntity(originalfileName, suffix, instance.clientId(), result, ext1);