统一结果返回
package org.itkoukou.oss.ossload.config;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 响应信息主体
*/
@Data
@NoArgsConstructor
public class R<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 成功
*/
public static final int SUCCESS = 200;
/**
* 失败
*/
public static final int FAIL = 500;
private int code;
private String msg;
private T data;
public static <T> R<T> ok() {
return restResult(null, SUCCESS, "操作成功");
}
public static <T> R<T> ok(T data) {
return restResult(data, SUCCESS, "操作成功");
}
public static <T> R<T> ok(String msg) {
return restResult(null, SUCCESS, msg);
}
public static <T> R<T> ok(String msg, T data) {
return restResult(data, SUCCESS, msg);
}
public static <T> R<T> fail() {
return restResult(null, FAIL, "操作失败");
}
public static <T> R<T> fail(String msg) {
return restResult(null, FAIL, msg);
}
public static <T> R<T> fail(T data) {
return restResult(data, FAIL, "操作失败");
}
public static <T> R<T> fail(String msg, T data) {
return restResult(data, FAIL, msg);
}
public static <T> R<T> fail(int code, String msg) {
return restResult(null, code, msg);
}
/**
* 返回警告消息
*
* @param msg 返回内容
* @return 警告消息
*/
public static <T> R<T> warn(String msg) {
return restResult(null, HttpStatus.WARN, msg);
}
/**
* 返回警告消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 警告消息
*/
public static <T> R<T> warn(String msg, T data) {
return restResult(data, HttpStatus.WARN, msg);
}
private static <T> R<T> restResult(T data, int code, String msg) {
R<T> r = new R<>();
r.setCode(code);
r.setData(data);
r.setMsg(msg);
return r;
}
public static <T> Boolean isError(R<T> ret) {
return !isSuccess(ret);
}
public static <T> Boolean isSuccess(R<T> ret) {
return R.SUCCESS == ret.getCode();
}
}
package org.itkoukou.oss.ossload.config;
/**
* 返回状态码
*/
public interface HttpStatus {
/**
* 操作成功
*/
int SUCCESS = 200;
/**
* 对象创建成功
*/
int CREATED = 201;
/**
* 请求已经被接受
*/
int ACCEPTED = 202;
/**
* 操作已经执行成功,但是没有返回数据
*/
int NO_CONTENT = 204;
/**
* 资源已被移除
*/
int MOVED_PERM = 301;
/**
* 重定向
*/
int SEE_OTHER = 303;
/**
* 资源没有被修改
*/
int NOT_MODIFIED = 304;
/**
* 参数列表错误(缺少,格式不匹配)
*/
int BAD_REQUEST = 400;
/**
* 未授权
*/
int UNAUTHORIZED = 401;
/**
* 访问受限,授权过期
*/
int FORBIDDEN = 403;
/**
* 资源,服务未找到
*/
int NOT_FOUND = 404;
/**
* 不允许的http方法
*/
int BAD_METHOD = 405;
/**
* 资源冲突,或者资源被锁
*/
int CONFLICT = 409;
/**
* 不支持的数据,媒体类型
*/
int UNSUPPORTED_TYPE = 415;
/**
* 系统内部错误
*/
int ERROR = 500;
/**
* 接口未实现
*/
int NOT_IMPLEMENTED = 501;
/**
* 系统警告消息
*/
int WARN = 601;
}
公共代码
@Resource
private FileStorageService x_file;//注入实列
public MinioClient minioClient ;//注入实列
//获取时间列表
public static final String FILEPATH = String.format("%s/", java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.of
maven(这里使用开源项目)
<!-- 引入本项目 -->
<dependency>
<groupId>org.dromara.x-file-storage</groupId>
<artifactId>x-file-storage-spring</artifactId>
<version>2.1.0</version>
</dependency>
<!--minio-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.0.3</version>
</dependency>
YML配置
dromara:
x-file-storage: #文件存储配置
default-platform: minio-1 #默认使用的存储平台
thumbnail-suffix: ".min.jpg" #缩略图后缀,例如【.min.jpg】【.png】
huawei-obs:
- platform: huawei-obs-1 # 存储平台标识
enable-storage: false # 启用存储
access-key: ??
secret-key: ??
end-point: ??
bucket-name: ??
domain: ?? # 访问域名,注意“/”结尾,例如:http://abc.obs.com/
base-path: test/ # 基础路径
- platform: huawei-obs-2 # 存储平台标识,这与这里不能重复
enable-storage: false # 启用存储
access-key: ??
secret-key: ??
end-point: ??
bucket-name: ??
domain: ?? # 访问域名,注意“/”结尾,例如:http://abc.obs.com/
base-path: test2/ # 基础路径
aliyun-oss:
- platform: aliyun-oss-1 # 存储平台标识
enable-storage: false # 启用存储
access-key: ??
secret-key: ??
end-point: ??
bucket-name: ??
domain: ?? # 访问域名,注意“/”结尾,例如:https://abc.oss-cn-shanghai.aliyuncs.com/
base-path: test/ # 基础路径
minio:
- platform: minio-1 # 存储平台标识
enable-storage: true # 启用存储
access-key: minioadmin
secret-key: minioadmin
end-point: http://192.168.0.189:9000/
bucket-name: upload
domain: http://192.168.0.189:9000/upload/ # 访问域名,注意“/”结尾,例如:http://minio.abc.com/abc/
base-path: / # 基础路径
server:
port: 1999
spring:
servlet:
multipart:
max-file-size: 100MB # 设置最大文件大小为10MB
max-request-size: 200MB # 设置最大请求大小为20MB
上传单文件
/**
* 上传普通附件
*/
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public R<FileInfo> upload(@RequestPart("file") MultipartFile file) {
return R.ok(x_file.of(file)
.setPath(FILEPATH)
.setObjectId(cn.hutool.core.util.IdUtil.getSnowflakeNextIdgetSnowflakeNextId())
.upload());
}
上传文件夹(C+V100%能用
/**
* @param filePath 文件夹路径
*/
private String uploadFolder(String filePath) throws Exception {
String format = String.format("%s/", java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy/MM/dd")));
File file = new File(filePath);
String rootName = file.getName();
minioClient = ((MinioFileStorage) x_file.getFileStorage()).getClient();
if (!file.isDirectory()) {
upload(file, rootName , format , minioClient);
} else {
recursion(file, rootName , format , minioClient);
}
return "";
}
public void recursion(File file, String rootName , String format , MinioClient client) throws Exception {
File[] files = file.listFiles();
if (files != null) {
for (File currFile : files) {
if (currFile.isDirectory()) {
recursion(currFile, rootName , format , client );
} else {
upload(currFile, rootName , format ,client);
}
}
}
}
public void upload(File file, String rootName , String format , MinioClient client ) throws Exception {
String rootPath = file.getAbsolutePath().substring(file.getAbsolutePath().indexOf(rootName)).replace("\\", "/");
client.uploadObject(UploadObjectArgs.builder()
.bucket(((MinioFileStorage) x_file.getFileStorage()).getBucketName())
.object(format + "/" + rootPath)
.filename(file.getAbsolutePath())
.build()
);
}