介绍
用于开始组建OSS文件服务项目 以完成了的对接
- Minio
 - 七牛云
 - 本地上传
 
- 业务中很多上次下载的工作,以前是在项目中直接写文件的上次下载,后面改成了OSS服务器,由于很多外包项目所以导致需要对接很多种OSS服务,所有有了这个公共的文件服务上传下载基础API
 - 此项目用到的是Spring 的 SPI
 
依赖
<properties><java.version>1.8</java.version><okhttp3.version>4.9.0</okhttp3.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- web starters --><dependency><groupId>cn.jdevelops</groupId><artifactId>starters-jdevelops-boot-web</artifactId><exclusions><exclusion><groupId>cn.jdevelops</groupId><artifactId>webs-jwt</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.4</version></dependency><!--oss spi--><dependency><groupId>cn.jdevelops</groupId><!-- <artifactId>minio-driver</artifactId>--><artifactId>qiniu-driver</artifactId><!-- <artifactId>local-driver</artifactId>--><version>2.0.4</version></dependency></dependencies>
使用
接口
package cn.jdevelops.file.controller;import cn.jdevelops.exception.exception.BusinessException;import cn.jdevelops.file.OssOperateAPI;import cn.jdevelops.file.bean.*;import cn.jdevelops.result.result.ResultVO;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletResponse;import javax.validation.Valid;import java.util.List;/*** 文件上传下载** @author lxw* @version V1.0* @date 2021/11/10*/@RestController@Api(tags = "文件操作", value = "文件管理")public class FileController {@Autowiredprivate OssOperateAPI fileOperation;@ApiOperation(value = "文件上传", notes = "文件管理")@PostMapping(value = "upload")public ResultVO<FilePathResult> upload(@Valid UploadDTO uploadDTO) {try {FilePathResult filePathResult = fileOperation.uploadFile(uploadDTO);return ResultVO.successForData(filePathResult);} catch (Exception e) {throw new BusinessException("文件上传失败!", e);}}@GetMapping("/download")@ApiOperation(value = "文件下载", notes = "文件管理")public void download(HttpServletResponse response, @Valid DownloadDTO dto) {try {fileOperation.downloadFile(response, dto);} catch (Exception e) {throw new BusinessException("文件下载失败!");}}@GetMapping("/getExpiryObjectUrl")@ApiOperation(value = "获取有效期访问地址", notes = "文件管理")public ResultVO<String> getExpiryObjectUrl(@Valid ExpireDateDTO dto) {try {String url = fileOperation.expireDateUrl(dto);return ResultVO.successForData(url);} catch (Exception e) {throw new BusinessException("获取有效期访问地址失败!");}}@DeleteMapping("/removeObjects")@ApiOperation(value = "删除", notes = "文件管理")public ResultVO<List<String>> removeObjects(@RequestBody @Valid RemoveFileDTO dto) {try {fileOperation.removeFiles(dto);return ResultVO.success();} catch (Exception e) {throw new BusinessException("删除失败!",e);}}}
必看
如果需要切换OSS服务器,不需要改动任何代码,只需要做到如下两点:

