1.引入依赖
<!--阿里云OSS依赖-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.4.2</version>
</dependency>
2.添加工具类
package com.liao.blogs.utils;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/**
* @BelongsProject: blogs_java
* @BelongsPackage: com.liao.blogs.service
* @Author: YunYang Liao
* @CreateTime: 2023-04-22 14:01
* @Description: TODO 阿里云OSS对象存储工具类
* @Version: 1.0
*/
@Slf4j
@Component
public class OssUtil {
//==========================下面五项配置修改成自己的========================//
// 区域
private static String endpoint = "***********";
// 访问id
private static String accessKeyId = "***********";
// 访问秘钥
private static String accessKeySecret = "***********";
// 桶名称
private static String bucketName = "***********";
// 访问URL
private static String url = "***********";
// 日期格式化对象
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
/**
* 获取OSS连接
*/
public OSSClient getOSSClient() {
// 创建
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// 判断仓库是否存在
if (ossClient.doesBucketExist(bucketName)) {
// System.out.println("仓库存在!");
log.info("获取连接成功");
} else {
// 没有创建仓库
log.error("仓库不存在");
throw new RuntimeException("仓库不存在");
}
return ossClient;
}
/**
* 基于OSS上传文件
*
* @param multipartFile 封装的文件上传对象
* @param bussessType 文件上传类型
* @return 返回访问文件的url
*/
public String upLoad(MultipartFile multipartFile, String bussessType) {
// 获取OSS连接
OSSClient ossClient = this.getOSSClient();
// 获取文件后缀名
String filename = multipartFile.getOriginalFilename();
filename.substring(filename.lastIndexOf("."));
String date = sdf.format(new Date());
// 获取uuid作为文件名
String uuid = UUID.randomUUID().toString().replace("-", "");
// 组合文件名
String fileName = bussessType + "/" + date + "/" + uuid + "." + bussessType;
String fileUrl = null;
// 通过OSSClient获取上传文件后返回的fileUrl
try {
// meta设置请求头
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType("image/jpg");
// ossClient.putObject(bucketName, fileName, in, meta);
// 上传图片
ossClient.putObject(bucketName, fileName, new ByteArrayInputStream(multipartFile.getBytes()), meta);
fileUrl = url + fileName;
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
} finally {
// 出现异常关闭连接
ossClient.shutdown();
}
log.info("上传成功");
return fileUrl;
}
public static String getcontentType(String FilenameExtension) {
if (FilenameExtension.equalsIgnoreCase(".bmp")) {
return "image/bmp";
}
if (FilenameExtension.equalsIgnoreCase(".gif")) {
return "image/gif";
}
if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
FilenameExtension.equalsIgnoreCase(".jpg") ||
FilenameExtension.equalsIgnoreCase(".png")) {
return "image/jpg";
}
if (FilenameExtension.equalsIgnoreCase(".html")) {
return "text/html";
}
if (FilenameExtension.equalsIgnoreCase(".txt")) {
return "text/plain";
}
if (FilenameExtension.equalsIgnoreCase(".vsd")) {
return "application/vnd.visio";
}
if (FilenameExtension.equalsIgnoreCase(".pptx") ||
FilenameExtension.equalsIgnoreCase(".ppt")) {
return "application/vnd.ms-powerpoint";
}
if (FilenameExtension.equalsIgnoreCase(".docx") ||
FilenameExtension.equalsIgnoreCase(".doc")) {
return "application/msword";
}
if (FilenameExtension.equalsIgnoreCase(".xml")) {
return "text/xml";
}
return "image/jpg";
}
}
3.文件过大产生异常解决方案
文件过大产生异常需要在application.yml中配置最大文件上传大小
spring:
servlet:
multipart:
# 设置文件大小
max-file-size: 100MB
max-request-size: 100MB