微服务版后端初始化
This commit is contained in:
70
xjrsoft-common/xjrsoft-commom-oss/pom.xml
Normal file
70
xjrsoft-common/xjrsoft-commom-oss/pom.xml
Normal file
@ -0,0 +1,70 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>xjrsoft-common</artifactId>
|
||||
<groupId>com.xjrsoft</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>xjrsoft-commom-oss</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.qiniu</groupId>
|
||||
<artifactId>qiniu-java-sdk</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.huaweicloud</groupId>
|
||||
<artifactId>esdk-obs-java</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.qcloud</groupId>
|
||||
<artifactId>cos_api</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.xjrsoft</groupId>
|
||||
<artifactId>xjrsoft-common-core</artifactId>
|
||||
<version>${xjrsoft.framework.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--引入fastjson依赖-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,123 @@
|
||||
|
||||
package com.xjrsoft.common.oss.config;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
import org.hibernate.validator.constraints.Range;
|
||||
import org.hibernate.validator.constraints.URL;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 云存储配置信息
|
||||
*
|
||||
* @author tzx
|
||||
*/
|
||||
@Data
|
||||
public class CloudStorageConfig implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
*类型 1:七牛 2:阿里云 3:腾讯云
|
||||
*/
|
||||
@Range(min=1, max=3, message = "类型错误")
|
||||
private Integer type;
|
||||
|
||||
/**
|
||||
* 七牛绑定的域名
|
||||
*/
|
||||
@NotBlank(message="七牛绑定的域名不能为空")
|
||||
@URL(message = "七牛绑定的域名格式不正确")
|
||||
private String qiniuDomain;
|
||||
|
||||
/**
|
||||
* 七牛路径前缀
|
||||
*/
|
||||
private String qiniuPrefix;
|
||||
|
||||
/**
|
||||
* 七牛ACCESS_KEY
|
||||
*/
|
||||
@NotBlank(message="七牛AccessKey不能为空")
|
||||
private String qiniuAccessKey;
|
||||
/**
|
||||
* 七牛SECRET_KEY
|
||||
*/
|
||||
@NotBlank(message="七牛SecretKey不能为空")
|
||||
private String qiniuSecretKey;
|
||||
/**
|
||||
* 七牛存储空间名
|
||||
*/
|
||||
@NotBlank(message="七牛空间名不能为空")
|
||||
private String qiniuBucketName;
|
||||
|
||||
/**
|
||||
* 阿里云绑定的域名
|
||||
*/
|
||||
@NotBlank(message="阿里云绑定的域名不能为空")
|
||||
@URL(message = "阿里云绑定的域名格式不正确")
|
||||
private String aliyunDomain;
|
||||
/**
|
||||
* 阿里云路径前缀
|
||||
*/
|
||||
private String aliyunPrefix;
|
||||
/**
|
||||
* 阿里云EndPoint
|
||||
*/
|
||||
@NotBlank(message="阿里云EndPoint不能为空")
|
||||
private String aliyunEndPoint;
|
||||
/**
|
||||
* 阿里云AccessKeyId
|
||||
*/
|
||||
@NotBlank(message="阿里云AccessKeyId不能为空")
|
||||
/**
|
||||
* 阿里云AccessKeySecret
|
||||
*/
|
||||
@NotBlank(message="阿里云AccessKeySecret不能为空")
|
||||
private String aliyunAccessKeySecret;
|
||||
/**
|
||||
* 阿里云BucketName
|
||||
*/
|
||||
@NotBlank(message="阿里云BucketName不能为空")
|
||||
private String aliyunBucketName;
|
||||
|
||||
/**
|
||||
* 腾讯云绑定的域名
|
||||
*/
|
||||
@NotBlank(message="腾讯云绑定的域名不能为空")
|
||||
@URL(message = "腾讯云绑定的域名格式不正确")
|
||||
private String qcloudDomain;
|
||||
/**
|
||||
* 腾讯云路径前缀
|
||||
*/
|
||||
private String qcloudPrefix;
|
||||
/**
|
||||
* 腾讯云AppId
|
||||
*/
|
||||
@NotNull(message="腾讯云AppId不能为空")
|
||||
private Integer qcloudAppId;
|
||||
/**
|
||||
* 腾讯云SecretId
|
||||
*/
|
||||
@NotBlank(message="腾讯云SecretId不能为空")
|
||||
private String qcloudSecretId;
|
||||
/**
|
||||
* 腾讯云SecretKey
|
||||
*/
|
||||
@NotBlank(message="腾讯云SecretKey不能为空")
|
||||
private String qcloudSecretKey;
|
||||
/**
|
||||
* 腾讯云BucketName
|
||||
*/
|
||||
@NotBlank(message="腾讯云BucketName不能为空")
|
||||
private String qcloudBucketName;
|
||||
/**
|
||||
* 腾讯云COS所属地区
|
||||
*/
|
||||
@NotBlank(message="所属地区不能为空")
|
||||
private String qcloudRegion;
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
|
||||
package com.xjrsoft.common.oss.factory;
|
||||
|
||||
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.xjrsoft.common.core.config.OSSConfig;
|
||||
import com.xjrsoft.common.core.enums.CloudType;
|
||||
import com.xjrsoft.common.oss.service.*;
|
||||
|
||||
/**
|
||||
* 文件上传Factory
|
||||
*
|
||||
* @author tzx
|
||||
*/
|
||||
public final class OssFactory {
|
||||
|
||||
private static final OSSConfig OSS_CONFIG;
|
||||
|
||||
|
||||
static {
|
||||
OSS_CONFIG = SpringUtil.getBean(OSSConfig.class);
|
||||
}
|
||||
|
||||
public static CloudStorageService build(){
|
||||
|
||||
if(OSS_CONFIG.getCloudType().getCode() == CloudType.QINIUCLOUD.getCode()){
|
||||
return new QiniuCloudStorageService(OSS_CONFIG);
|
||||
}else if(OSS_CONFIG.getCloudType().getCode() == CloudType.ALICLOUD.getCode()){
|
||||
return new AliyunCloudStorageService(OSS_CONFIG);
|
||||
}else if(OSS_CONFIG.getCloudType().getCode() == CloudType.QCLOUD.getCode()){
|
||||
return new QcloudCloudStorageService(OSS_CONFIG);
|
||||
}else if(OSS_CONFIG.getCloudType().getCode() == CloudType.HWCLOUD.getCode()){
|
||||
return new HwCloudStorageService(OSS_CONFIG);
|
||||
}else if(OSS_CONFIG.getCloudType().getCode() == CloudType.MINIO.getCode()){
|
||||
return new MinioService(OSS_CONFIG);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,67 @@
|
||||
package com.xjrsoft.common.oss.service;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.xjrsoft.common.core.config.OSSConfig;
|
||||
import com.xjrsoft.common.core.constant.StringPool;
|
||||
import com.xjrsoft.common.core.exception.MyException;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 阿里云存储
|
||||
*
|
||||
* @author tzx
|
||||
*/
|
||||
public class AliyunCloudStorageService extends CloudStorageService {
|
||||
private OSSClient client;
|
||||
|
||||
public AliyunCloudStorageService(OSSConfig config){
|
||||
this.config = config;
|
||||
|
||||
//初始化
|
||||
init();
|
||||
}
|
||||
|
||||
private void init(){
|
||||
client = new OSSClient(config.getEndpoint(), config.getAccessKey(),
|
||||
config.getSecretKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
return upload(new ByteArrayInputStream(data), path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
try {
|
||||
client.putObject(config.getBucketName(), path, inputStream);
|
||||
} catch (Exception e){
|
||||
throw new MyException("上传文件失败,请检查配置信息", e);
|
||||
}
|
||||
|
||||
return "https://" + config.getBucketName() + StringPool.DOT + config.getEndpoint() + StringPool.SLASH + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(String objectName) {
|
||||
try {
|
||||
String[] split = objectName.split(config.getBucketName() + StringPool.DOT + config.getEndpoint() + StringPool.SLASH);
|
||||
client.deleteObject(config.getBucketName(),split[1]);
|
||||
} catch (Exception e){
|
||||
throw new RuntimeException("删除文件失败,请检查配置信息", e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,74 @@
|
||||
package com.xjrsoft.common.oss.service;
|
||||
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.xjrsoft.common.core.config.OSSConfig;
|
||||
import com.xjrsoft.common.core.uitls.DateUtils;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 云存储(支持七牛、阿里云、腾讯云、华为)
|
||||
*
|
||||
* @author tzx
|
||||
*/
|
||||
public abstract class CloudStorageService {
|
||||
/** 云存储配置信息 */
|
||||
OSSConfig config;
|
||||
|
||||
/**
|
||||
* 文件路径
|
||||
* @param prefix 前缀
|
||||
* @param suffix 后缀
|
||||
* @return 返回上传路径
|
||||
*/
|
||||
public String getPath(String prefix, String suffix) {
|
||||
//生成uuid
|
||||
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
//文件路径
|
||||
String path = DateUtils.format(new Date(), "yyyyMMdd") + "/" + uuid;
|
||||
|
||||
if(StrUtil.isNotBlank(prefix)){
|
||||
path = prefix + "/" + path;
|
||||
}
|
||||
|
||||
return path + suffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param data 文件字节数组
|
||||
* @param path 文件路径,包含文件名
|
||||
* @return 返回http地址
|
||||
*/
|
||||
public abstract String upload(byte[] data, String path);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param data 文件字节数组
|
||||
* @param suffix 后缀
|
||||
* @return 返回http地址
|
||||
*/
|
||||
public abstract String uploadSuffix(byte[] data, String suffix);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param inputStream 字节流
|
||||
* @param path 文件路径,包含文件名
|
||||
* @return 返回http地址
|
||||
*/
|
||||
public abstract String upload(InputStream inputStream, String path);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param inputStream 字节流
|
||||
* @param suffix 后缀
|
||||
* @return 返回http地址
|
||||
*/
|
||||
public abstract String uploadSuffix(InputStream inputStream, String suffix);
|
||||
|
||||
public abstract boolean delete(String objectName );
|
||||
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
package com.xjrsoft.common.oss.service;
|
||||
|
||||
import com.obs.services.ObsClient;
|
||||
import com.xjrsoft.common.core.config.OSSConfig;
|
||||
import com.xjrsoft.common.core.exception.MyException;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @Author: tzx
|
||||
* @Date:2022/3/8 10:52
|
||||
*/
|
||||
public class HwCloudStorageService extends CloudStorageService {
|
||||
private ObsClient client;
|
||||
|
||||
public HwCloudStorageService(OSSConfig config){
|
||||
this.config = config;
|
||||
|
||||
//初始化
|
||||
init();
|
||||
}
|
||||
|
||||
private void init(){
|
||||
client = new ObsClient(config.getAccessKey(),
|
||||
config.getSecretKey(),config.getEndpoint());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
return upload(new ByteArrayInputStream(data), path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
try {
|
||||
client.putObject(config.getBucketName(), path, inputStream);
|
||||
} catch (Exception e){
|
||||
throw new MyException("上传文件失败,请检查配置信息", e);
|
||||
}
|
||||
|
||||
return config.getEndpoint() + "/" + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(String objectName) {
|
||||
try {
|
||||
client.deleteObject(config.getBucketName(), objectName);
|
||||
} catch (Exception e){
|
||||
throw new RuntimeException("删除文件失败,请检查配置信息", e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,73 @@
|
||||
package com.xjrsoft.common.oss.service;
|
||||
|
||||
import com.xjrsoft.common.core.config.OSSConfig;
|
||||
import com.xjrsoft.common.core.constant.StringPool;
|
||||
import com.xjrsoft.common.core.exception.MyException;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.PutObjectArgs;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author : tzx
|
||||
*/
|
||||
public class MinioService extends CloudStorageService {
|
||||
private MinioClient client;
|
||||
|
||||
public MinioService(OSSConfig config){
|
||||
this.config = config;
|
||||
|
||||
//初始化
|
||||
init();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
private void init(){
|
||||
client = MinioClient.builder().endpoint(config.getEndpoint()).credentials(config.getAccessKey(),config.getSecretKey()).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
return upload(new ByteArrayInputStream(data), path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
try {
|
||||
PutObjectArgs args = PutObjectArgs.builder()
|
||||
.bucket(config.getBucketName())
|
||||
.object(path).stream(inputStream,inputStream.available(), -1).build();
|
||||
client.putObject(args);
|
||||
} catch (Exception e){
|
||||
throw new MyException("上传文件失败,请检查配置信息", e);
|
||||
}
|
||||
|
||||
return config.getEndpoint() + StringPool.SLASH + config.getBucketName() + StringPool.SLASH + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(String objectName) {
|
||||
try {
|
||||
PutObjectArgs args = PutObjectArgs.builder()
|
||||
.bucket(config.getBucketName())
|
||||
.object(objectName).build();
|
||||
client.putObject(args);
|
||||
} catch (Exception e){
|
||||
throw new RuntimeException("删除文件失败,请检查配置信息", e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,97 @@
|
||||
package com.xjrsoft.common.oss.service;
|
||||
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.qcloud.cos.COSClient;
|
||||
import com.qcloud.cos.ClientConfig;
|
||||
import com.qcloud.cos.request.DelFileRequest;
|
||||
import com.qcloud.cos.request.UploadFileRequest;
|
||||
import com.qcloud.cos.sign.Credentials;
|
||||
import com.xjrsoft.common.core.config.OSSConfig;
|
||||
import com.xjrsoft.common.core.exception.MyException;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 腾讯云存储
|
||||
*
|
||||
* @author tzx
|
||||
*/
|
||||
public class QcloudCloudStorageService extends CloudStorageService {
|
||||
private COSClient client;
|
||||
|
||||
public QcloudCloudStorageService(OSSConfig config){
|
||||
this.config = config;
|
||||
|
||||
//初始化
|
||||
init();
|
||||
}
|
||||
|
||||
private void init(){
|
||||
Credentials credentials = new Credentials(config.getAppId(), config.getAccessKey(),
|
||||
config.getSecretKey());
|
||||
|
||||
//初始化客户端配置
|
||||
ClientConfig clientConfig = new ClientConfig();
|
||||
//设置bucket所在的区域,华南:gz 华北:tj 华东:sh
|
||||
clientConfig.setRegion(config.getRegion());
|
||||
|
||||
client = new COSClient(clientConfig, credentials);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
//腾讯云必需要以"/"开头
|
||||
if(!path.startsWith("/")) {
|
||||
path = "/" + path;
|
||||
}
|
||||
|
||||
//上传到腾讯云
|
||||
UploadFileRequest request = new UploadFileRequest(config.getBucketName(), path, data);
|
||||
String response = client.uploadFile(request);
|
||||
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
if(jsonObject.getInteger("code") != 0) {
|
||||
throw new MyException("文件上传失败," + jsonObject.getString("message"));
|
||||
}
|
||||
|
||||
return config.getPrefix() + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
try {
|
||||
byte[] data = IOUtils.toByteArray(inputStream);
|
||||
return this.upload(data, path);
|
||||
} catch (IOException e) {
|
||||
throw new MyException("上传文件失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(String objectName) {
|
||||
try {
|
||||
DelFileRequest request = new DelFileRequest(config.getBucketName(), objectName);
|
||||
String response = client.delFile(request);
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
if (jsonObject.getInteger("code") != 0) {
|
||||
throw new RuntimeException("文件上传失败," + jsonObject.getString("message"));
|
||||
}
|
||||
}catch (Exception e){
|
||||
throw new RuntimeException("文件上传失败,请检查配置" );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,75 @@
|
||||
package com.xjrsoft.common.oss.service;
|
||||
|
||||
import com.qiniu.common.Zone;
|
||||
import com.qiniu.http.Response;
|
||||
import com.qiniu.storage.Configuration;
|
||||
import com.qiniu.storage.UploadManager;
|
||||
import com.qiniu.util.Auth;
|
||||
import com.xjrsoft.common.core.config.OSSConfig;
|
||||
import com.xjrsoft.common.core.exception.MyException;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 七牛云存储
|
||||
*
|
||||
* @author tzx
|
||||
*/
|
||||
public class QiniuCloudStorageService extends CloudStorageService {
|
||||
private UploadManager uploadManager;
|
||||
private String token;
|
||||
|
||||
public QiniuCloudStorageService(OSSConfig config){
|
||||
this.config = config;
|
||||
|
||||
//初始化
|
||||
init();
|
||||
}
|
||||
|
||||
private void init(){
|
||||
uploadManager = new UploadManager(new Configuration(Zone.autoZone()));
|
||||
token = Auth.create(config.getAccessKey(), config.getSecretKey()).
|
||||
uploadToken(config.getBucketName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
try {
|
||||
Response res = uploadManager.put(data, path, token);
|
||||
if (!res.isOK()) {
|
||||
throw new MyException("上传七牛出错:" + res);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new MyException("上传文件失败,请核对七牛配置信息", e);
|
||||
}
|
||||
|
||||
return config.getEndpoint() + "/" + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
try {
|
||||
byte[] data = IOUtils.toByteArray(inputStream);
|
||||
return this.upload(data, path);
|
||||
} catch (IOException e) {
|
||||
throw new MyException("上传文件失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(String objectName) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,60 @@
|
||||
package com.xjrsoft.common.oss.utils;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.xjrsoft.common.core.constant.StringPool;
|
||||
import com.xjrsoft.common.oss.factory.OssFactory;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class UploadUtil {
|
||||
|
||||
//单文件上传
|
||||
public static String uploadFile(MultipartFile file) throws Exception {
|
||||
if (file.isEmpty()) {
|
||||
throw new RuntimeException("上传文件不能为空");
|
||||
}
|
||||
//上传文件
|
||||
String suffix = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(StringPool.DOT));
|
||||
return Objects.requireNonNull(OssFactory.build()).uploadSuffix(file.getBytes(), suffix);
|
||||
}
|
||||
|
||||
//多文件上传
|
||||
public static List<String> uploadFiles(List<MultipartFile> files) throws Exception {
|
||||
if (CollectionUtil.isNotEmpty(files)) {
|
||||
throw new RuntimeException("上传文件不能为空");
|
||||
}
|
||||
List<String> urls = new ArrayList<>();
|
||||
//上传文件
|
||||
for (MultipartFile file : files) {
|
||||
urls.add(uploadFile(file));
|
||||
}
|
||||
return urls;
|
||||
}
|
||||
|
||||
public static InputStream download(String path) {
|
||||
try {
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
Request req = new Request.Builder().url(path).build();
|
||||
InputStream inputStream=null;
|
||||
okhttp3.Response resp = client.newCall(req).execute();
|
||||
if (resp.isSuccessful()) {
|
||||
ResponseBody body = resp.body();
|
||||
inputStream = body.byteStream();
|
||||
}
|
||||
return inputStream;
|
||||
} catch (Exception e){
|
||||
throw new RuntimeException("获取文件失败,请检查配置信息", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean delete(String path) {
|
||||
return Objects.requireNonNull(OssFactory.build()).delete(path);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user