From 0344dc66783353faff118a0ea91f7d2aa07bbd4a Mon Sep 17 00:00:00 2001
From: chenjiahe <763432473@qq.com>
Date: 星期六, 02 四月 2022 18:47:44 +0800
Subject: [PATCH] 优化加密初始化方法

---
 src/main/java/com/hx/util/COSUtil.java |   51 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/src/main/java/com/hx/util/COSUtil.java b/src/main/java/com/hx/util/COSUtil.java
index 714c7e8..8c6a5ca 100644
--- a/src/main/java/com/hx/util/COSUtil.java
+++ b/src/main/java/com/hx/util/COSUtil.java
@@ -6,6 +6,7 @@
 import com.qcloud.cos.ClientConfig;
 import com.qcloud.cos.auth.BasicCOSCredentials;
 import com.qcloud.cos.auth.COSCredentials;
+import com.qcloud.cos.model.ObjectMetadata;
 import com.qcloud.cos.model.PutObjectRequest;
 import com.qcloud.cos.model.PutObjectResult;
 import com.qcloud.cos.region.Region;
@@ -33,7 +34,7 @@
 	 * @return
 	 * @throws IOException
 	 */
-	public static String uploadImg(String key,File localFile,String secretId, String secretKey,String regionName,String bucketName) throws IOException {
+	public static String uploadImg(String key,MultipartFile localFile,String secretId, String secretKey,String regionName,String bucketName) throws IOException {
 
 		// 1 鍒濆鍖栫敤鎴疯韩浠戒俊鎭紙secretId, secretKey锛夈��
 		COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
@@ -45,12 +46,58 @@
 		COSClient cosClient = new COSClient(cred, clientConfig);
 
 		//寮�濮嬩笂浼�
+		ObjectMetadata objectMetadata = new ObjectMetadata();
+		// 璁剧疆杈撳叆娴侀暱搴︿负500
+		objectMetadata.setContentLength(localFile.getSize());
+		// 璁剧疆 Content type, 榛樿鏄� application/octet-stream,瀵逛簬鏈湴鏂囦欢涓婁紶锛岄粯璁ゆ牴鎹湰鍦版枃浠剁殑鍚庣紑杩涜鏄犲皠
+		// 锛屼緥濡� jpg 鏂囦欢鏄犲皠 涓篿mage/jpeg瀵逛簬娴佸紡涓婁紶 榛樿鏄� application/octet-stream
+		//objectMetadata.setContentType("application/pdf");
+		PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key,localFile.getInputStream(),objectMetadata);
+		PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
+
+		//鎷兼帴璺緞
+		StringBuilder imgUrl = new StringBuilder();
+		imgUrl.append("https://"+bucketName+".cos."+regionName+".myqcloud.com");
+		if(key.startsWith("/")){
+			imgUrl.append(key);
+		}else{
+			imgUrl.append("/"+key);
+		}
+		return imgUrl.toString();
+
+	}
+
+
+	/** 鍚庣璋冪敤涓婁紶鍥剧墖
+	 * @param key 涓婁紶璺緞锛堝寘鎷浘鐗囧悕绉板拰鍜屽悗缂�锛夛紝鎸囧畾瑕佷笂浼犲埌 COS 涓婂璞¢敭
+	 * @param localFile
+	 * @param secretId 鐢ㄦ埛id
+	 * @param secretKey 鐢ㄦ埛绉橀挜
+	 * @param regionName 瀛樺湪鍩燂紝鍙傝�冭吘璁簯
+	 * @param bucketName 鎸囧畾瑕佷笂浼犲埌鐨勫瓨鍌ㄦ《
+	 * @return
+	 * @throws IOException
+	 */
+	public static String uploadImg(String key,File localFile,String secretId, String secretKey,String regionName,String bucketName) throws IOException {
+
+		// 1 鍒濆鍖栫敤鎴疯韩浠戒俊鎭紙secretId, secretKey锛夈��
+		COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
+		// 2 璁剧疆 bucket 鐨勫尯鍩�, COS 鍦板煙鐨勭畝绉拌鍙傜収 https://cloud.tencent.com/document/product/436/6224
+		// clientConfig 涓寘鍚簡璁剧疆 region, https(榛樿 http), 瓒呮椂, 浠g悊绛� set 鏂规硶, 浣跨敤鍙弬瑙佹簮鐮佹垨鑰呭父瑙侀棶棰� Java SDK 閮ㄥ垎銆�
+		Region region = new Region(regionName);
+		ClientConfig clientConfig = new ClientConfig(region);
+		// 3 鐢熸垚 cos 瀹㈡埛绔��
+		COSClient cosClient = new COSClient(cred, clientConfig);
+
+		// 璁剧疆 Content type, 榛樿鏄� application/octet-stream,瀵逛簬鏈湴鏂囦欢涓婁紶锛岄粯璁ゆ牴鎹湰鍦版枃浠剁殑鍚庣紑杩涜鏄犲皠
+		// 锛屼緥濡� jpg 鏂囦欢鏄犲皠 涓篿mage/jpeg瀵逛簬娴佸紡涓婁紶 榛樿鏄� application/octet-stream
+		//objectMetadata.setContentType("application/pdf");
 		PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
 		PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
 
 		//鎷兼帴璺緞
 		StringBuilder imgUrl = new StringBuilder();
-		imgUrl.append("https://"+bucketName+"-"+secretId+".cos."+regionName+".myqcloud.com");
+		imgUrl.append("https://"+bucketName+".cos."+regionName+".myqcloud.com");
 		if(key.startsWith("/")){
 			imgUrl.append(key);
 		}else{

--
Gitblit v1.8.0