From 7b319c5d9e8f133fa63637eb2754b1b6a223599d Mon Sep 17 00:00:00 2001 From: ChenJiaHe <763432473@qq.com> Date: 星期四, 03 十二月 2020 17:58:16 +0800 Subject: [PATCH] 优化 --- src/main/java/com/hx/util/COSUtil.java | 63 +++++++++++++++++++++++++++++++ pom.xml | 7 +++ hx-common.iml | 13 +++--- 3 files changed, 76 insertions(+), 7 deletions(-) diff --git a/hx-common.iml b/hx-common.iml index 4676147..1348670 100644 --- a/hx-common.iml +++ b/hx-common.iml @@ -19,7 +19,13 @@ </content> <orderEntry type="inheritedJdk" /> <orderEntry type="sourceFolder" forTests="false" /> - <orderEntry type="library" name="Maven: org.apache.commons:commons-csv:1.6" level="project" /> + <orderEntry type="library" name="Maven: com.qcloud:cos_api:5.6.32" level="project" /> + <orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" /> + <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.11" level="project" /> + <orderEntry type="library" name="Maven: joda-time:joda-time:2.9.9" level="project" /> + <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.6" level="project" /> + <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.0" level="project" /> + <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.6" level="project" /> <orderEntry type="library" name="Maven: com.sun.mail:javax.mail:1.6.2" level="project" /> <orderEntry type="library" name="Maven: javax.activation:activation:1.1" level="project" /> <orderEntry type="library" name="Maven: org.apache.commons:commons-csv:1.6" level="project" /> @@ -30,7 +36,6 @@ <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.13" level="project" /> <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.5.12" level="project" /> <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" /> - <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.11" level="project" /> <orderEntry type="library" name="Maven: cglib:cglib:3.1" level="project" /> <orderEntry type="library" name="Maven: org.ow2.asm:asm:4.2" level="project" /> <orderEntry type="library" name="Maven: com.google.zxing:core:3.3.0" level="project" /> @@ -42,9 +47,6 @@ <orderEntry type="library" name="Maven: redis.clients:jedis:2.9.0" level="project" /> <orderEntry type="library" name="Maven: org.apache.commons:commons-pool2:2.5.0" level="project" /> <orderEntry type="library" name="Maven: com.auth0:java-jwt:3.10.3" level="project" /> - <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.6" level="project" /> - <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.0" level="project" /> - <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.6" level="project" /> <orderEntry type="library" name="Maven: io.jsonwebtoken:jjwt:0.9.1" level="project" /> <orderEntry type="library" name="Maven: javax.servlet.jsp.jstl:jstl-api:1.2" level="project" /> <orderEntry type="library" name="Maven: javax.servlet:servlet-api:2.5" level="project" /> @@ -88,7 +90,6 @@ <orderEntry type="library" name="Maven: org.mybatis:mybatis-spring:1.3.0" level="project" /> <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:2.0.5.RELEASE" level="project" /> <orderEntry type="library" name="Maven: com.zaxxer:HikariCP:2.7.9" level="project" /> - <orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" /> <orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.0.9.RELEASE" level="project" /> <orderEntry type="library" scope="RUNTIME" name="Maven: mysql:mysql-connector-java:5.1.47" level="project" /> <orderEntry type="library" name="Maven: com.alibaba:druid:1.1.9" level="project" /> diff --git a/pom.xml b/pom.xml index 5900039..1be7491 100644 --- a/pom.xml +++ b/pom.xml @@ -13,9 +13,14 @@ <artifactId>hx-common</artifactId> <packaging>jar</packaging> - <dependencies> + <!--鑵捐浜� cos--> + <dependency> + <groupId>com.qcloud</groupId> + <artifactId>cos_api</artifactId> + </dependency> + <!--鍙戦�侀偖浠�--> <dependency> <groupId>com.sun.mail</groupId> diff --git a/src/main/java/com/hx/util/COSUtil.java b/src/main/java/com/hx/util/COSUtil.java new file mode 100644 index 0000000..714c7e8 --- /dev/null +++ b/src/main/java/com/hx/util/COSUtil.java @@ -0,0 +1,63 @@ +package com.hx.util; + +import com.aliyun.oss.OSSClient; +import com.aliyun.oss.model.OSSObject; +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.model.PutObjectRequest; +import com.qcloud.cos.model.PutObjectResult; +import com.qcloud.cos.region.Region; +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; + +/**鑵捐浜� COS + * @author ChenJiaHe + * @date 2020-12-03 + */ +public class COSUtil { + + /** 鍚庣璋冪敤涓婁紶鍥剧墖 + * @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); + + //寮�濮嬩笂浼� + 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"); + if(key.startsWith("/")){ + imgUrl.append(key); + }else{ + imgUrl.append("/"+key); + } + return imgUrl.toString(); + + } + +} -- Gitblit v1.8.0