文章目录
- Spring Boot 集成 File Storage
Spring Boot 集成 File Storage
统一依赖管理
<!-- spring-file-storage -->
<spring-file-storage.version>0.7.0</spring-file-storage.version>
<minio.versioin>8.4.3</minio.versioin>
<okhttp.version>4.10.0</okhttp.version>
<!-- spring-file-storage 必须要引入 -->
<dependency>
<groupId>cn.xuyanwu</groupId>
<artifactId>spring-file-storage</artifactId>
<version>${spring-file-storage.version}</version>
</dependency>
<!-- MinIO 不使用的情况下可以不引入 -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>${minio.versioin}</version>
</dependency>
<!-- okhttp 目前是 minio 使用 -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
配置类
package com.qboot.filestorage.config;
import cn.xuyanwu.spring.file.storage.EnableFileStorage;
import org.springframework.context.annotation.Configuration;
/**
* X Spring File Storage 配置
* 文档:https://spring-file-storage.xuyanwu.cn/#/
*
* @author Tellsea
* @date 2023/2/16
*/
@EnableFileStorage
@Configuration
public class FileStorageConfig {
}
切面配置保存路径
package com.qboot.filestorage.aspect;
import cn.hutool.core.date.DateUtil;
import cn.xuyanwu.spring.file.storage.FileInfo;
import cn.xuyanwu.spring.file.storage.UploadPretreatment;
import cn.xuyanwu.spring.file.storage.aspect.FileStorageAspect;
import cn.xuyanwu.spring.file.storage.aspect.UploadAspectChain;
import cn.xuyanwu.spring.file.storage.platform.FileStorage;
import cn.xuyanwu.spring.file.storage.recorder.FileRecorder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* 对象存储切面
*
* @author Tellsea
* @date 2023/2/17
*/
@Slf4j
@Component
public class DefaultFileStorageAspect implements FileStorageAspect {
@Override
public FileInfo uploadAround(UploadAspectChain chain, FileInfo fileInfo, UploadPretreatment pre, FileStorage fileStorage, FileRecorder fileRecorder) {
fileInfo.setPath(DateUtil.format(DateUtil.date(), "yyyy/MM/dd/"));
fileInfo = chain.next(fileInfo, pre, fileStorage, fileRecorder);
return fileInfo;
}
}
控制层
package com.qboot.filestorage.controller;
import com.qboot.common.entity.Result;
import com.qboot.filestorage.service.DefaultFileStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
* 对象存储
*
* @author Tellsea
* @date 2023/2/16
*/
@Api(tags = "对象存储")
@RestController
@RequestMapping("/fileStorage")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class DefaultFileStorageController {
private final DefaultFileStorageService defaultFileStorageService;
@ApiOperation("文件上传")
@PostMapping("/upload")
public Result upload(@RequestParam("file") MultipartFile file) {
return defaultFileStorageService.upload(file);
}
}
接口
package com.qboot.filestorage.service;
import com.qboot.common.entity.Result;
import org.springframework.web.multipart.MultipartFile;
/**
* 对象存储 接口
*
* @author Tellsea
* @date 2023/2/17
*/
public interface DefaultFileStorageService {
/**
* 文件上传
*
* @param file
* @return
*/
Result upload(MultipartFile file);
}
接口实现类
package com.qboot.filestorage.service.impl;
import cn.xuyanwu.spring.file.storage.FileInfo;
import cn.xuyanwu.spring.file.storage.FileStorageService;
import cn.xuyanwu.spring.file.storage.platform.FileStorage;
import com.qboot.common.consts.MsgConst;
import com.qboot.common.entity.Result;
import com.qboot.common.utils.AssertUtils;
import com.qboot.filestorage.service.DefaultFileStorageService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* 对象存储 接口实现类
*
* @author Tellsea
* @date 2023/2/17
*/
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class DefaultFileStorageServiceImpl implements DefaultFileStorageService {
private final FileStorageService fileStorageService;
@Override
public Result upload(MultipartFile file) {
AssertUtils.isEmpty(file, "文件对象不能为空");
FileInfo fileInfo = fileStorageService.of(file).upload();
AssertUtils.isEmpty(file, MsgConst.UPLOAD_ERROR.getMsg());
return Result.ok(MsgConst.UPLOAD_SUCCESS, fileInfo);
}
}