| | |
| | | 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; |
| | |
| | | |
| | | 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; |
| | |
| | | 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); |
| | |
| | | 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"); |
| | |
| | | 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); |
| | |
| | | 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 + "'}"; |