FastDFS整合springboot2以上版本教程

  1. 引入第三方jar

            <dependency>
                <groupId>com.github.tobato</groupId>
                <artifactId>fastdfs-client</artifactId>
                <version>1.26.5</version>
            </dependency>
    
  2. 修改配置文件

    fdfs:
      # 链接超时
      connect-timeout: 60
      # 读取时间
      so-timeout: 60
      # 生成缩略图参数
      thumb-image:
        width: 150
        height: 150
      tracker-list: xxx:xxx:xxx:xxx:22122
    
  3. FastDFS配置bean

    package com.chen.facemanager.conf;
    
    import com.github.tobato.fastdfs.FdfsClientConfig;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableMBeanExport;
    import org.springframework.context.annotation.Import;
    import org.springframework.jmx.support.RegistrationPolicy;
    
    @Configuration
    @Import(FdfsClientConfig.class)
    // Jmx重复注册bean的问题
    @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
    public class DfsConfig {
    }
    
  4. 使用单元测试

    package com.chen.facemanager;
    
    import com.chen.facemanager.util.FileDfsUtil;
    import com.github.tobato.fastdfs.domain.fdfs.StorePath;
    import com.github.tobato.fastdfs.service.FastFileStorageClient;
    import org.apache.commons.lang3.StringUtils;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.web.multipart.MultipartFile;
    
    import javax.annotation.Resource;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class FastDFS {
    
        @Resource
        private FastFileStorageClient storageClient ;
    
    
        @Test
        public void test() throws FileNotFoundException {
    
            File file = new File("C:\\Users\\Mr-CHEN\\Pictures\\sao.png");
            StorePath storePath = storageClient.uploadFile(null,new FileInputStream(file),file.length(),"png");
            System.out.println(storePath);
        }
    }
    
    

上传完成。
可以在浏览器查看效果!
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值