代码不难--直接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=你的桶名字
喜欢的话给个关注 谢谢!好哥们