From adc8542adeabbcbeff425d6471762c04e8f3cffe Mon Sep 17 00:00:00 2001 From: Pu Zhibing <393733352@qq.com> Date: 星期三, 25 六月 2025 15:03:38 +0800 Subject: [PATCH] 修改功能和增加上传功能 --- ManagementQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/controller/util/UploadUtil.java | 123 ++++++++++++++++++++++++++++++++--------- 1 files changed, 96 insertions(+), 27 deletions(-) diff --git a/ManagementQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/controller/util/UploadUtil.java b/ManagementQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/controller/util/UploadUtil.java index b0a0d29..34e402f 100644 --- a/ManagementQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/controller/util/UploadUtil.java +++ b/ManagementQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/controller/util/UploadUtil.java @@ -1,11 +1,14 @@ package com.stylefeng.guns.modular.system.controller.util; +import com.heredata.hos.model.bucket.Bucket; import com.stylefeng.guns.config.properties.GunsProperties; import com.stylefeng.guns.core.base.controller.BaseController; import com.stylefeng.guns.core.common.exception.BizExceptionEnum; import com.stylefeng.guns.core.exception.GunsException; import com.stylefeng.guns.core.util.ToolUtil; import com.stylefeng.guns.modular.system.service.ITImgMidService; +import com.stylefeng.guns.modular.system.util.qianyuntong.NCOSSUtil; +import com.stylefeng.guns.modular.system.util.qianyuntong.QianYunTongProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -18,7 +21,6 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; -import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -66,14 +68,30 @@ MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile file = (MultipartFile) multipartRequest.getFile("myfile"); if (file.getSize() != 0) { - String fileName = UUID.randomUUID().toString() + "." + ToolUtil.getFileSuffix(file.getOriginalFilename()); - try { - String fileSavePath = gunsProperties.getFileUploadPath() + "img\\"; - file.transferTo(new File(fileSavePath + fileName)); - } catch (Exception e) { - e.printStackTrace(); + String pictureName = UUID.randomUUID().toString() + "." + ToolUtil.getFileSuffix(file.getOriginalFilename()); + String bucketName = QianYunTongProperties.bucketName; + Bucket grjyTest = NCOSSUtil.getBucketInfo(bucketName); + if (null == grjyTest) { + //创建桶 + Boolean bucket = NCOSSUtil.createBucket(bucketName); + if (!bucket) { + throw new RuntimeException("创建存储桶失败"); + } + //设置桶策略 + String policyText = "{\"Version\":\"2025-06-23\",\"Statement\":[{\"Sid\":\"Stmt20250623\",\"Action\":[\"GetObject\"],\"Effect\":\"Allow\",\"Resource\":\"" + bucketName + "/*\",\"Principal\":\"*\"}]}"; + Boolean bucketPolicy = NCOSSUtil.setBucketPolicy(bucketName, policyText); + if (!bucketPolicy) { + throw new RuntimeException("设置桶策略失败"); + } } - ossUpload = gunsProperties.getPictureServerAddress() + "img/" + fileName; + //上传对象 + String key = "imgs/management/" + pictureName; + String object = NCOSSUtil.putObject(bucketName, key, file.getInputStream()); + if (null == object) { + throw new RuntimeException("上传图片失败"); + } + + ossUpload = "https://traffic.qytzt.cn/v1/AUTH_" + QianYunTongProperties.account + "/" + bucketName + "/" + key; // ossUpload = OssUploadUtil.ossUpload(request, file); @@ -89,21 +107,37 @@ private ITImgMidService itImgMidService; - @RequestMapping("/image1") + @RequestMapping("/image") @ResponseBody public String image1(@RequestPart("file") MultipartFile picture, HttpServletRequest request) { try { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile file = (MultipartFile) picture; - String fileName = UUID.randomUUID().toString() + "." + ToolUtil.getFileSuffix(file.getOriginalFilename()); - try { - String fileSavePath = gunsProperties.getFileUploadPath() + "img\\"; - file.transferTo(new File(fileSavePath + fileName)); - } catch (Exception e) { - e.printStackTrace(); + String pictureName = UUID.randomUUID().toString() + "." + ToolUtil.getFileSuffix(file.getOriginalFilename()); + String bucketName = QianYunTongProperties.bucketName; + Bucket grjyTest = NCOSSUtil.getBucketInfo(bucketName); + if (null == grjyTest) { + //创建桶 + Boolean bucket = NCOSSUtil.createBucket(bucketName); + if (!bucket) { + throw new RuntimeException("创建存储桶失败"); + } + //设置桶策略 + String policyText = "{\"Version\":\"2025-06-23\",\"Statement\":[{\"Sid\":\"Stmt20250623\",\"Action\":[\"GetObject\"],\"Effect\":\"Allow\",\"Resource\":\"" + bucketName + "/*\",\"Principal\":\"*\"}]}"; + Boolean bucketPolicy = NCOSSUtil.setBucketPolicy(bucketName, policyText); + if (!bucketPolicy) { + throw new RuntimeException("设置桶策略失败"); + } } - String pictureName = gunsProperties.getPictureServerAddress() + "img/" + fileName; + //上传对象 + String key = "imgs/management/" + pictureName; + String object = NCOSSUtil.putObject(bucketName, key, file.getInputStream()); + if (null == object) { + throw new RuntimeException("上传图片失败"); + } + + pictureName = "https://traffic.qytzt.cn/v1/AUTH_" + QianYunTongProperties.account + "/" + bucketName + "/" + key; // String pictureName = OssUploadUtil.ossUpload(request, file); System.out.println("mediaResp"); @@ -126,10 +160,29 @@ String suffix = picture.getOriginalFilename().substring(picture.getOriginalFilename().lastIndexOf(".")); String pictureName = UUID.randomUUID().toString() + suffix; try { - // 文件目录路径 - String fileSavePath = gunsProperties.getFileUploadPath(); - File file = new File(fileSavePath + pictureName); - picture.transferTo(file); + String bucketName = QianYunTongProperties.bucketName; + Bucket grjyTest = NCOSSUtil.getBucketInfo(bucketName); + if (null == grjyTest) { + //创建桶 + Boolean bucket = NCOSSUtil.createBucket(bucketName); + if (!bucket) { + throw new RuntimeException("创建存储桶失败"); + } + //设置桶策略 + String policyText = "{\"Version\":\"2025-06-23\",\"Statement\":[{\"Sid\":\"Stmt20250623\",\"Action\":[\"GetObject\"],\"Effect\":\"Allow\",\"Resource\":\"" + bucketName + "/*\",\"Principal\":\"*\"}]}"; + Boolean bucketPolicy = NCOSSUtil.setBucketPolicy(bucketName, policyText); + if (!bucketPolicy) { + throw new RuntimeException("设置桶策略失败"); + } + } + //上传对象 + String key = "file/management/" + pictureName; + String object = NCOSSUtil.putObject(bucketName, key, picture.getInputStream()); + if (null == object) { + throw new RuntimeException("上传图片失败"); + } + + pictureName = "https://traffic.qytzt.cn/v1/AUTH_" + QianYunTongProperties.account + "/" + bucketName + "/" + key; } catch (Exception e) { log.error(e.getMessage(), e); throw new GunsException(BizExceptionEnum.UPLOAD_ERROR); @@ -145,14 +198,30 @@ String callback = request.getParameter("callback"); try { // 上传文件目录 - String fileName = UUID.randomUUID().toString() + "." + ToolUtil.getFileSuffix(picture.getOriginalFilename()); - try { - String fileSavePath = gunsProperties.getFileUploadPath() + "img\\"; - picture.transferTo(new File(fileSavePath + fileName)); - } catch (Exception e) { - e.printStackTrace(); + String pictureName = UUID.randomUUID().toString() + "." + ToolUtil.getFileSuffix(picture.getOriginalFilename()); + String bucketName = QianYunTongProperties.bucketName; + Bucket grjyTest = NCOSSUtil.getBucketInfo(bucketName); + if (null == grjyTest) { + //创建桶 + Boolean bucket = NCOSSUtil.createBucket(bucketName); + if (!bucket) { + throw new RuntimeException("创建存储桶失败"); + } + //设置桶策略 + String policyText = "{\"Version\":\"2025-06-23\",\"Statement\":[{\"Sid\":\"Stmt20250623\",\"Action\":[\"GetObject\"],\"Effect\":\"Allow\",\"Resource\":\"" + bucketName + "/*\",\"Principal\":\"*\"}]}"; + Boolean bucketPolicy = NCOSSUtil.setBucketPolicy(bucketName, policyText); + if (!bucketPolicy) { + throw new RuntimeException("设置桶策略失败"); + } } - String pictureName = gunsProperties.getPictureServerAddress() + "img/" + fileName; + //上传对象 + String key = "imgs/management/" + pictureName; + String object = NCOSSUtil.putObject(bucketName, key, picture.getInputStream()); + if (null == object) { + throw new RuntimeException("上传图片失败"); + } + + pictureName = "https://traffic.qytzt.cn/v1/AUTH_" + QianYunTongProperties.account + "/" + bucketName + "/" + key; // pictureName = OssUploadUtil.ossUpload(super.getHttpServletRequest(), picture); String result = "{'original': '" + picture.getOriginalFilename() + "', 'state': 'SUCCESS', 'url': '" + pictureName + "'}"; -- Gitblit v1.7.1