package com.ndsoft.single.web.message; import com.ndsoft.single.common.base.BaseController; import com.ndsoft.single.common.utils.DateUtil; 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.model.*; import com.qcloud.cos.region.Region; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.io.InputStream; import java.util.LinkedList; import java.util.List; import java.util.Random; /** * 大文件上傳 */ @Controller public class BlockUploadController extends BaseController { private static final String FILE_PATH_PREFIX = "\\"; private static final String FILE_PREFIX = "/"; @RequestMapping("/blockUpload") @ResponseBody public void blockUpload(@RequestParam("file") MultipartFile file) throws Exception { // 文件的完整名字 String fileName = file.getOriginalFilename(); if (fileName.contains(FILE_PATH_PREFIX)) { fileName = fileName.substring(fileName.lastIndexOf(FILE_PATH_PREFIX) + 1); } String type = fileName.substring(fileName.lastIndexOf(".") + 1); Random rand = new Random(); int random = rand.nextInt(); // 上傳后的名稱 String key = FILE_PREFIX + DateUtil.getDateStr("yyMMddHHmmss") + (random > 0 ? random : (-1) * random) + "." + type; // 獲取分塊上傳的 uploadId String uploadId = InitMultipartUploadDemo(key); // 獲取上傳的所有分塊信息 List<PartETag> partETags = UploadPartDemo(uploadId, key, file); // 完成分片上傳 completePartDemo(uploadId, partETags, key); } /** * 獲取分塊上傳的 uploadId * * @param key * @return */ public String InitMultipartUploadDemo(String key) { // 1 初始化用戶身份信息(secretId, secretKey) // ndFileProperties.getOssTxcosAccessKey() 指 accessKey; ndFileProperties.getOssTxcosAccessSecret() 指 SecretKey COSCredentials cred = new BasicCOSCredentials(ndFileProperties.getOssTxcosAccessKey(), ndFileProperties.getOssTxcosAccessSecret()); // 2 設置bucket的區域, COS地域的簡稱請參照 https://www.qcloud.com/document/product/436/6224 // ndFileProperties.getOssTxcosRegion() - 存儲桶所在的地域 ClientConfig clientConfig = new ClientConfig(new Region(ndFileProperties.getOssTxcosRegion())); // 3 生成cos客戶端 COSClient cosclient = new COSClient(cred, clientConfig); // bucket名需包含appid ndFileProperties.getOssTxcosDefaultBucketName()-此處填寫的存儲桶名稱必須為此格式 String bucketName = ndFileProperties.getOssTxcosDefaultBucketName(); // 初始化分塊上傳任務 InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key); // 設置存儲類型, 默認是標准(Standard), 低頻(Standard_IA), 歸檔(Archive) request.setStorageClass(StorageClass.Standard); try { InitiateMultipartUploadResult initResult = cosclient.initiateMultipartUpload(request); // 獲取uploadid String uploadId = initResult.getUploadId(); return uploadId; } catch (CosServiceException e) { throw e; } catch (CosClientException e) { throw e; } finally { cosclient.shutdown(); } } /** * 獲取上傳的所有分塊信息 * * @param uploadId * @param key * @param file * @return * @throws IOException */ public List<PartETag> UploadPartDemo(String uploadId, String key, MultipartFile file) throws IOException { // 1 初始化用戶身份信息(secretId, secretKey) // ndFileProperties.getOssTxcosAccessKey() 指 accessKey; ndFileProperties.getOssTxcosAccessSecret() 指 SecretKey COSCredentials cred = new BasicCOSCredentials(ndFileProperties.getOssTxcosAccessKey(), ndFileProperties.getOssTxcosAccessSecret()); // 2 設置bucket的區域, COS地域的簡稱請參照 https://www.qcloud.com/document/product/436/6224 // ndFileProperties.getOssTxcosRegion() 指 存儲桶所在的地域 如:ap-shanghai ClientConfig clientConfig = new ClientConfig(new Region(ndFileProperties.getOssTxcosRegion())); // 3 生成cos客戶端 COSClient cosclient = new COSClient(cred, clientConfig); // bucket名需包含appid String bucketName = ndFileProperties.getOssTxcosDefaultBucketName(); // 是否進行流量控制 boolean userTrafficLimit = false; // 所有分塊 List<PartETag> partETags = new LinkedList<>(); // 每片分塊的大小 int partSize = 2 * 1024 * 1024 * 100; // 上傳文件的大小 long fileSize = file.getSize(); // 分塊片數 int partCount = (int) (fileSize / partSize); if (fileSize % partSize != 0) { partCount++; } // 生成要上傳的數據, 這里初始化一個10M的數據 for (int i = 0; i < partCount; i++) { // 起始位置 long startPos = i * partSize; // 分片大小 long curPartSize = (i + 1 == partCount) ? (fileSize - startPos) : partSize; // 大文件 InputStream instream = file.getInputStream(); // 跳過已經上傳的分片。 instream.skip(startPos); // 用於上傳分塊請求 UploadPartRequest uploadPartRequest = new UploadPartRequest(); // 存儲桶命名 uploadPartRequest.setBucketName(bucketName); // 指定分塊上傳到 COS 上的路徑 uploadPartRequest.setKey(key); // 標識指定分塊上傳的 uploadId uploadPartRequest.setUploadId(uploadId); // 設置分塊的數據來源輸入流 uploadPartRequest.setInputStream(instream); // 設置分塊的長度 uploadPartRequest.setPartSize(curPartSize); // 上傳分片編號 uploadPartRequest.setPartNumber(i + 1); // 是否進行流量控制 if (userTrafficLimit) { // 用於對上傳對象進行流量控制,單位:bit/s,默認不進行流量控制 uploadPartRequest.setTrafficLimit(8 * 1024 * 1024); } try { // 上傳分片數據的輸入流 UploadPartResult uploadPartResult = cosclient.uploadPart(uploadPartRequest); // 獲取上傳分塊信息 PartETag partETag = uploadPartResult.getPartETag(); // 把上傳返回的分塊信息放入到分片集合中 partETags.add(partETag); // 獲取復制生成對象的CRC64 String crc64 = uploadPartResult.getCrc64Ecma(); } catch (CosServiceException e) { throw e; } catch (CosClientException e) { throw e; } } cosclient.shutdown(); return partETags; } /** * 完成分片上傳 * * @param uploadId * @param partETags * @param key */ public void completePartDemo(String uploadId, List<PartETag> partETags, String key) { // 1 初始化用戶身份信息(secretId, secretKey) accessKey-騰訊雲cos accessKey, secretKey-騰訊雲cos 訪問Secret // ndFileProperties.getOssTxcosAccessKey() 指 accessKey; ndFileProperties.getOssTxcosAccessSecret() 指 SecretKey COSCredentials cred = new BasicCOSCredentials(ndFileProperties.getOssTxcosAccessKey(), ndFileProperties.getOssTxcosAccessSecret()); // 2 設置bucket的區域, COS地域的簡稱請參照 https://www.qcloud.com/document/product/436/6224 ClientConfig clientConfig = new ClientConfig(new Region(ndFileProperties.getOssTxcosRegion())); // 3 生成cos客戶端 COSClient cosclient = new COSClient(cred, clientConfig); // bucket名需包含appid String bucketName = ndFileProperties.getOssTxcosDefaultBucketName(); // 分片上傳結束后,調用complete完成分片上傳 CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags); try { CompleteMultipartUploadResult completeResult = cosclient.completeMultipartUpload(completeMultipartUploadRequest); String etag = completeResult.getETag(); String crc64 = completeResult.getCrc64Ecma(); } catch (CosServiceException e) { throw e; } catch (CosClientException e) { throw e; } cosclient.shutdown(); } }
Maven
<dependency> <groupId>com.qcloud</groupId> <artifactId>cos_api</artifactId> <version>5.6.22</version> </dependency>
Postman請求