FastDFS与SpringBoot结合使用小Demo

FastDFS入门小demo

  1. 创建项目(SpringBoot项目)

  2. 导入依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>com.github.tobato</groupId>
        <artifactId>fastdfs-client</artifactId>
        <version>1.26.1-RELEASE</version>
    </dependency>
    <dependency>
       <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    
  3. 进行配置

    server:
      port: 8082
    spring:
      application:
        name: upload-service
      servlet:
        multipart:
          max-file-size: 5MB # 设置文件上传的大小
    fdfs:
      so-timeout: 1501
      connect-timeout: 601
      thumb-image: # 缩略图
        width: 60
        height: 60
      tracker-list: # tracker地址
        - 192.168.0.155:22122
    
  4. 启动类

    @SpringBootApplication
    @EnableDiscoveryClient
    public class LyUploadApplication {
        public static void main(String[] args) {
            SpringApplication.run(LyUploadApplication.class,args);
        }
    }
    
  5. FastClient组件

    @Configuration
    @Import(FdfsClientConfig.class)
    // 解决jmx重复注册bean的问题
    @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
    public class FastClientImporter {
    
    }
    
  6. 单元测试

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = LyUploadApplication.class)
    public class FdfsTest {
    
        @Autowired
        private FastFileStorageClient storageClient;
    
        @Autowired
        private ThumbImageConfig thumbImageConfig;
    
        @Test
        public void testUpload() throws FileNotFoundException {
            File file = new File("F:\\FastDFS\\tiger.jpg");
            // 上传并且生成缩略图
            StorePath storePath = this.storageClient.uploadFile(
                    new FileInputStream(file), file.length(), "jpg", null);
            // 带分组的路径
            System.out.println(storePath.getFullPath());
            // 不带分组的路径
            System.out.println(storePath.getPath());
        }
    }
    
  7. 根据打印的路径访问:
    入门成功访问页面

  8. 小Demo收工,如有疑问,欢迎留言.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值