添加pom.xml依赖:一定要导入依赖
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.4.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.6</version>
</dependency>
service注解代码
public void putObject(MultipartFile file)
throws IOException, CosServiceException, CosClientException, InterruptedException {
// 初始化秘钥信息
String secretId = "你的secretId";
String secretKey = "你的secretKey ";
// 1 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
// 2 设置bucket的区域, COS地域的简称请参照 ;xy: 此处是'ap-chengdu'成都服务器
ClientConfig clientConfig = new ClientConfig(new Region("ap-chengdu"));
// 3 生成cos客户端
COSClient cosClient = new COSClient(cred, clientConfig);
// xy: 这是执行的高级-上传文件,简单的20M一下的文件可以https://cloud.tencent.com/document/product/436/10199里的上传文件。
// 原文: 将本地文件或者已知长度的输入流内容上传到 COS. 适用于图片类小文件(20M以下)上传,最大支持 5 GB(含), 5 GB 以上必须使用分块上传或高级 API 上传。如果本地文件大部分为20M以上, 建议参考使用接口文档的高级API.
// 如果COS上已存在对象, 则会进行覆盖. 另外因为对象存储中不存在目录,如果要创建目录对象, 请参考接口文档的FAQ.
ExecutorService threadPool = Executors.newFixedThreadPool(32);
// 传入一个 threadpool, 若不传入线程池, 默认 TransferManager 中会生成一个单线程的线程池。
// xy: https://cloud.tencent.com/document/product/436/12263在此地址中,查找方法,如查找到"上传文件"
// 方法名为upload,则调用为 transferManager.upload(putObjectRequest),见xy-A
TransferManager transferManager = new TransferManager(cosClient, threadPool);
// bucket 的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式,该bucket是自己创建的
String bucketName = "appraisal-1253661407";
// xy: 唯一文件名 ; 原文: 对象键(Key)是对象在存储桶中的唯一标识;
String key = new Date().getTime() + "_" + new Random().nextInt(10) + "_source.jpg";
// 获取本地文件
// 方法A:接收的MultipartFile类型参数
CommonsMultipartFile cf = (CommonsMultipartFile) file;
DiskFileItem fi = (DiskFileItem) cf.getFileItem();
File f = fi.getStoreLocation();//xy: 本地文件地址
// 方法B:接收的Attachment类型参数
//储存的地址
String path="某地址"
//获取随机32位数组成,jpg图片名
String fileName = UUID.randomUUID().toString()+".jpg";
DataHandler dh = image.getDataHandler();
/*先存本地
try {
File targetFile = new File(path);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
InputStream ins =dh.getInputStream();
OutputStream out = new FileOutputStream(new File(path+File.separator+fileName));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = ins.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
File f=new File(path+File.separator+fileName)
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, f);
// xy-A 本地文件上传
Upload upload = transferManager.upload(putObjectRequest);
// 等待传输结束(如果想同步的等待上传结束,则调用 waitForCompletion)
UploadResult uploadResult = upload.waitForUploadResult();
// 关闭 TransferManger
transferManager.shutdownNow();
上传后的地址: "https://你的bucketName.piccd.myqcloud.com/" + key
}
完整代码
public void putObject(MultipartFile file)
throws IOException, CosServiceException, CosClientException, InterruptedException {
// 初始化秘钥信息
String secretId = "你的secretId";
String secretKey = "你的secretKey ";
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
ClientConfig clientConfig = new ClientConfig(new Region("ap-chengdu"));
COSClient cosClient = new COSClient(cred, clientConfig);
ExecutorService threadPool = Executors.newFixedThreadPool(32);
TransferManager transferManager = new TransferManager(cosClient, threadPool);
String bucketName = "ABC-123456";
String key = new Date().getTime() + "_" + new Random().nextInt(10) + "_source.jpg";
CommonsMultipartFile cf = (CommonsMultipartFile) file;
DiskFileItem fi = (DiskFileItem) cf.getFileItem();
File f = fi.getStoreLocation();
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, f);
Upload upload = transferManager.upload(putObjectRequest);
UploadResult uploadResult = upload.waitForUploadResult();
transferManager.shutdownNow();
jsonResult.setData("https://ABC-123456.piccd.myqcloud.com/" + key);
return jsonResult;
}