首页 > 其他分享 >【SpringBoot 学习】54、Spring Boot 集成 File Storage 实现各个平台文件对象处理

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

时间:2023-04-29 11:04:28浏览次数:32  
标签:SpringBoot spring storage Boot file 54 import com cn



文章目录

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


标签:SpringBoot,spring,storage,Boot,file,54,import,com,cn
From: https://blog.51cto.com/u_15269008/6236652

相关文章

  • 【SpringBoot 学习】53、Spring Boot 集成 Spring Boot Admin
    文章目录SpringBoot集成SpringBootAdminSpringBootAdmin服务端SpringBootAdmin客户端SpringBoot集成SpringBootAdminSpringBootAdmin是服务端、客户端模式。如果把两个端搭建在同一个项目中也可以,但是客户端要是挂了,服务端也挂了,所以可以但没必要搭建独立的S......
  • SpringBoot RabbitMQ死信队列
    1.死信定义无法被消费的消息,称为死信。如果死信一直留在队列中,会导致一直被消费,却从不消费成功,专门有一个存放死信的队列,称为死信队列(DDX,dead-letter-exchange)。死信队列DLX,DeadLetterExchange的缩写,又死信邮箱、死信交换机。其实DLX就是一个普通的交换机,和一般的交换......
  • springboot 发送邮件
    @AutowiredprivateJavaMailSenderjavaMailSender;publicStringsendEmail(ToMailtoMail){SimpleMailMessagemessage=newSimpleMailMessage();message.setFrom("[email protected]");message.setTo("my_M......
  • jeecgboot整合JdbcTemplate方便多表联合查询
    感觉jeecgboot处理复杂的多表联合查询有点费劲,就自己实现了JdbcTemplate的整合,其实也不是整合吧,因为jeecgboot已经把JdbcTemplate整合进来了。我查了下项目的依赖关系,发现jeecg-boot-base-core模块依赖了mybatis-plus-boot-starter,而mybatis-plus-boot-starter依赖了spri......
  • Spring Boot经验
    Spring、SpringBoot经验本文记录作者在实际使用Spring或则SpringBoot过程中遇到比较好的案例或则经验,以供开发学习使用1.校验篇生产过程中前后端都会进行数据格式的校验,后端校验一般采用JSR303的校验模式1.1使用引入依赖<dependency><groupId>javax.validation<......
  • Spring Boot 和 Docker 实现微服务部署
    Springboot开发轻巧的微服务提供了便利,Docker的发展又极大的方便了微服务的部署。这篇文章介绍一下如果借助maven来快速的生成微服务的镜像以及快速启动服务。其实将SpringBoot项目用Docker部署也不是什么多么神秘的技术,也要先生成镜像,再用镜像启动容器,如果说有什么方便......
  • 你一直在用的 Spring Boot Starters 究竟是怎么回事
    SpringBoot对比SpringMVC最大的优点就是使用简单,约定大于配置。不会像之前用SpringMVC的时候,时不时被xml配置文件搞的晕头转向,冷不防还因为xml配置上的一点疏忽,导致整个项目莫名其妙的不可用,顿感生活无所依恋,简称生无可恋。这要归功于组成了SpringBoot的各种各样的......
  • vscode下搭建springboot
    安装两个扩展JavaExtensionforPackSpringBootExtensionPack配置mavenctrl+,搜索java.configuration.maven输入setting.xml的路径注意路径不能有中文或者空格创建springboot项目ctrl+shift+p创建项目,输入springbootInitializer即可。参考博客vscode配置ma......
  • jeecgboot的jar分离打包部署
    因为jeecgboot单体应用打包的jar包太大了,得200M左右,每次更新后上传云服务器有点费劲,所以看了官网有分离打包部署的方法,但是按照官网的步骤啊,发现会出现问题,我现在把我最后成功的配置写下来,我的版本是jeecg-boot-2.4.6,如果其他版本就只能参考下了。主要是修改jeecg-boot-m......
  • Maven指令打包SpringBoot项目提示没有主清单文件
    Maven指令打包SpringBoot项目提示没有主清单文件原文链接:https://blog.csdn.net/greedystar/article/details/86068314项目打包为Jar后,通过java-jarxxxxx.jar运行时提示xxxxx.jar中没有主清单属性,如下:打开jar包,META-INF目录下的MANIFEST.MF,内容如下:Manifest-Version:1.0A......