最简单的文件上传-spring-file-storage

概述

Spring File Storage工具几乎整合了市面上所有的OSS对象存储平台,包括本地、FTP、SFTP、WebDAV、阿里云OSS、华为云OBS、七牛云Kodo、腾讯云COS、百度云 BOS、又拍云USS、MinIO、京东云 OSS、网易数帆 NOS等其它兼容 S3 协议的平台,只要在springboot中通过极简的方式就可以实现文件存储。

官网:https://spring-file-storage.xuyanwu.cn

案例:文件上传至阿里云OSS平台

1 导包

        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.10.2</version>
        </dependency>
        <dependency>
            <groupId>cn.xuyanwu</groupId>
            <artifactId>spring-file-storage</artifactId>
            <version>0.5.0</version>
        </dependency>

2 配置文件增加配置

spring:
  #文件存储配置(本地、oss)
  file-storage:
    default-platform: local-1
    thumbnail-suffix: ".min.jpg" #缩略图后缀
    local:
      - platform: local-1 # 存储平台标识
        enable-storage: true #是否开启本存储(只能选一种)
        enable-access: true #启用访问(线上请使用 Nginx 配置,效率更高)
        domain: "http://127.0.0.1:2222" #访问域名,注意后面要和path-patterns保持一致,“/”结尾
        base-path: /tmp/Pictures/ # 存储地址
        path-patterns: /** #访问路径
    aliyun-oss:
      - platform: aliyun-oss
        enable-storage: true
        access-key: 12312323
        secret-key: 32432434
        end-point: 435455545454543
        bucket-name: firebook
        domain: http://fire100.top
        base-path: #云平台文件路径

3 启动类增加@EnableFileStorage 注解

4 案例

4.1 上传文件

@RestController
@RequestMapping("/file")
@Api(tags = "文件管理api")
public class FileController {

    @Autowired
    private FileStorageService fileStorageService;

    @PostMapping("upload")
    @ApiOperation(value = "文件上传",notes = "文件上传")
    public R upload(MultipartFile file){
        FileInfo upload  = fileStorageService.of(file).upload();
        return R.ok(upload);
    }

    /**
     * 上传图片裁剪大小并生成一张缩略图
     */
    @PostMapping("/uploadThumbnail")
    public FileInfo uploadThumbnail(MultipartFile file) {
        return fileStorageService.of(file)
                .image(img -> img.size(1000,1000))  //将图片大小调整到 1000*1000
                .thumbnail(th -> th.size(200,200))  //再生成一张 200*200 的缩略图
                .upload();
    }

    /**ali
     * 上传文件到指定存储平台,成功返回文件信息
     */
    @PostMapping("/upload-platform")
    public FileInfo uploadPlatform(MultipartFile file) {
        return fileStorageService.of(file)
                .setPlatform("aliyun-oss")    //使用指定的存储平台
                .upload();
    }
}

其他案例请参照官网配置,至此,文件上传案例完毕

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天雨编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值