|
@@ -22,11 +22,15 @@ import com.qcloud.cos.exception.CosServiceException;
|
|
|
import com.qcloud.cos.exception.MultiObjectDeleteException;
|
|
|
import com.qcloud.cos.http.HttpMethodName;
|
|
|
import com.qcloud.cos.model.*;
|
|
|
+import com.qcloud.cos.model.ciModel.common.ImageProcessRequest;
|
|
|
+import com.qcloud.cos.model.ciModel.persistence.CIUploadResult;
|
|
|
+import com.qcloud.cos.model.ciModel.persistence.PicOperations;
|
|
|
import com.qcloud.cos.region.Region;
|
|
|
import com.qcloud.cos.transfer.Transfer;
|
|
|
import com.qcloud.cos.transfer.TransferManager;
|
|
|
import com.qcloud.cos.transfer.TransferProgress;
|
|
|
import com.qcloud.cos.transfer.Upload;
|
|
|
+import com.qcloud.cos.utils.Jackson;
|
|
|
import org.joda.time.DateTime;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@@ -36,10 +40,8 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
import java.io.*;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.net.URL;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.LinkedList;
|
|
|
-import java.util.List;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
// 图片处理概述
|
|
@@ -121,7 +123,7 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
|
* - 如果对象是图片,则同时产生缩略图
|
|
|
*/
|
|
|
@Override
|
|
|
- public SysFileResult putObject(MultipartFile multipartFile, String object_key) {
|
|
|
+ public SysFileResult putObject(MultipartFile multipartFile, String object_key, Integer is_watermark) {
|
|
|
|
|
|
if (multipartFile.isEmpty()) throw new CustException("file 上传文件不能为空");
|
|
|
|
|
@@ -172,8 +174,9 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
|
inputStream = multipartFile.getInputStream();
|
|
|
PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, object_key, inputStream, metadata);
|
|
|
|
|
|
- // 普通上传 (没有进度)
|
|
|
-// // cosClient.putObject(putObjectRequest);
|
|
|
+// // 普通上传 (没有进度)
|
|
|
+// PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
|
|
|
+
|
|
|
|
|
|
// 高级上传
|
|
|
TransferManager transferManager = TencentCosUtil.createTransferManager(cosClient);
|
|
@@ -181,11 +184,18 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
|
showTransferProgress(filename, upload, null); // 查询上传进度,直到上传结束
|
|
|
UploadResult uploadResult = upload.waitForUploadResult(); // 捕获可能出现的异常
|
|
|
|
|
|
+ System.out.println("[腾讯云][cos-uploadResult] = " + uploadResult.getDateStr());
|
|
|
+
|
|
|
+ // 打水印
|
|
|
+ if (is_watermark != null && is_watermark == 1) {
|
|
|
+ makeWater(object_key);
|
|
|
+ }
|
|
|
+
|
|
|
// 自定义返回结果实体
|
|
|
SysFileResult result = new SysFileResult();
|
|
|
result.setKey(object_key);
|
|
|
- result.setRequest_id(uploadResult.getRequestId());
|
|
|
- result.setE_tag(uploadResult.getETag());
|
|
|
+// result.setRequest_id(uploadResult.getRequestId());
|
|
|
+// result.setE_tag(uploadResult.getETag());
|
|
|
result.setDomain(ACCESSIBLE_DOMAIN);
|
|
|
result.setUrl(ACCESSIBLE_DOMAIN + "/" + object_key);
|
|
|
result.setTarget(1);
|
|
@@ -208,6 +218,66 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ // [腾讯云COS] 打水印
|
|
|
+ public void makeWater(String object_key) {
|
|
|
+ // 初始化 CosClient
|
|
|
+ COSClient cosClient = getClient();
|
|
|
+ InputStream inputStream = null;
|
|
|
+ try {
|
|
|
+
|
|
|
+ // 对原图发起云上处理
|
|
|
+ ImageProcessRequest request = new ImageProcessRequest(BUCKET_NAME, object_key);
|
|
|
+
|
|
|
+ PicOperations picOperations = new PicOperations();
|
|
|
+ picOperations.setIsPicInfo(1);
|
|
|
+ List<PicOperations.Rule> rule_list = new ArrayList<>();
|
|
|
+
|
|
|
+ PicOperations.Rule rule_item = new PicOperations.Rule();
|
|
|
+
|
|
|
+ // -- 水印 --------------------------------
|
|
|
+ // 文字水印:https://cloud.tencent.com/document/product/436/119425
|
|
|
+ // 上传时处理:https://cloud.tencent.com/document/product/460/18147#.E4.BA.91.E4.B8.8A.E6.95.B0.E6.8D.AE.E5.A4.84.E7.90.86
|
|
|
+ String text = "稻谷文化";
|
|
|
+ byte[] textBates = text.getBytes(StandardCharsets.UTF_8);
|
|
|
+ String encodedText = Base64.getUrlEncoder().encodeToString(textBates).replaceAll("=", "");
|
|
|
+ String rule = "watermark/2/text/" + encodedText +
|
|
|
+ "/fill/IzNEM0QzRA/fontsize/28/dissolve/50/batch/1/spacing/40/degree/35";
|
|
|
+ // - dissolve: 文字透明度,取值1 - 100 ,默认90
|
|
|
+ // - gravity: 九宫格位置 (gravity/northeast/)
|
|
|
+ // - dx: 水平(横轴)边距,单位为像素,缺省值为0
|
|
|
+ // - dy: 垂直(纵轴)边距,单位为像素,默认值为0
|
|
|
+ // - batch: 平铺水印功能,可将文字水印平铺至整张图片。值为1时,表示开启平铺水印功能
|
|
|
+ // - spacing: 平铺模式下的水平、垂直间距相对文字水印贴图的宽高百分比,范围为[0,100],默认10
|
|
|
+ // - degree: 文字水印的旋转角度设置,取值范围为0 - 360,默认0
|
|
|
+
|
|
|
+
|
|
|
+ rule_item.setBucket(BUCKET_NAME);
|
|
|
+ rule_item.setFileId("/" + object_key); // 以 '/' 开头是为绝对路径,否则会在当前目录下再创建一个对象路径
|
|
|
+ rule_item.setRule(rule);
|
|
|
+ // ----------------------------------
|
|
|
+
|
|
|
+ rule_list.add(rule_item);
|
|
|
+ picOperations.setRules(rule_list);
|
|
|
+
|
|
|
+ System.out.println("Jackson.toJsonString(picOperations) = " + Jackson.toJsonString(picOperations));
|
|
|
+
|
|
|
+ request.setPicOperations(picOperations);
|
|
|
+ CIUploadResult ciUploadResult = cosClient.processImage(request);
|
|
|
+ System.out.println("[腾讯云][cos-ciUploadResult] OriginalInfo= " + Jackson.toJsonString(ciUploadResult.getOriginalInfo()));
|
|
|
+ System.out.println("[腾讯云][cos-ciUploadResult] ProcessResults = " + Jackson.toJsonString(ciUploadResult.getProcessResults()));
|
|
|
+
|
|
|
+ } finally {
|
|
|
+ if (inputStream != null) {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ // 忽略关闭流时的异常
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (cosClient != null) cosClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// [腾讯云COS] 查询对象是否存在
|
|
|
@Override
|
|
|
public boolean doesObjectExist(String object_key) {
|