• 第2-1-4章 SpringBoot整合FastDFS文件存储服务


    5 SpringBoot整合

    5.1 操作步骤

    1. 配置FastDFS执行环境
    2. 文件上传配置
    3. 整合Swagger2测试接口

    5.2 项目依赖

    
    
        com.github.tobato
        fastdfs-client
        1.26.5
    
    
    
    
        io.springfox
        springfox-swagger2
        2.9.2
    
    
        io.springfox
        springfox-swagger-ui
        2.9.2
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    5.3 客户端开发

    5.3.1 FastDFS配置

    fdfs:
      # 链接超时
      connect-timeout: 60
      # 读取时间
      so-timeout: 60
      # 生成缩略图参数
      thumb-image:
        width: 150
        height: 150
      tracker-list: 192.168.86.101:22122
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    5.3.2 FastDFS配置类

    @Configuration
    @Import(FdfsClientConfig.class)
    // 避免Jmx重复注册bean
    @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
    public class DFSConfig {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    5.3.3 文件工具类

    import com.github.tobato.fastdfs.domain.fdfs.StorePath;
    import com.github.tobato.fastdfs.service.FastFileStorageClient;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    import org.springframework.util.StringUtils;
    import org.springframework.web.multipart.MultipartFile;
    import javax.annotation.Resource;
    
    
    @Component
    public class FileDfsUtil {
        private static final Logger LOGGER = LoggerFactory.getLogger(FileDfsUtil.class);
        @Resource
        private FastFileStorageClient storageClient ;
        /**
         * 上传文件
         */
        public String upload(MultipartFile multipartFile) throws Exception{
            String originalFilename = multipartFile.getOriginalFilename().
                    substring(multipartFile.getOriginalFilename().
                            lastIndexOf(".") + 1);
            StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
                    multipartFile.getInputStream(),
                    multipartFile.getSize(),originalFilename , null);
            return storePath.getFullPath() ;
        }
    
        /**
         * 删除文件
         */
        public void deleteFile(String fileUrl) {
            if (StringUtils.isEmpty(fileUrl)) {
                LOGGER.info("fileUrl == >>文件路径为空...");
                return;
            }
            try {
                StorePath storePath = StorePath.parseFromUrl(fileUrl);
                storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
            } catch (Exception e) {
                LOGGER.info(e.getMessage());
            }
        }
    }
    
    • 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
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    5.3.4 文件上传配置

    spring:
      application:
        name: fdfs-demo
      jackson:
        time-zone: GMT+8
        date-format: yyyy-MM-dd HH:mm:ss
      servlet:
        multipart:
          max-file-size: 100MB
          max-request-size: 100MB
          enabled: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    5.3.5 配置Swagger2

    配置类:

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    
    /**
     * Swagger 配置文件
     */
    @Configuration
    public class SwaggerConfig {
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.itheima.fdfs.demo"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("SpringBoot利用Swagger构建API文档")
                    .description("Fast DFS接口")
                    .version("version 1.0")
                    .build();
        }
    }
    
    • 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

    启动类:

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @EnableSwagger2
    @SpringBootApplication
    public class FdfsDemoApplication {
    	public static void main(String[] args) {
    		SpringApplication.run(FdfsDemoApplication.class, args);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    5.3.6 API接口

    import com.itheima.fdfs.demo.common.FileDfsUtil;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.http.ResponseEntity;
    import org.springframework.util.StringUtils;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.multipart.MultipartFile;
    
    import javax.annotation.Resource;
    
    @RestController
    public class FileController {
        @Resource
        private FileDfsUtil fileDfsUtil ;
        /**
         * http://localhost:8081/swagger-ui.html
         */
        @ApiOperation(value="上传文件", notes="测试FastDFS文件上传")
        @RequestMapping(value = "/uploadFile",headers="content-type=multipart/form-data", method = RequestMethod.POST)
        public ResponseEntity<String> uploadFile (@RequestParam("file") MultipartFile file){
            String result ;
            try{
                String path = fileDfsUtil.upload(file) ;
                if (!StringUtils.isEmpty(path)){
                    result = path ;
                } else {
                    result = "上传失败" ;
                }
            } catch (Exception e){
                e.printStackTrace() ;
                result = "服务异常" ;
            }
            return ResponseEntity.ok(result);
        }
    
        /**
         * 文件删除
         */
        @RequestMapping(value = "/deleteByPath", method = RequestMethod.GET)
        public ResponseEntity<String> deleteByPath (){
            String filePathName = "group1/M00/00/00/rBIAAmNmi82AJxLsAABdrZgsqUU214.jpg" ;
            fileDfsUtil.deleteFile(filePathName);
            return ResponseEntity.ok("SUCCESS") ;
        }
    }
    
    • 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
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    5.4 接口演示

    1. 访问页面:http://localhost:8081/fdfs-demo/swagger-ui.html
      在这里插入图片描述

    2. 测试接口

      • 上传接口
      • 删除接口
  • 相关阅读:
    无信号交叉口车辆通行控制研究
    NOIP2023模拟1联测22 黑暗料理
    如何克隆笔记本电脑上的硬盘?
    ChinaTravel成流量密码,景区如何打造视频监控管理平台提升旅游体验
    深度学习之语义分割算法(入门学习)
    低成本实现webhook接收端[python]
    各种信息收集
    nacos配置中心源码分析——拉取配置信息
    负载均衡的艺术:释放 Ribbon 的潜力
    洗袜子的洗衣机哪款好?高性价比小型洗衣机测评
  • 原文地址:https://blog.csdn.net/weixin_42208775/article/details/127743272