ChenJiaHe
2020-12-03 ccd95bd3a438e6f76a0ecf87338a626a12061c1f
优化
1个文件已修改
11 ■■■■ 已修改文件
src/main/java/com/hx/util/COSUtil.java 11 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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,7 +46,13 @@
        COSClient cosClient = new COSClient(cred, clientConfig);
        //开始上传
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
        ObjectMetadata objectMetadata = new ObjectMetadata();
        // 设置输入流长度为500
        objectMetadata.setContentLength(500);
        // 设置 Content type, 默认是 application/octet-stream,对于本地文件上传,默认根据本地文件的后缀进行映射
        // ,例如 jpg 文件映射 为image/jpeg对于流式上传 默认是 application/octet-stream
        //objectMetadata.setContentType("application/pdf");
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key,localFile.getInputStream(),objectMetadata);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        //拼接路径