206 lines
6.7 KiB
Java
206 lines
6.7 KiB
Java
package com.agri.common.utils;
|
|
|
|
|
|
import com.agri.common.config.OSSConfig;
|
|
import com.agri.common.exception.BusinessException;
|
|
import com.aliyun.oss.OSS;
|
|
import com.aliyun.oss.common.utils.BinaryUtil;
|
|
import com.aliyun.oss.model.MatchMode;
|
|
import com.aliyun.oss.model.PolicyConditions;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.io.InputStream;
|
|
import java.util.Date;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
|
|
@Slf4j
|
|
@Component
|
|
public class AliyunOSSUtils {
|
|
|
|
/**
|
|
* 图片格式
|
|
*/
|
|
private static final String[] FILE_TYPE = new String[]{".bmp", ".jpg",
|
|
".jpeg", ".gif", ".png", ".mp4", ".jfif", ".ico", ".pdf"};
|
|
|
|
@Resource
|
|
private OSS ossClient;
|
|
|
|
@Resource
|
|
private OSSConfig ossConfig;
|
|
|
|
/**
|
|
* 图像上传
|
|
*
|
|
* @param uploadFile 图像流处理
|
|
* @return 文件访问路径
|
|
*/
|
|
public String upLoadFile(MultipartFile uploadFile) {
|
|
return ossPutObject(uploadFile, null);
|
|
}
|
|
|
|
/**
|
|
* 图像上传
|
|
*
|
|
* @param uploadFile 图像流处理
|
|
* @param prefixKey 文件/对象名称的前缀,类似目录,例如:backend/
|
|
* @return 文件访问路径
|
|
*/
|
|
public String upLoadFile(MultipartFile uploadFile, String prefixKey) {
|
|
return ossPutObject(uploadFile, prefixKey);
|
|
}
|
|
|
|
@Deprecated
|
|
public String uploadAvatar(MultipartFile uploadFile) {
|
|
return ossPutObject(uploadFile,null);
|
|
}
|
|
|
|
/**
|
|
* 图片上传
|
|
*
|
|
* @param inputStream 图像流处理
|
|
* @return 文件访问路径
|
|
*/
|
|
@Deprecated
|
|
public String upLoadFile(InputStream inputStream) {
|
|
String suffix = ".png";
|
|
//文件名
|
|
String key = ossConfig.getPrefixKey() + UUID.randomUUID().toString()
|
|
.replace("-", "") + suffix;
|
|
try {
|
|
ossClient.putObject(ossConfig.getBucketName(), key, inputStream);
|
|
inputStream.close();
|
|
log.info("--------图片上传成功--------");
|
|
} catch (Exception e) {
|
|
log.info("oos上传失败:" + e.getMessage());
|
|
throw new BusinessException("图片上传失败");
|
|
}
|
|
//返回文件访问路径
|
|
return key;
|
|
}
|
|
|
|
/**
|
|
* 图片上传
|
|
*
|
|
* @param inputStream 图像流处理
|
|
* @return 文件访问路径
|
|
*/
|
|
public String upLoadOtherFile(InputStream inputStream, String filename) {
|
|
String suffix = filename.substring(filename.lastIndexOf("."));
|
|
//文件名
|
|
String key = ossConfig.getPrefixKey() + UUID.randomUUID().toString()
|
|
.replace("-", "") + suffix;
|
|
try {
|
|
ossClient.putObject(ossConfig.getBucketName(), key, inputStream);
|
|
inputStream.close();
|
|
} catch (Exception e) {
|
|
log.error("oos上传失败:{}", e.getMessage());
|
|
throw new BusinessException("文件上传失败");
|
|
}
|
|
//返回文件访问路径
|
|
return key;
|
|
}
|
|
|
|
/**
|
|
* 校验文件后缀
|
|
*
|
|
* @param suffix 文件后缀
|
|
* @return
|
|
*/
|
|
private void verifySuffix(String suffix) {
|
|
// 校验文件格式
|
|
for (String type : FILE_TYPE) {
|
|
if (type.equalsIgnoreCase(suffix)) {
|
|
return;
|
|
}
|
|
}
|
|
throw new BusinessException("暂不支持此格式的文件");
|
|
}
|
|
|
|
/**
|
|
* 获取签名
|
|
*
|
|
* @return
|
|
*/
|
|
public Map<String, String> getSign() {
|
|
String accessId = ossConfig.getAccessKeyId(); // 请填写您的AccessKeyId。
|
|
String endpoint = ossConfig.getEndpoint(); // 请填写您的 endpoint。
|
|
String bucket = ossConfig.getBucketName(); // 请填写您的 bucketname 。
|
|
String host = "https://" + bucket + "." + endpoint; // host的格式为 bucketname.endpoint
|
|
String dir = ossConfig.getPrefixKey(); // 用户上传文件时指定的前缀。
|
|
|
|
try {
|
|
long expireTime = 180;//60秒
|
|
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
|
|
Date expiration = new Date(expireEndTime);
|
|
PolicyConditions policyConds = new PolicyConditions();
|
|
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
|
|
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
|
|
|
|
String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
|
|
byte[] binaryData = postPolicy.getBytes("utf-8");
|
|
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
|
|
String postSignature = ossClient.calculatePostSignature(postPolicy);
|
|
|
|
Map<String, String> respMap = new LinkedHashMap<>();
|
|
respMap.put("accessid", accessId);
|
|
respMap.put("policy", encodedPolicy);
|
|
respMap.put("signature", postSignature);
|
|
respMap.put("dir", dir);
|
|
respMap.put("host", host);
|
|
respMap.put("expire", String.valueOf(expireEndTime / 1000));
|
|
|
|
return respMap;
|
|
|
|
} catch (Exception e) {
|
|
// Assert.fail(e.getMessage());
|
|
System.out.println(e.getMessage());
|
|
throw new BusinessException("获取签名失败");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* oss 文件上传并获得访问路径
|
|
*
|
|
* @param uploadFile 需要上传的文件
|
|
* @param prefixKey 文件/对象名称的前缀,类似目录 例如:backend/
|
|
* @return
|
|
*/
|
|
private String ossPutObject(MultipartFile uploadFile, String prefixKey) {
|
|
|
|
//获取文件名称
|
|
String originalFilename = uploadFile.getOriginalFilename();
|
|
//文件后缀
|
|
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
|
|
verifySuffix(suffix);
|
|
long size = uploadFile.getSize();
|
|
if (size > ossConfig.getMaxSize()) {
|
|
throw new BusinessException("文件超出大小限制");
|
|
}
|
|
//前缀,类似目录
|
|
if (StringUtils.isBlank(prefixKey)) {
|
|
prefixKey = ossConfig.getPrefixKey();
|
|
}
|
|
//文件路径=prefixKey+文件名称
|
|
String key = prefixKey + UUID.randomUUID().toString()
|
|
.replace("-", "") + suffix;
|
|
try {
|
|
InputStream inputStream = uploadFile.getInputStream();
|
|
ossClient.putObject(ossConfig.getBucketName(), key, inputStream);
|
|
inputStream.close();
|
|
log.info("--------文件上传成功--------");
|
|
} catch (Exception e) {
|
|
log.error("oos上传失败:", e);
|
|
throw new BusinessException("图片上传失败");
|
|
}
|
|
//返回文件访问路径
|
|
return key;
|
|
}
|
|
|
|
}
|