FastDFS整合springboot2以上版本教程

  1. 引入第三方jar

            
                com.github.tobato
                fastdfs-client
                1.26.5
            
    
  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);
        }
    }
    
    

上传完成。
可以在浏览器查看效果!
FastDFS整合springboot2以上版本教程_第1张图片

你可能感兴趣的:(SpringBoot)