From 71107ee2e4c8e024ddf65ee7f6dbe5fc75698a87 Mon Sep 17 00:00:00 2001 From: fengb Date: Thu, 5 Oct 2023 08:50:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4oss=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E4=BD=BF=E7=94=A8=E6=9E=B6=E6=9E=84?= =?UTF-8?q?=E8=87=AA=E5=B8=A6=E7=9A=84=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengb --- .../hiver/mall/controller/OSSController.java | 48 ----------------- .../java/cc/hiver/mall/utils/OssUtil.java | 53 ------------------- 2 files changed, 101 deletions(-) delete mode 100644 hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/controller/OSSController.java delete mode 100644 hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/utils/OssUtil.java diff --git a/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/controller/OSSController.java b/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/controller/OSSController.java deleted file mode 100644 index cf705e2f..00000000 --- a/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/controller/OSSController.java +++ /dev/null @@ -1,48 +0,0 @@ -package cc.hiver.mall.controller; - -import cc.hiver.core.common.utils.ResultUtil; -import cc.hiver.core.common.vo.Result; -import cc.hiver.mall.utils.OssUtil; -import com.aliyun.oss.OSS; -import com.aliyun.oss.OSSClient; -import com.aliyun.oss.model.OSSObject; -import io.swagger.annotations.Api; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.io.ByteArrayResource; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.util.ObjectUtils; -import org.springframework.web.bind.annotation.*; -import org.springframework.web.multipart.MultipartFile; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; - -/** - * @类描述 OSS文件传输接口 - * @作者 冯彬 - * @时间 2023/10/1-下午11:19 - */ -@Slf4j -@RestController -@Api(tags = "文件传输接口") -@RequestMapping(value = "/hiver/mall/oss/") -public class OSSController { - - @Autowired - private OssUtil ossUtil; - - @PostMapping("/upload") - public Result uploadFile(@RequestParam("file") MultipartFile file) throws Exception { - if (ObjectUtils.isEmpty(file)){ - return ResultUtil.error("上传失败,文件为空。"); - } - String objectName = file.getOriginalFilename(); // 文件名作为对象名称 - InputStream inputStream = file.getInputStream(); - String path = ossUtil.uploadFile(objectName,inputStream); // 上传文件并返回文件URL - return ResultUtil.success(path); - } - -} diff --git a/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/utils/OssUtil.java b/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/utils/OssUtil.java deleted file mode 100644 index a7e933bf..00000000 --- a/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/utils/OssUtil.java +++ /dev/null @@ -1,53 +0,0 @@ -package cc.hiver.mall.utils; - -import com.aliyun.oss.ClientException; -import com.aliyun.oss.OSS; -import com.aliyun.oss.OSSClientBuilder; -import com.aliyun.oss.OSSException; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -import java.io.File; -import java.io.InputStream; -import java.net.URL; -import java.util.Calendar; -import java.util.Date; - -/** - * @类描述 - * @作者 冯彬 - * @时间 2023/10/1-下午11:45 - */ - -@Component -public class OssUtil { - @Value("${aliyun.oss.endpoint}") - private String endpoint; - @Value("${aliyun.oss.accessKeyId}") - private String accessKeyId; - @Value("${aliyun.oss.accessKeySecret}") - private String accessKeySecret; - @Value("${aliyun.oss.bucketName}") - private String bucketName; - - public OSS getOssClient() { - return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); - } - - public String uploadFile(String objectName, InputStream file) throws ClientException, OSSException { - OSS ossClient = getOssClient(); - ossClient.putObject(bucketName, objectName, file); - ossClient.shutdown(); - Date currentDate = new Date(); - // 创建Calendar对象并设置为当前日期 - Calendar calendar = Calendar.getInstance(); - calendar.setTime(currentDate); - // 添加10000天 - calendar.add(Calendar.DAY_OF_MONTH, 10000); - // 获取10000天后的日期 - Date expirationDate = calendar.getTime(); - URL url = ossClient.generatePresignedUrl(bucketName, objectName, expirationDate); - String path = url.getPath(); - return path; - } -}