ChenJiaHe
2020-12-03 7b319c5d9e8f133fa63637eb2754b1b6a223599d
优化
1个文件已添加
2个文件已修改
83 ■■■■■ 已修改文件
hx-common.iml 13 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml 7 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/util/COSUtil.java 63 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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" />
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>
src/main/java/com/hx/util/COSUtil.java
New file
@@ -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), 超时, 代理等 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();
    }
}