【SpringBoot 学习】54、Spring Boot 集成 File Storage 实现各个平台文件对象处理

文章介绍了如何在SpringBoot应用中集成FileStorage库,用于对象存储。配置了相关依赖,包括FileStorage、MinIO和OkHttp,并提供了配置类、切面处理保存路径的代码示例,以及控制器和服务层的接口和实现,用于文件上传。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Tellsea

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

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

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

打赏作者

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

抵扣说明:

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

余额充值