Springboot 上传多张图片到腾讯云(单张)

 代码不难--直接cv 改一下上面的就行 如何还有别的业务要求自己书写

@PostMapping("/upload/")
public Result<List<String>> uploadImages(@RequestParam("files") MultipartFile[] files) {
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = ConstantPropertiesUtil.END_POINT;   //你的地址

String bucketName = ConstantPropertiesUtil.BUCKET_NAME;  // 你的桶名字
// 1 初始化用户身份信息(secretId, secretKey)。
String secretId = ConstantPropertiesUtil.ACCESS_KEY_ID; // 你的id
String secretKey = ConstantPropertiesUtil.ACCESS_KEY_SECRET;  //你的key

List<String> imageUrls = new ArrayList<>();

COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
Region region = new Region(endpoint);
ClientConfig clientConfig = new ClientConfig(region);
clientConfig.setHttpProtocol(HttpProtocol.https);

COSClient cosClient = new COSClient(cred, clientConfig);

try {
fileUploadService.remove(null);
for (MultipartFile file : files) {
if (!file.isEmpty()) {
InputStream inputStream = file.getInputStream();
String originalFilename = file.getOriginalFilename();

String key = generateKey(originalFilename);
String dateUrl = new DateTime().toString("yyyy/MM/dd");
key = dateUrl + "/" + key;

ObjectMetadata objectMetadata = new ObjectMetadata();
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, objectMetadata);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
System.out.println(JSON.toJSONString(putObjectResult));
String url = generateImageUrl(key);
imageUrls.add(url);
FileUpload fileUpload = new FileUpload();
fileUpload.setFileName(url);
fileUploadService.save(fileUpload);
}
}
} catch (CosClientException | IOException e) {
// Handle exception
e.printStackTrace();
} finally {
cosClient.shutdown();
}
return Result.ok(imageUrls);
}

private String generateKey(String originalFilename) {
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
return uuid + "-" + originalFilename;
}

private String generateImageUrl(String key) {
String endpoint = ConstantPropertiesUtil.END_POINT;

String bucketName = ConstantPropertiesUtil.BUCKET_NAME;
return "https://" + bucketName + ".cos." + endpoint + ".myqcloud.com/" + key;
}

package com.rjt.cn.utils;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
* 常量类,读取配置文件application.properties中的配置
*/
@Component
public class ConstantPropertiesUtil implements InitializingBean {

@Value("${你的地址}")
private String region;

@Value("${你的id}")
private String secretId;

@Value("${你的key}")
private String secretKey;

@Value("${你的桶名字}")
private String bucketName;

public static String 你的地址;
public static String 你的id;
public static String 你的key;
public static String 你的桶名字;

@Override
public void afterPropertiesSet() throws Exception {
你的地址 = region;
你的id = secretId;
你的key = secretKey;
你的桶名字 = bucketName;
}
}

配置yml

tencent.cos.file.region=你的地址
tencent.cos.file.secretid=你的id
tencent.cos.file.secretkey=你的key
tencent.cos.file.bucketname=你的桶名字

喜欢的话给个关注 谢谢!好哥们

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值