FastDFS实战(五)- Spring Boot集成FastDFS

源码下载地址:https://github.com/tobato/FastDFS_Client

一、 创建Spring Boot项目,并导入fastdfs依赖
<!--fastdfs 客户端依赖-->
<dependency>
    <groupId>com.github.tobato</groupId>
    <artifactId>fastdfs-client</artifactId>
    <version>1.25.2-RELEASE</version>
</dependency>
二、配置
  1. 入口类

    package com.baizhi;
    
    import com.github.tobato.fastdfs.FdfsClientConfig;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.EnableMBeanExport;
    import org.springframework.context.annotation.Import;
    import org.springframework.jmx.support.RegistrationPolicy;
    
    @Import(FdfsClientConfig.class)
    // 解决jmx重复注册bean的问题
    @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
    
    @SpringBootApplication
    public class FastdfsClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(FastdfsClientApplication.class, args);
        }
    }
  2. application.yml

    
    # =========================================================
    
    
    # 分布式文件系统FDFS配置
    
    
    # =========================================================
    
    fdfs:
      soTimeout: 30
      connectTimeout: 60
      thumbImage:             #缩略图生成参数
        width: 150
        height: 150
      trackerList:            #TrackerList参数,支持多个
        - 192.168.128.141:22122
      pool:
        max-total: 100
        max-wait-millis: 60
  3. 测试

package com.baizhi;

import com.github.tobato.fastdfs.domain.GroupState;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.github.tobato.fastdfs.service.TrackerClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.*;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class FastdfsClientApplicationTests {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Autowired
    private TrackerClient trackerClient;

    @Test
    public void contextLoads() {
        List<GroupState> groupStates = trackerClient.listGroups();
        for (GroupState groupState : groupStates) {
            System.out.println(groupState);
        }

    }

    /**
     * 测试文件上传
     */
    @Test
    public void upload() {

        try {
            File file = new File("d:\\ds.jpg");

            FileInputStream inputStream = new FileInputStream(file);
            //StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), "jpg", null);

            //fastFileStorageClient.uploadSlaveFile(storePath.getGroup(),storePath.getPath(),inputStream,inputStream.available(),"a_",null);
            fastFileStorageClient.uploadSlaveFile("group1","M00/00/00/wKiAjVlpNjiAK5IHAADGA0F72jo578.jpg",inputStream,inputStream.available(),"a_",null);

            //System.out.println(storePath.getGroup() + " " + storePath.getPath());

            inputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试上传缩略图
     */
    @Test
    public void uploadCrtThumbImage() {
        try {
            File file = new File("d:\\ds.jpg");

            FileInputStream inputStream = new FileInputStream(file);
            // 测试上传 缩略图
            StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(inputStream, file.length(), "jpg", null);

            System.out.println(storePath.getGroup() + "  " + storePath.getPath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试文件下载
     */
    @Test
    public void download() {
        try {
            byte[] bytes = fastFileStorageClient.downloadFile("group1", "M00/00/00/wKiAjVlpMfiAagnbAADGA0F72jo134_150x150.jpg", new DownloadByteArray());

            FileOutputStream stream = new FileOutputStream("a.jpg");

            stream.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
    * 测试文件删除
     */
    @Test
    public void deleteFile(){
        fastFileStorageClient.deleteFile("group1","M00/00/00/wKiAjVlpQVyARpQwAADGA0F72jo566.jpg");
    }
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值