| | |
| | | package com.ruoyi.file.utils; |
| | | |
| | | import cn.hutool.core.img.ImgUtil; |
| | | import cn.hutool.extra.qrcode.QrCodeUtil; |
| | | import com.aliyun.oss.ClientException; |
| | | import com.aliyun.oss.OSS; |
| | |
| | | import com.ruoyi.common.core.utils.uuid.IdUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.awt.*; |
| | | import java.io.*; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.InputStream; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * @author jqs34 |
| | |
| | | */ |
| | | public class OBSUploadUtils { |
| | | |
| | | |
| | | |
| | | public static void main(String[] args) throws Exception { |
| | | String activityId = "123"; |
| | | String backImageUrl = "2023/8/3/6742e22f748349429e454c9c6ba168ee.jpg"; |
| | | String fileUrl = null; |
| | | File qrCodeFile = new File("/home/image/qrcode.png"); |
| | | // 二维码内容 |
| | | String text = "https://wxapp.hhhrt.cn/mini/activity?"+activityId; |
| | | String text = "https://wxapp.hhhrt.cn/mini/coupon"; |
| | | // 生成二维码 |
| | | QrCodeUtil.generate(text, 100, 100, qrCodeFile); |
| | | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| | | InputStream codeStream = new FileInputStream(qrCodeFile); |
| | | InputStream backStream = OBSUploadUtils.getOSSInputStream(backImageUrl); |
| | | // 将图片合成在一起 |
| | | ImgUtil.pressImage( |
| | | backStream, // 主图片 |
| | | out, // 输出图片 |
| | | ImgUtil.read(codeStream).getScaledInstance(200, 200, Image.SCALE_DEFAULT), //水印图片 |
| | | 0, //x坐标修正值。 默认在中间,偏移量相对于中间偏移 |
| | | 350, //y坐标修正值。 默认在中间,偏移量相对于中间偏移 |
| | | 1.0f |
| | | ); |
| | | InputStream inputStream = new ByteArrayInputStream(out.toByteArray()); |
| | | fileUrl = OBSUploadUtils.uploadInputStream(inputStream,activityId); |
| | | fileUrl = OBSUploadUtils.uploadInputStream(codeStream,"couponCode"); |
| | | System.out.println(fileUrl); |
| | | } |
| | | |
| | |
| | | OSS ossClient = createOss(); |
| | | PutObjectResult result = null; |
| | | try { |
| | | |
| | | String fileName = FileUploadUtils.extractFilename(file); |
| | | System.out.println(fileName + "开始上传"); |
| | | System.out.println(fileName + "开始上传" + new Date()); |
| | | String prefix = fileName.substring(fileName.lastIndexOf(".")); |
| | | String objectName = filePath + uuid + prefix; |
| | | InputStream inputStream = file.getInputStream(); |
| | |
| | | // 创建PutObject请求。 |
| | | result = ossClient.putObject(putObjectRequest); |
| | | // 如果上传成功,则返回200。 |
| | | System.out.println(fileName + "上传返回" + result.getResponse().getStatusCode()); |
| | | System.out.println(fileName + "上传返回"+ new Date() + result.getResponse().getStatusCode()); |
| | | inputStream.close(); |
| | | return result.getResponse().getUri(); |
| | | } catch (OSSException oe) { |
| | | System.out.println("Caught an OSSException, which means your request made it to OSS, " |
| | |
| | | PutObjectResult result = null; |
| | | try { |
| | | |
| | | System.out.println(fileName + "开始上传"); |
| | | System.out.println(fileName + "开始上传"+ new Date()); |
| | | String objectName = filePath + fileName + ".jpg"; |
| | | // 创建PutObjectRequest对象。 |
| | | PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream); |
| | |
| | | // 创建PutObject请求。 |
| | | result = ossClient.putObject(putObjectRequest); |
| | | // 如果上传成功,则返回200。 |
| | | System.out.println(fileName + "上传返回" + result.getResponse().getStatusCode()); |
| | | System.out.println(fileName + "上传返回"+ new Date() + result.getResponse().getStatusCode()); |
| | | return result.getResponse().getUri(); |
| | | } catch (OSSException oe) { |
| | | System.out.println("Caught an OSSException, which means your request made it to OSS, " |