From b3243b62611ec22a020e988e94b535e6e758a914 Mon Sep 17 00:00:00 2001
From: chenjiahe <763432473@qq.com>
Date: 星期一, 04 九月 2023 17:25:34 +0800
Subject: [PATCH] 新版excel

---
 src/main/java/com/hx/util/COSUtil.java |  135 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 130 insertions(+), 5 deletions(-)

diff --git a/src/main/java/com/hx/util/COSUtil.java b/src/main/java/com/hx/util/COSUtil.java
index 62cc1fe..92efaba 100644
--- a/src/main/java/com/hx/util/COSUtil.java
+++ b/src/main/java/com/hx/util/COSUtil.java
@@ -1,22 +1,26 @@
 package com.hx.util;
 
-import com.aliyun.oss.OSSClient;
-import com.aliyun.oss.model.OSSObject;
+import com.hx.exception.TipsException;
 import com.qcloud.cos.COSClient;
 import com.qcloud.cos.ClientConfig;
 import com.qcloud.cos.auth.BasicCOSCredentials;
 import com.qcloud.cos.auth.COSCredentials;
+import com.qcloud.cos.exception.CosClientException;
+import com.qcloud.cos.exception.CosServiceException;
+import com.qcloud.cos.http.HttpProtocol;
+import com.qcloud.cos.model.GetObjectRequest;
 import com.qcloud.cos.model.ObjectMetadata;
 import com.qcloud.cos.model.PutObjectRequest;
 import com.qcloud.cos.model.PutObjectResult;
 import com.qcloud.cos.region.Region;
+import com.qcloud.cos.transfer.Download;
+import com.qcloud.cos.transfer.TransferManager;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
-import java.util.Date;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 /**鑵捐浜� COS
  * @author ChenJiaHe
@@ -109,4 +113,125 @@
 
 	}
 
+	/**涓嬭浇鏂囦欢
+	 * @param key 涓婁紶璺緞锛堝寘鎷浘鐗囧悕绉板拰鍜屽悗缂�锛夛紝鎸囧畾瑕佷笂浼犲埌 COS 涓婂璞¢敭
+	 * @param secretId 鐢ㄦ埛id
+	 * @param secretKey 鐢ㄦ埛绉橀挜
+	 * @param regionName 瀛樺湪鍩燂紝鍙傝�冭吘璁簯
+	 * @param bucketName 鎸囧畾瑕佷笂浼犲埌鐨勫瓨鍌ㄦ《
+	 */
+	public static File download(String key,String secretId, String secretKey,String regionName,String bucketName){
+
+		// 浣跨敤楂樼骇鎺ュ彛蹇呴』鍏堜繚璇佹湰杩涚▼瀛樺湪涓�涓� TransferManager 瀹炰緥锛屽鏋滄病鏈夊垯鍒涘缓
+		// 璇︾粏浠g爜鍙傝鏈〉锛氶珮绾ф帴鍙� -> 鍒涘缓 TransferManager
+		TransferManager transferManager = createTransferManager( secretId, secretKey,regionName);
+		// 鏈湴鏂囦欢璺緞
+		File downloadFile = null;
+		try {
+			GetObjectRequest getObjectRequest;
+			//鎴彇鏂囦欢鍚嶇О
+			String[] datas = key.split(regionName+".myqcloud.com/");
+			if(datas.length ==1){
+				getObjectRequest = new GetObjectRequest(bucketName, datas[0]);
+			}else if(datas.length ==2){
+				getObjectRequest = new GetObjectRequest(bucketName, datas[1]);
+			}else{
+				throw new TipsException("鏂囦欢璺緞閿欒銆恔ey銆�");
+			}
+
+			////鐢熸垚涓存椂鏂囦欢
+			//鑾峰彇鍚庣紑鍚嶇О
+			String suffix = key.substring(key.lastIndexOf("."));
+			downloadFile = File.createTempFile("temp", suffix);
+
+			// 杩斿洖涓�涓紓姝ョ粨鏋� Donload, 鍙悓姝ョ殑璋冪敤 waitForCompletion 绛夊緟涓嬭浇缁撴潫, 鎴愬姛杩斿洖 void, 澶辫触鎶涘嚭寮傚父
+			Download download = transferManager.download(getObjectRequest, downloadFile);
+			download.waitForCompletion();
+		} catch (CosServiceException e) {
+			e.printStackTrace();
+		} catch (CosClientException e) {
+			e.printStackTrace();
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+		}catch (IOException e){
+			e.printStackTrace();
+		}finally {
+			// 纭畾鏈繘绋嬩笉鍐嶄娇鐢� transferManager 瀹炰緥涔嬪悗锛屽叧闂箣
+			// 璇︾粏浠g爜鍙傝鏈〉锛氶珮绾ф帴鍙� -> 鍏抽棴 TransferManager
+			shutdownTransferManager(transferManager);
+		}
+
+		return downloadFile;
+	}
+
+	/**
+	 * 鍒涘缓 TransferManager 瀹炰緥锛岃繖涓疄渚嬬敤鏉ュ悗缁皟鐢ㄩ珮绾ф帴鍙�
+	 * @param secretId
+	 * @param secretKey
+	 * @return
+	 */
+	public static TransferManager createTransferManager(String secretId, String secretKey,String regionName) {
+		// 鍒涘缓涓�涓� COSClient 瀹炰緥锛岃繖鏄闂� COS 鏈嶅姟鐨勫熀纭�瀹炰緥銆�
+		// 璇︾粏浠g爜鍙傝鏈〉: 绠�鍗曟搷浣� -> 鍒涘缓 COSClient
+		COSClient cosClient = createCOSClient( secretId, secretKey,regionName);
+
+		// 鑷畾涔夌嚎绋嬫睜澶у皬锛屽缓璁湪瀹㈡埛绔笌 COS 缃戠粶鍏呰冻锛堜緥濡備娇鐢ㄨ吘璁簯鐨� CVM锛屽悓鍦板煙涓婁紶 COS锛夌殑鎯呭喌涓嬶紝璁剧疆鎴�16鎴�32鍗冲彲锛屽彲杈冨厖鍒嗙殑鍒╃敤缃戠粶璧勬簮
+		// 瀵逛簬浣跨敤鍏綉浼犺緭涓旂綉缁滃甫瀹借川閲忎笉楂樼殑鎯呭喌锛屽缓璁噺灏忚鍊硷紝閬垮厤鍥犵綉閫熻繃鎱紝閫犳垚璇锋眰瓒呮椂銆�
+		ExecutorService threadPool = Executors.newFixedThreadPool(32);
+
+		// 浼犲叆涓�涓� threadpool, 鑻ヤ笉浼犲叆绾跨▼姹狅紝榛樿 TransferManager 涓細鐢熸垚涓�涓崟绾跨▼鐨勭嚎绋嬫睜銆�
+		TransferManager transferManager = new TransferManager(cosClient, threadPool);
+
+		return transferManager;
+	}
+
+	/**
+	 * 鍏抽棴绠$悊
+	 * @param transferManager
+	 */
+	public static void shutdownTransferManager(TransferManager transferManager) {
+		// 鎸囧畾鍙傛暟涓� true, 鍒欏悓鏃朵細鍏抽棴 transferManager 鍐呴儴鐨� COSClient 瀹炰緥銆�
+		// 鎸囧畾鍙傛暟涓� false, 鍒欎笉浼氬叧闂� transferManager 鍐呴儴鐨� COSClient 瀹炰緥銆�
+		transferManager.shutdownNow(true);
+	}
+
+	/**
+	 *  鍒涘缓 COSClient 瀹炰緥锛岃繖涓疄渚嬬敤鏉ュ悗缁皟鐢ㄨ姹�
+	 * @param secretId
+	 * @param secretKey
+	 * @return
+	 */
+	public static COSClient createCOSClient(String secretId, String secretKey,String regionName) {
+		// 璁剧疆鐢ㄦ埛韬唤淇℃伅銆�
+		// SECRETID 鍜� SECRETKEY 璇风櫥褰曡闂鐞嗘帶鍒跺彴 https://console.cloud.tencent.com/cam/capi 杩涜鏌ョ湅鍜岀鐞�
+		COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
+
+		// ClientConfig 涓寘鍚簡鍚庣画璇锋眰 COS 鐨勫鎴风璁剧疆锛�
+		ClientConfig clientConfig = new ClientConfig();
+
+		// 璁剧疆 bucket 鐨勫湴鍩�
+		// COS_REGION 璇峰弬鐓� https://cloud.tencent.com/document/product/436/6224
+		clientConfig.setRegion(new Region(regionName));
+
+		// 璁剧疆璇锋眰鍗忚, http 鎴栬�� https
+		// 5.6.53 鍙婃洿浣庣殑鐗堟湰锛屽缓璁缃娇鐢� https 鍗忚
+		// 5.6.54 鍙婃洿楂樼増鏈紝榛樿浣跨敤浜� https
+		clientConfig.setHttpProtocol(HttpProtocol.https);
+
+		// 浠ヤ笅鐨勮缃紝鏄彲閫夌殑锛�
+
+		// 璁剧疆 socket 璇诲彇瓒呮椂锛岄粯璁� 30s
+		clientConfig.setSocketTimeout(300*1000);
+		// 璁剧疆寤虹珛杩炴帴瓒呮椂锛岄粯璁� 30s
+		clientConfig.setConnectionTimeout(30*1000);
+
+		// 濡傛灉闇�瑕佺殑璇濓紝璁剧疆 http 浠g悊锛宨p 浠ュ強 port
+		//clientConfig.setHttpProxyIp("httpProxyIp");
+		//clientConfig.setHttpProxyPort(80);
+
+		// 鐢熸垚 cos 瀹㈡埛绔��
+		return new COSClient(cred, clientConfig);
+	}
+
+
 }

--
Gitblit v1.8.0