| | |
| | | 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.base.tips.SuccessTip; |
| | | import com.stylefeng.guns.core.common.exception.BizExceptionEnum; |
| | | import com.stylefeng.guns.core.exception.GunsException; |
| | | import com.stylefeng.guns.core.util.ObsUploadUtil; |
| | | import com.stylefeng.guns.modular.cloudPayment.example.MediaExample; |
| | | import com.stylefeng.guns.modular.system.model.TImgMid; |
| | | import com.stylefeng.guns.core.util.ToolUtil; |
| | | import com.stylefeng.guns.modular.system.service.ITImgMidService; |
| | | import com.stylefeng.guns.modular.system.util.OssUploadUtil; |
| | | import com.unionpay.upyzt.exception.UpyztException; |
| | | import com.unionpay.upyzt.resp.MediaResp; |
| | | import com.stylefeng.guns.modular.system.util.ResultUtil; |
| | | 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.io.IOException; |
| | | 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) { |
| | | ossUpload = OssUploadUtil.ossUpload(request, file); |
| | | 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 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); |
| | | // ossUpload = ObsUploadUtil.obsUpload(super.getHttpServletRequest(), file); |
| | | m.put("imgUrl", ossUpload); |
| | | } |
| | |
| | | } |
| | | @Resource |
| | | private ITImgMidService itImgMidService; |
| | | |
| | | |
| | | |
| | | @RequestMapping("/image") |
| | | @ResponseBody |
| | | public Object image(@RequestPart("file") MultipartFile picture, HttpServletRequest request) { |
| | | public ResultUtil image1(@RequestPart("file") MultipartFile file, HttpServletRequest request) { |
| | | try { |
| | | MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; |
| | | MultipartFile file = (MultipartFile) picture; |
| | | String pictureName = OssUploadUtil.ossUpload(request, file); |
| | | System.out.println("mediaResp"); |
| | | MediaResp mediaResp = MediaExample.uploadImage(picture); |
| | | System.out.println("------"+mediaResp); |
| | | TImgMid tImgMid = new TImgMid(); |
| | | tImgMid.setMid(mediaResp.getMediaId()); |
| | | tImgMid.setUrl(pictureName); |
| | | itImgMidService.insert(tImgMid); |
| | | // String pictureName = ObsUploadUtil.obsUpload(super.getHttpServletRequest(), picture); |
| | | return pictureName; |
| | | } catch (UpyztException e){ |
| | | e.printStackTrace(); |
| | | return ERROR; |
| | | }catch (Exception e1) { |
| | | e1.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | @RequestMapping("/image1") |
| | | @ResponseBody |
| | | public String image1(@RequestPart("file") MultipartFile picture, HttpServletRequest request) { |
| | | try { |
| | | MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; |
| | | MultipartFile file = (MultipartFile) picture; |
| | | String pictureName = OssUploadUtil.ossUpload(request, file); |
| | | 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) { |
| | | return ResultUtil.error("创建存储桶失败"); |
| | | } |
| | | //设置桶策略 |
| | | String policyText = "{\"Version\":\"2025-06-23\",\"Statement\":[{\"Sid\":\"Stmt20250623\",\"Action\":[\"GetObject\"],\"Effect\":\"Allow\",\"Resource\":\"" + bucketName + "/*\",\"Principal\":\"*\"}]}"; |
| | | Boolean bucketPolicy = NCOSSUtil.setBucketPolicy(bucketName, policyText); |
| | | if (!bucketPolicy) { |
| | | return ResultUtil.error("设置桶策略失败"); |
| | | } |
| | | } |
| | | //上传对象 |
| | | String key = "imgs/management/" + pictureName; |
| | | String object = NCOSSUtil.putObject(bucketName, key, file.getInputStream()); |
| | | if (null == object) { |
| | | return ResultUtil.error("上传图片失败"); |
| | | } |
| | | |
| | | pictureName = "https://traffic.qytzt.cn/v1/AUTH_" + QianYunTongProperties.account + "/" + bucketName + "/" + key; |
| | | |
| | | // String pictureName = OssUploadUtil.ossUpload(request, file); |
| | | System.out.println("mediaResp"); |
| | | // MediaResp mediaResp = MediaExample.uploadImage(picture); |
| | | // System.out.println("------"+mediaResp); |
| | | // String pictureName = ObsUploadUtil.obsUpload(super.getHttpServletRequest(), picture); |
| | | return pictureName; |
| | | return ResultUtil.success(pictureName); |
| | | } catch (Exception e1) { |
| | | e1.printStackTrace(); |
| | | return null; |
| | |
| | | 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); |
| | |
| | | @RequestMapping("/imageUp") |
| | | public String imageUp(@RequestPart("upfile") MultipartFile picture, HttpServletRequest request) { |
| | | String callback = request.getParameter("callback"); |
| | | String pictureName = UUID.randomUUID().toString() + ".jpg"; |
| | | try { |
| | | // 上传文件目录 |
| | | //String fileSavePath = gunsProperties.getFileUploadPath(); |
| | | //picture.transferTo(new File(fileSavePath + pictureName)); |
| | | // 文件全路径 |
| | | //pictureName = gunsProperties.getPictureServerAddress() + pictureName; |
| | | // pictureName = ObsUploadUtil.obsUpload(super.getHttpServletRequest(), picture); |
| | | pictureName = OssUploadUtil.ossUpload(super.getHttpServletRequest(), picture); |
| | | |
| | | 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 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 + "'}"; |
| | | if (callback == null) { |
| | | return result; |