add 增加 OSS 对象存储模块 相关代码(未完成)
This commit is contained in:
parent
d7fde6fe0d
commit
7e90d84598
14
pom.xml
14
pom.xml
|
@ -33,6 +33,12 @@
|
|||
<lock4j.version>2.2.1</lock4j.version>
|
||||
<datasource.version>3.4.0</datasource.version>
|
||||
|
||||
<!-- OSS 配置 -->
|
||||
<qiniu.version>7.8.0</qiniu.version>
|
||||
<aliyun.oss.version>3.13.0</aliyun.oss.version>
|
||||
<qcloud.cos.version>5.6.47</qcloud.cos.version>
|
||||
<minio.version>8.2.2</minio.version>
|
||||
|
||||
<!-- docker 配置 -->
|
||||
<docker.registry.url>localhost</docker.registry.url>
|
||||
<docker.registry.host>http://${docker.registry.url}:2375</docker.registry.host>
|
||||
|
@ -180,6 +186,13 @@
|
|||
<version>${ruoyi-vue-plus.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- demo模块 -->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-oss</artifactId>
|
||||
<version>${ruoyi-vue-plus.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- demo模块 -->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
|
@ -199,6 +212,7 @@
|
|||
<module>ruoyi-common</module>
|
||||
<module>ruoyi-demo</module>
|
||||
<module>ruoyi-extend</module>
|
||||
<module>ruoyi-oss</module>
|
||||
</modules>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
|
|
@ -42,6 +42,11 @@
|
|||
<artifactId>ruoyi-quartz</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-oss</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 代码生成-->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
<?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>ruoyi-vue-plus</artifactId>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<version>2.5.1</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>ruoyi-oss</artifactId>
|
||||
|
||||
<description>
|
||||
OSS对象存储模块
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- 通用工具-->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.qiniu</groupId>
|
||||
<artifactId>qiniu-java-sdk</artifactId>
|
||||
<version>${qiniu.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>${aliyun.oss.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.qcloud</groupId>
|
||||
<artifactId>cos_api</artifactId>
|
||||
<version>${qcloud.cos.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
<version>${minio.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,69 @@
|
|||
package com.ruoyi.oss.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 云存储配置信息
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "cloud-storage")
|
||||
public class CloudStorageConfig implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 类型 1:七牛 2:阿里云 3:腾讯云 4: minio
|
||||
*/
|
||||
private Integer type;
|
||||
|
||||
/**
|
||||
* 七牛绑定的域名
|
||||
*/
|
||||
private String domain;
|
||||
|
||||
/**
|
||||
* 七牛路径前缀
|
||||
*/
|
||||
private String prefix;
|
||||
|
||||
/**
|
||||
* 七牛ACCESS_KEY
|
||||
*/
|
||||
private String accessKey;
|
||||
|
||||
/**
|
||||
* 七牛SECRET_KEY
|
||||
*/
|
||||
private String secretKey;
|
||||
|
||||
/**
|
||||
* 七牛存储空间名
|
||||
*/
|
||||
private String bucketName;
|
||||
|
||||
/**
|
||||
* 腾讯云AppId
|
||||
*/
|
||||
private Integer qcloudAppId;
|
||||
|
||||
/**
|
||||
* 腾讯云SecretId
|
||||
*/
|
||||
private String qcloudSecretId;
|
||||
|
||||
/**
|
||||
* 腾讯云SecretKey
|
||||
*/
|
||||
private String qcloudSecretKey;
|
||||
|
||||
/**
|
||||
* 腾讯云COS所属地区
|
||||
*/
|
||||
private String qcloudRegion;
|
||||
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.ruoyi.oss.constant;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
|
||||
public class CloudConstant {
|
||||
|
||||
/**
|
||||
* 云存储配置KEY
|
||||
*/
|
||||
public final static String CLOUD_STORAGE_CONFIG_KEY = "sys.oss.cloud-storage";
|
||||
|
||||
/**
|
||||
* 云服务商
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum CloudService {
|
||||
|
||||
/**
|
||||
* 七牛云
|
||||
*/
|
||||
QINIU(1),
|
||||
|
||||
/**
|
||||
* 阿里云
|
||||
*/
|
||||
ALIYUN(2),
|
||||
|
||||
/**
|
||||
* 腾讯云
|
||||
*/
|
||||
QCLOUD(3),
|
||||
|
||||
/**
|
||||
* minio
|
||||
*/
|
||||
MINIO(4);
|
||||
|
||||
private final int value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.ruoyi.oss.exception;
|
||||
|
||||
/**
|
||||
* OSS异常类
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
public class OssException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public OssException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.ruoyi.oss.service;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public interface ICloudStorageService {
|
||||
|
||||
/**
|
||||
* 文件路径
|
||||
*
|
||||
* @param prefix 前缀
|
||||
* @param suffix 后缀
|
||||
* @return 返回上传路径
|
||||
*/
|
||||
String getPath(String prefix, String suffix);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param data 文件字节数组
|
||||
* @param path 文件路径,包含文件名
|
||||
* @return 返回http地址
|
||||
*/
|
||||
String upload(byte[] data, String path);
|
||||
|
||||
/**
|
||||
* 文件删除
|
||||
*
|
||||
* @param path 文件路径,包含文件名
|
||||
*/
|
||||
void delete(String path);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param data 文件字节数组
|
||||
* @param suffix 后缀
|
||||
* @return 返回http地址
|
||||
*/
|
||||
String uploadSuffix(byte[] data, String suffix);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param inputStream 字节流
|
||||
* @param path 文件路径,包含文件名
|
||||
* @return 返回http地址
|
||||
*/
|
||||
String upload(InputStream inputStream, String path);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param inputStream 字节流
|
||||
* @param suffix 后缀
|
||||
* @return 返回http地址
|
||||
*/
|
||||
String uploadSuffix(InputStream inputStream, String suffix);
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.ruoyi.oss.service.abstractd;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.ruoyi.oss.service.ICloudStorageService;
|
||||
import com.ruoyi.oss.utils.DateUtils;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 云存储(支持七牛、阿里云、腾讯云、minio)
|
||||
*/
|
||||
public abstract class AbstractCloudStorageService implements ICloudStorageService {
|
||||
|
||||
/**
|
||||
* 云存储配置信息
|
||||
*/
|
||||
protected CloudStorageConfig config;
|
||||
|
||||
public int getServiceType() {
|
||||
return config.getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPath(String prefix, String suffix) {
|
||||
// 生成uuid
|
||||
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
// 文件路径
|
||||
String path = DateUtils.dateTime() + "/" + uuid;
|
||||
if (StrUtil.isNotBlank(prefix)) {
|
||||
path = prefix + "/" + path;
|
||||
}
|
||||
return path + suffix;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package com.ruoyi.oss.service.impl;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.ruoyi.oss.exception.OssException;
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 阿里云存储
|
||||
*/
|
||||
public class AliyunCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
|
||||
private OSSClient client;
|
||||
|
||||
public AliyunCloudStorageServiceImpl(CloudStorageConfig config) {
|
||||
this.config = config;
|
||||
// 初始化
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
client = new OSSClient(config.getDomain(), 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 OssException("上传文件失败,请检查配置信息");
|
||||
}
|
||||
return config.getDomain() + "/" + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
path = path.replace(config.getDomain() + "/", "");
|
||||
try {
|
||||
client.deleteObject(config.getBucketName(), path);
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请检查配置信息");
|
||||
}
|
||||
}
|
||||
|
||||
@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));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package com.ruoyi.oss.service.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.ruoyi.oss.exception.OssException;
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
import io.minio.MinioClient;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* minio存储
|
||||
*/
|
||||
public class MinioCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
|
||||
private MinioClient minioClient;
|
||||
|
||||
public MinioCloudStorageServiceImpl(CloudStorageConfig config) {
|
||||
this.config = config;
|
||||
// 初始化
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
minioClient = MinioClient.builder()
|
||||
.endpoint(config.getDomain())
|
||||
.credentials(config.getAccessKey(), config.getSecretKey())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
try {
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请核对Minio配置信息");
|
||||
}
|
||||
return config.getDomain() + "/" + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
try {
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new OssException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
byte[] data = IoUtil.readBytes(inputStream);
|
||||
return this.upload(data, 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));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
package com.ruoyi.oss.service.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.qcloud.cos.COSClient;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 腾讯云存储
|
||||
*/
|
||||
public class QcloudCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
|
||||
private COSClient client;
|
||||
|
||||
public QcloudCloudStorageServiceImpl(CloudStorageConfig config) {
|
||||
this.config = config;
|
||||
// 初始化
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
// Credentials credentials = new Credentials(config.getQcloudAppId(), config.getQcloudSecretId(),
|
||||
// config.getQcloudSecretKey());
|
||||
// 初始化客户端配置
|
||||
// ClientConfig clientConfig = new ClientConfig();
|
||||
// // 设置bucket所在的区域,华南:gz 华北:tj 华东:sh
|
||||
// clientConfig.setRegion(config.getQcloudRegion());
|
||||
// client = new COSClient(clientConfig, credentials);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
// 腾讯云必需要以"/"开头
|
||||
if (!path.startsWith("/")) {
|
||||
path = "/" + path;
|
||||
}
|
||||
// 上传到腾讯云
|
||||
// UploadFileRequest request = new UploadFileRequest(config.getQcloudBucketName(), path, data);
|
||||
// String response = client.uploadFile(request);
|
||||
// Map<String, Object> jsonObject = JsonUtils.parseMap(response);
|
||||
// if (Convert.toInt(jsonObject.get("code")) != 0) {
|
||||
// throw new OssException("文件上传失败," + Convert.toStr(jsonObject.get("message")));
|
||||
// }
|
||||
return config.getDomain() + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
// path = path.replace(config.getDomain(),"");
|
||||
// DelFileRequest request = new DelFileRequest(config.getBucketName(), path);
|
||||
// String response = client.delFile(request);
|
||||
// Map<String, Object> jsonObject = JsonUtils.parseMap(response);
|
||||
// if (Convert.toInt(jsonObject.get("code")) != 0) {
|
||||
// throw new OssException("文件删除失败," + Convert.toStr(jsonObject.get("message")));
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
byte[] data = IoUtil.readBytes(inputStream);
|
||||
return this.upload(data, 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));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
package com.ruoyi.oss.service.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.qiniu.http.Response;
|
||||
import com.qiniu.storage.BucketManager;
|
||||
import com.qiniu.storage.Configuration;
|
||||
import com.qiniu.storage.Region;
|
||||
import com.qiniu.storage.UploadManager;
|
||||
import com.qiniu.util.Auth;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.ruoyi.oss.exception.OssException;
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 七牛云存储
|
||||
*/
|
||||
public class QiniuCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
|
||||
private UploadManager uploadManager;
|
||||
private BucketManager bucketManager;
|
||||
private String token;
|
||||
|
||||
public QiniuCloudStorageServiceImpl(CloudStorageConfig config) {
|
||||
this.config = config;
|
||||
// 初始化
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
// z0 z1 z2
|
||||
Configuration config = new Configuration(Region.autoRegion());
|
||||
// 默认不使用https
|
||||
config.useHttpsDomains = false;
|
||||
uploadManager = new UploadManager(config);
|
||||
Auth auth = Auth.create(this.config.getAccessKey(), this.config.getSecretKey());
|
||||
token = auth.uploadToken(this.config.getBucketName());
|
||||
bucketManager = new BucketManager(auth, config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
try {
|
||||
Response res = uploadManager.put(data, path, token);
|
||||
if (!res.isOK()) {
|
||||
throw new RuntimeException("上传七牛出错:" + res.toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请核对七牛配置信息");
|
||||
}
|
||||
return config.getDomain() + "/" + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
try {
|
||||
path = path.replace(config.getDomain() + "/", "");
|
||||
Response res = bucketManager.delete(config.getBucketName(), path);
|
||||
if (!res.isOK()) {
|
||||
throw new RuntimeException("删除七牛文件出错:" + res.toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OssException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
byte[] data = IoUtil.readBytes(inputStream);
|
||||
return this.upload(data, 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));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
package com.ruoyi.oss.utils;
|
||||
|
||||
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 时间工具类
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
public static String YYYY = "yyyy";
|
||||
|
||||
public static String YYYY_MM = "yyyy-MM";
|
||||
|
||||
public static String YYYY_MM_DD = "yyyy-MM-dd";
|
||||
|
||||
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
|
||||
|
||||
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
private static String[] parsePatterns = {
|
||||
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
|
||||
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
|
||||
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
|
||||
|
||||
/**
|
||||
* 获取当前Date型日期
|
||||
*
|
||||
* @return Date() 当前日期
|
||||
*/
|
||||
public static Date getNowDate() {
|
||||
return new Date();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前日期, 默认格式为yyyy-MM-dd
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public static String getDate() {
|
||||
return dateTimeNow(YYYY_MM_DD);
|
||||
}
|
||||
|
||||
public static final String getTime() {
|
||||
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
|
||||
}
|
||||
|
||||
public static final String dateTimeNow() {
|
||||
return dateTimeNow(YYYYMMDDHHMMSS);
|
||||
}
|
||||
|
||||
public static final String dateTimeNow(final String format) {
|
||||
return parseDateToStr(format, new Date());
|
||||
}
|
||||
|
||||
public static final String dateTime(final Date date) {
|
||||
return parseDateToStr(YYYY_MM_DD, date);
|
||||
}
|
||||
|
||||
public static final String parseDateToStr(final String format, final Date date) {
|
||||
return new SimpleDateFormat(format).format(date);
|
||||
}
|
||||
|
||||
public static final Date dateTime(final String format, final String ts) {
|
||||
try {
|
||||
return new SimpleDateFormat(format).parse(ts);
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期路径 即年/月/日 如2018/08/08
|
||||
*/
|
||||
public static final String datePath() {
|
||||
Date now = new Date();
|
||||
return DateFormatUtils.format(now, "yyyy/MM/dd");
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期路径 即年/月/日 如20180808
|
||||
*/
|
||||
public static final String dateTime() {
|
||||
Date now = new Date();
|
||||
return DateFormatUtils.format(now, "yyyyMMdd");
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期型字符串转化为日期 格式
|
||||
*/
|
||||
public static Date parseDate(Object str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return parseDate(str.toString(), parsePatterns);
|
||||
} catch (ParseException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务器启动时间
|
||||
*/
|
||||
public static Date getServerStartDate() {
|
||||
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
|
||||
return new Date(time);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算两个时间差
|
||||
*/
|
||||
public static String getDatePoor(Date endDate, Date nowDate) {
|
||||
long nd = 1000 * 24 * 60 * 60;
|
||||
long nh = 1000 * 60 * 60;
|
||||
long nm = 1000 * 60;
|
||||
// long ns = 1000;
|
||||
// 获得两个时间的毫秒时间差异
|
||||
long diff = endDate.getTime() - nowDate.getTime();
|
||||
// 计算差多少天
|
||||
long day = diff / nd;
|
||||
// 计算差多少小时
|
||||
long hour = diff % nd / nh;
|
||||
// 计算差多少分钟
|
||||
long min = diff % nd % nh / nm;
|
||||
// 计算差多少秒//输出结果
|
||||
// long sec = diff % nd % nh % nm / ns;
|
||||
return day + "天" + hour + "小时" + min + "分钟";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.ruoyi.system.controller;
|
||||
|
||||
|
||||
import com.aliyun.oss.ServiceException;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
import com.ruoyi.common.exception.CustomException;
|
||||
import com.ruoyi.common.utils.PageUtils;
|
||||
import com.ruoyi.common.utils.poi.ExcelUtil;
|
||||
import com.ruoyi.system.domain.SysOss;
|
||||
import com.ruoyi.system.service.ISysOssService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 文件上传 控制层
|
||||
*
|
||||
* @author chkj
|
||||
* @date 2019-07-15
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/system/oss")
|
||||
public class SysOssController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private ISysOssService iSysOssService;
|
||||
|
||||
/**
|
||||
* 查询文件上传列表
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
public TableDataInfo list(SysOss sysOss) {
|
||||
List<SysOss> list = iSysOssService.list(sysOss);
|
||||
return PageUtils.buildDataInfo(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
*/
|
||||
@PostMapping("/upload")
|
||||
public AjaxResult upload(@RequestParam("file") MultipartFile file) {
|
||||
if (file.isEmpty()) {
|
||||
throw new CustomException("上传文件不能为空");
|
||||
}
|
||||
Map<String, String> json = iSysOssService.upload(file);
|
||||
return AjaxResult.success(json);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.ruoyi.system.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 文件上传表 sys_oss
|
||||
*
|
||||
* @author chkj
|
||||
* @date 2019-07-15
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@NoArgsConstructor
|
||||
@TableName("sys_oss")
|
||||
public class SysOss implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
private String fileName;
|
||||
|
||||
/**
|
||||
* 文件后缀名
|
||||
*/
|
||||
private String fileSuffix;
|
||||
|
||||
/**
|
||||
* URL地址
|
||||
*/
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 上传人
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private String createBy;
|
||||
|
||||
/**
|
||||
* 服务商
|
||||
*/
|
||||
private Integer service;
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.ruoyi.system.factory;
|
||||
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
|
||||
/**
|
||||
* 文件上传Factory
|
||||
*/
|
||||
public class OSSFactory {
|
||||
|
||||
// private static ISysConfigService sysConfigService;
|
||||
|
||||
static {
|
||||
// OSSFactory.sysConfigService = SpringUtils.getBean(ISysConfigService.class);
|
||||
}
|
||||
|
||||
public static AbstractCloudStorageService build() {
|
||||
// String jsonconfig = sysConfigService.selectConfigByKey(CloudConstant.CLOUD_STORAGE_CONFIG_KEY);
|
||||
// // 获取云存储配置信息
|
||||
// CloudStorageConfig config = JSON.parseObject(jsonconfig, CloudStorageConfig.class);
|
||||
// if (config.getType() == CloudConstant.CloudService.QINIU.getValue()) {
|
||||
// return new QiniuCloudStorageServiceImpl(config);
|
||||
// } else if (config.getType() == CloudConstant.CloudService.ALIYUN.getValue()) {
|
||||
// return new AliyunCloudStorageServiceImpl(config);
|
||||
// } else if (config.getType() == CloudConstant.CloudService.QCLOUD.getValue()) {
|
||||
// return new QcloudCloudStorageServiceImpl(config);
|
||||
// } else if (config.getType() == CloudConstant.CloudService.MINIO.getValue()) {
|
||||
// return new MinioCloudStorageServiceImpl(config);
|
||||
// }
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.ruoyi.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.ruoyi.system.domain.SysOss;
|
||||
|
||||
/**
|
||||
* 文件上传 数据层
|
||||
*
|
||||
* @author chkj
|
||||
* @date 2019-07-15
|
||||
*/
|
||||
public interface SysOssMapper extends BaseMapper<SysOss> {
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.ruoyi.system.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.ruoyi.system.domain.SysOss;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 文件上传 服务层
|
||||
*
|
||||
* @author chkj
|
||||
* @date 2019-07-15
|
||||
*/
|
||||
public interface ISysOssService extends IService<SysOss> {
|
||||
/**
|
||||
* 列表查询
|
||||
*/
|
||||
List<SysOss> list(SysOss sysOss);
|
||||
|
||||
Map<String, String> upload(MultipartFile file);
|
||||
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.ruoyi.system.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.ruoyi.common.exception.CustomException;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
import com.ruoyi.system.domain.SysOss;
|
||||
import com.ruoyi.system.factory.OSSFactory;
|
||||
import com.ruoyi.system.mapper.SysOssMapper;
|
||||
import com.ruoyi.system.service.ISysOssService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 文件上传 服务层实现
|
||||
*
|
||||
* @author chkj
|
||||
* @date 2019-07-15
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SysOssServiceImpl extends ServiceImpl<SysOssMapper, SysOss> implements ISysOssService {
|
||||
|
||||
@Autowired
|
||||
private CloudStorageConfig config;
|
||||
|
||||
@Override
|
||||
public List<SysOss> list(SysOss sysOss) {
|
||||
LambdaQueryWrapper<SysOss> wrapper = new LambdaQueryWrapper<>();
|
||||
return baseMapper.selectList(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> upload(MultipartFile file) {
|
||||
String originalfileName = file.getOriginalFilename();
|
||||
String suffix = originalfileName.substring(originalfileName.lastIndexOf("."));
|
||||
try {
|
||||
AbstractCloudStorageService storage = OSSFactory.build();
|
||||
String url = storage.uploadSuffix(file.getBytes(), suffix);
|
||||
// 保存文件信息
|
||||
SysOss ossEntity = new SysOss()
|
||||
.setUrl(url).setFileSuffix(suffix)
|
||||
.setFileName(originalfileName)
|
||||
.setService(storage.getServiceType());
|
||||
save(ossEntity);
|
||||
Map<String, String> map = new HashMap<>(2);
|
||||
map.put("url", ossEntity.getUrl());
|
||||
map.put("fileName", ossEntity.getFileName());
|
||||
return map;
|
||||
} catch (IOException e) {
|
||||
throw new CustomException("文件读取异常!!!", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.chkj.cloudStorage.mapper.SysOssMapper">
|
||||
|
||||
<!-- 通用查询映射结果 -->
|
||||
<resultMap id="BaseResultMap" type="com.chkj.cloudStorage.domain.SysOss">
|
||||
<id column="id" property="id"/>
|
||||
<result column="file_name" property="fileName"/>
|
||||
<result column="file_suffix" property="fileSuffix"/>
|
||||
<result column="url" property="url"/>
|
||||
<result column="create_time" property="createTime"/>
|
||||
<result column="create_by" property="createBy"/>
|
||||
<result column="service" property="service"/>
|
||||
</resultMap>
|
||||
|
||||
<!-- 通用查询结果列 -->
|
||||
<sql id="Base_Column_List">
|
||||
id, file_name, file_suffix, url, create_time, create_by, service
|
||||
</sql>
|
||||
|
||||
</mapper>
|
|
@ -0,0 +1,30 @@
|
|||
CREATE TABLE `sys_oss`
|
||||
(
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
`file_name` varchar(64) NOT NULL DEFAULT '' COMMENT '文件名',
|
||||
`file_suffix` varchar(10) NOT NULL DEFAULT '' COMMENT '文件后缀名',
|
||||
`url` varchar(200) NOT NULL COMMENT 'URL地址',
|
||||
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
|
||||
`create_by` varchar(64) NOT NULL DEFAULT '' COMMENT '上传人',
|
||||
`service` tinyint(2) NOT NULL DEFAULT '1' COMMENT '服务商',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE = InnoDB
|
||||
AUTO_INCREMENT = 8
|
||||
DEFAULT CHARSET = utf8 COMMENT ='文件上传';
|
||||
|
||||
INSERT INTO `ry`.`sys_menu` (`menu_name`, `parent_id`, `order_num`, `url`, `menu_type`, `visible`, `perms`, `icon`,
|
||||
`create_by`, `create_time`, `update_by`, `update_time`, `remark`)
|
||||
VALUES ('文件管理', '1', '10', '/system/oss', 'C', '0', 'system:oss:view', '#', 'admin', '2018-11-16 13:59:45', '', NULL,
|
||||
'');
|
||||
INSERT INTO `ry`.`sys_menu` (`menu_name`, `parent_id`, `order_num`, `url`, `menu_type`, `visible`, `perms`, `icon`,
|
||||
`create_by`, `create_time`, `update_by`, `update_time`, `remark`)
|
||||
VALUES ('文件上传', '1056', '1', '#', 'F', '0', 'system:oss:add', '#', 'admin', '2018-11-16 13:59:45', '', NULL, '');
|
||||
INSERT INTO `ry`.`sys_menu` (`menu_name`, `parent_id`, `order_num`, `url`, `menu_type`, `visible`, `perms`, `icon`,
|
||||
`create_by`, `create_time`, `update_by`, `update_time`, `remark`)
|
||||
VALUES ('文件删除', '1056', '2', '#', 'F', '0', 'system:oss:remove', '#', 'admin', '2018-11-16 13:59:45', '', NULL, '');
|
||||
INSERT INTO `ry`.`sys_menu` (`menu_name`, `parent_id`, `order_num`, `url`, `menu_type`, `visible`, `perms`, `icon`,
|
||||
`create_by`, `create_time`, `update_by`, `update_time`, `remark`)
|
||||
VALUES ('文件配置', '1056', '3', '#', 'F', '0', 'system:oss:config', '#', 'admin', '2018-11-16 13:59:45', '', NULL, '');
|
||||
INSERT INTO `ry`.`sys_menu` (`menu_name`, `parent_id`, `order_num`, `url`, `menu_type`, `visible`, `perms`, `icon`,
|
||||
`create_by`, `create_time`, `update_by`, `update_time`, `remark`)
|
||||
VALUES ('文件修改', '1056', '4', '#', 'F', '0', 'system:oss:remove', '#', 'admin', '2018-11-16 13:59:45', '', NULL, '');
|
Loading…
Reference in New Issue