前言:最近在写一个小论坛,其中一项需求是用户上传图片进行保存。考虑到自己的云服务器配置太低,所以有了使用腾讯云对象存储服务的想法。所以这里主要说说我是怎么把文件不保存到本地服务器直接上传到腾讯云COS的。
目录
1、SpringBoot接收到MultiPartFile类型的图片文件
1、SpringBoot接收到MultiPartFile类型的图片文件
@RequestMapping("/test")
public String test(MultipartFile postpicture) {
if (postpicture==null) {
return "null";
}
System.out.println(postpicture);
return "success";
}
2、查阅COS相关文档写上传工具类
package club.hkw.utils;
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.ObjectMetadata;
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.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
public class COSUploadUtil {
// 初始化用户身份信息
String secretId = "";
String secretKey = "";
// 地域
String bucketRegion = "";
// bucket名称
String bucketName = "";
//根据需要设置,参考官方文档
String basicPath = "";
public boolean upLoadFile2COS(Long fileSize, String filename, MultipartFile file, String userid) throws IOException {
// 创建cos客户端
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
Region region = new Region(bucketRegion);
ClientConfig clientConfig = new ClientConfig(region);
COSClient cosClient = new COSClient(cred, clientConfig);
// 获取输入流
InputStream inputStream = new BufferedInputStream(file.getInputStream());
ObjectMetadata objectMetadata = new ObjectMetadata();
// 设置输入流长度为500
// 这里要强调一下,因为腾讯云支持本地文件上传和文件流上传,为了不必要的麻烦所以选择文件流上传,根据官方文档,为了避免oom,必须要设置元数据并告知输入流长度
objectMetadata.setContentLength(fileSize);
// 具体用法参考官方文档
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, basicPath + userid + "/" + filename, inputStream, objectMetadata);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
// 完成上传之后,关闭连接
destory(cosClient);
// 通过回调函数判断是否上传成功,有etag信息则表示上传成功,否则上传失败
if (putObjectResult.getETag() != null)
return true;
else
return false;
}
// 关闭连接
public void destory(COSClient cosClient) {
cosClient.shutdown();
}
}
3、在controller里面调用工具类进行上传
@RequestMapping("/test")
public String test(MultipartFile postpicture) throws IOException {
if (postpicture==null) {
return "null";
}
// 这里说一下传递的四个参数的含义:
//postpicture.getSize():图片的文件大小,对应工具类里面的字节流长度
//test.jsp:文件的名称,具体看工具类
//postpicture:multipartFile类型的图片文件
//111111:COS上面所在文件的文件夹名称(如果没有该文件夹,自动创建,根据需要设置)
new COSUploadUtil().upLoadFile2COS(postpicture.getSize(), "test.jpg", postpicture, "111111");
return "111";
}
4、SpringBoot相关配置
1.cos sdk的依赖
<!-- 腾讯云对象存储 -->
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.15</version>
</dependency>
2.文件上传相关依赖
<!-- 文件上传下载需要的依赖 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!-- 文件上传依赖 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
3.由于springboot默认对上传文件大小有限制,所以需要配置配置文件application.yml(不同的springboot版本配置方式不同,详情自己查阅百度谷歌,这里是springboot2.0以上的配置),根据自己的需要进行配置就行了
spring:
servlet:
multipart:
max-file-size: 3MB
max-request-size: 60MB