1 2 3 4 5 6 7 8 9 10 11 12 13
| <!-- spring-file-storage 必须要引入 --> <dependency> <groupId>cn.xuyanwu</groupId> <artifactId>spring-file-storage</artifactId> <version>0.5.0</version> </dependency>
<!-- 阿里云oss --> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.10.2</version> </dependency>
|
application.yml文件中配置些基础信息。
enable-storage:只有状态开启才会被识别到
default-platform:默认的上传平台
domain:生成的文件url中访问的域名
base-path:存储地址
thumbnail-suffix:缩略图后缀
要是上传OSS对象存储平台,将aliyun oss提供的变量配置到相应的模块上即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| spring: file-storage: default-platform: local-1 thumbnail-suffix: ".min.jpg" local: - platform: local-1 enable-storage: true enable-access: true domain: "http://127.0.0.1:2222" base-path: /tmp/Pictures/ path-patterns: /** aliyun-oss: - platform: aliyun-oss enable-storage: true access-key: xxxx secret-key: xxxx end-point: xxx bucket-name: firebook domain: http://fire100.top base-path:
|
springboot启动类中增加注解@EnableFileStorage,显式的开启文件上传功能,到这就可以用了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| @EnableFileStorage @SpringBootApplication public class SpringbootFileStorageApplication {
public static void main(String[] args) { SpringApplication.run(SpringbootFileStorageApplication.class, args); } } 上传文件 接下来在业务类中引入FileStorageService服务,如下只要一行代码就可以完成文件上传,是不是So easy,下载也是如法炮制。
@RestController public class FileController {
@Autowired private FileStorageService fileStorageService;
@PostMapping(value = {"/upload"}) public Object upload(MultipartFile file) { FileInfo upload = fileStorageService.of(file).upload(); return upload; } }
|
不仅如此spring-file-storage还支持多种文件形式,URI、URL、String、byte[]、InputStream、MultipartFile,使开发更加灵活。
件上传功能,更多时候我们都是在上传图片,那就会有动态裁剪图片、生成缩略图的需求,这些 spring-file-storage 都可以很容易实现。
1 2 3 4 5 6 7 8 9 10
|
@PostMapping("/uploadThumbnail") public FileInfo uploadThumbnail(MultipartFile file) { return fileStorageService.of(file) .image(img -> img.size(1000,1000)) .thumbnail(th -> th.size(200,200)) .upload(); }
|
而且我们还可以动态选择上传平台,配置文件中将所有平台开启,在实际使用中自由的选择。
1 2 3 4 5 6 7 8 9
|
@PostMapping("/upload-platform") public FileInfo uploadPlatform(MultipartFile file) { return fileStorageService.of(file) .setPlatform("aliyun-oss") .upload(); }
|
下载文件
下载文件也同样的简单,可以直接根据文件url或者文件流下载。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
@PostMapping("/download") public void download(MultipartFile file) { FileInfo fileInfo = fileStorageService.getFileInfoByUrl("http://file.abc.com/test/a.jpg"); fileStorageService.download(fileInfo).file("C:\\a.jpg");
fileStorageService.download("http://file.abc.com/test/a.jpg").file("C:\\a.jpg");
fileStorageService.downloadTh(fileInfo).file("C:\\th.jpg"); } 提供了监听下载进度的功能,可以清晰明了的掌握文件的下载情况。
fileStorageService.download(fileInfo).setProgressMonitor(new ProgressListener() { @Override public void start() { System.out.println("下载开始"); }
@Override public void progress(long progressSize,long allSize) { System.out.println("已下载 " + progressSize + " 总大小" + allSize); }
@Override public void finish() { System.out.println("下载结束"); } }).file("C:\\a.jpg");
|
文件存在、删除
我们还可以根据文件的URL地址来判断文件是否存在、以及删除文件。
//直接通过文件信息中的 url 删除,省去手动查询文件信息记录的过程
fileStorageService.delete(“http://file.abc.com/test/a.jpg“);
//直接通过文件信息中的 url 判断文件是否存在,省去手动查询文件信息记录的过程
boolean exists2 = fileStorageService.exists(“http://file.abc.com/test/a.jpg“);
切面工具还提供了每种操作的切面,可以在每个动作的前后进行干预,比如打日志或者玩点花活,实现FileStorageAspect类重写对应动作的xxxAround方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
@Slf4j @Component public class LogFileStorageAspect implements FileStorageAspect {
@Override public FileInfo uploadAround(UploadAspectChain chain, FileInfo fileInfo, UploadPretreatment pre, FileStorage fileStorage, FileRecorder fileRecorder) { log.info("上传文件 before -> {}",fileInfo); fileInfo = chain.next(fileInfo,pre,fileStorage,fileRecorder); log.info("上传文件 after -> {}",fileInfo); return fileInfo; } }
|