From c23f369aa619d7ede418b80eaa4322443d22f43b 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: Mon, 5 Jun 2023 13:37:37 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E6=96=B0=E5=A2=9E=20OssClient=20File=20?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dromara/common/oss/core/OssClient.java | 17 ++++++++++++++ .../resource/service/ISysOssService.java | 4 ++++ .../service/impl/SysOssServiceImpl.java | 22 +++++++++++++++---- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java index 2578559c..62ca6b14 100644 --- a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java +++ b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java @@ -24,6 +24,7 @@ import org.dromara.common.oss.exception.OssException; import org.dromara.common.oss.properties.OssProperties; import java.io.ByteArrayInputStream; +import java.io.File; import java.io.InputStream; import java.net.URL; import java.util.Date; @@ -115,6 +116,18 @@ public class OssClient { return UploadResult.builder().url(getUrl() + "/" + path).filename(path).build(); } + public UploadResult upload(File file, String path) { + try { + PutObjectRequest putObjectRequest = new PutObjectRequest(properties.getBucketName(), path, file); + // 设置上传对象的 Acl 为公共读 + putObjectRequest.setCannedAcl(getAccessPolicy().getAcl()); + client.putObject(putObjectRequest); + } catch (Exception e) { + throw new OssException("上传文件失败,请检查配置信息:[" + e.getMessage() + "]"); + } + return UploadResult.builder().url(getUrl() + "/" + path).filename(path).build(); + } + public void delete(String path) { path = path.replace(getUrl() + "/", ""); try { @@ -132,6 +145,10 @@ public class OssClient { return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType); } + public UploadResult uploadSuffix(File file, String suffix) { + return upload(file, getPath(properties.getPrefix(), suffix)); + } + /** * 获取文件元数据 * 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 430b7fdd..ce3622ad 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 @@ -7,6 +7,8 @@ import org.dromara.resource.domain.vo.SysOssVo; import org.springframework.web.multipart.MultipartFile; import jakarta.servlet.http.HttpServletResponse; + +import java.io.File; import java.io.IOException; import java.util.Collection; import java.util.List; @@ -28,6 +30,8 @@ public interface ISysOssService { SysOssVo upload(MultipartFile file); + SysOssVo upload(File file); + Boolean insertByBo(SysOssBo bo); void download(Long ossId, HttpServletResponse response) throws IOException; 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 607cff97..33eeaf5b 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 @@ -7,6 +7,7 @@ import cn.hutool.core.util.ObjectUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import org.dromara.common.core.constant.CacheNames; import org.dromara.common.core.exception.ServiceException; @@ -30,7 +31,7 @@ import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import jakarta.servlet.http.HttpServletResponse; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -133,15 +134,28 @@ public class SysOssServiceImpl implements ISysOssService { throw new ServiceException(e.getMessage()); } // 保存文件信息 + return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult); + } + + @Override + public SysOssVo upload(File file) { + String originalfileName = file.getName(); + String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length()); + OssClient storage = OssFactory.instance(); + UploadResult uploadResult = storage.uploadSuffix(file, suffix); + // 保存文件信息 + return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult); + } + + private SysOssVo buildResultEntity(String originalfileName, String suffix, String configKey, UploadResult uploadResult) { SysOss oss = new SysOss(); oss.setUrl(uploadResult.getUrl()); oss.setFileSuffix(suffix); oss.setFileName(uploadResult.getFilename()); oss.setOriginalName(originalfileName); - oss.setService(storage.getConfigKey()); + oss.setService(configKey); baseMapper.insert(oss); - SysOssVo sysOssVo = new SysOssVo(); - MapstructUtils.convert(oss, sysOssVo); + SysOssVo sysOssVo = MapstructUtils.convert(oss, SysOssVo.class); return this.matchingUrl(sysOssVo); }