低调人的gravatar头像
低调人 2019-12-27 20:51:51
SpringBoot2.x整合FastDFS

本篇博客学习SpringBoot 2.1.11.RELEASE整合FastDFS。

FastDFS作用

FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件上传、文件下载等,解决了大容量存储和负载均衡的问题。

安装连接:

CentOS 7 安裝FastDFS V6.0.3

我们开始吧

新建一个springboot项目

pom文件

加入fastdfs-client-java包

<dependency>
	<groupId>org.csource</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27-SNAPSHOT</version>
</dependency>

fdfs_client.conf

注意:fastdfs-client-java包mvn库中没有编译包,需要自己下载编译到自己的mvn本地库中,官方地址:https://github.com/happyfish100/fastdfs-client-java

resources文件夹下新建fdfs_client.conf,编写如下:

connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 6666
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 192.168.31.100:22122

编写fastdfs初始化连接配置工具类文件工具类

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.stereotype.Service;

import java.io.*;

@Service
public class FastDFSService {

    FastDFSService() throws IOException, MyException {
        ClientGlobal.init("fdfs_client.conf");
    }



    public String upload(byte[] bs, String stringbe) {
        TrackerServer trackerServer = null;
        StorageServer storageServer = null;
        String fileIds = null;
        try {
            trackerServer = init();
            StorageClient1 storageClient = new StorageClient1(trackerServer, storageServer);
            fileIds = storageClient.upload_file1(bs, getFileExt(stringbe), null);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        } finally {
            close(storageServer, trackerServer);
        }
        return fileIds;
    }

    public byte[] download(String groupName) {
        TrackerServer trackerServer = null;
        StorageServer storageServer = null;
        byte[] b = null;
        try {
            trackerServer = init();
            StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
            b = storageClient1.download_file1(groupName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(storageServer, trackerServer);
        }
        return b;
    }

    public FileInfo getFileInfo(String groupName) {
        TrackerServer trackerServer = null;
        StorageServer storageServer = null;
        FileInfo fi = null;
        try {
            trackerServer = init();
            StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
            fi = storageClient1.get_file_info1(groupName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(storageServer, trackerServer);
        }
        return fi;
    }

    public NameValuePair[] getFileMate(String groupName) {
        TrackerServer trackerServer = null;
        StorageServer storageServer = null;
        NameValuePair nvps[] = null;
        try {
            trackerServer = init();
            StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
            nvps = storageClient1.get_metadata1(groupName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(storageServer, trackerServer);
        }
        return nvps;
    }

    public int delete(String groupName) {
        TrackerServer trackerServer = null;
        StorageServer storageServer = null;
        int i = 0;
        try {
            trackerServer = init();
            StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
            i = storageClient1.delete_file1(groupName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(storageServer, trackerServer);
        }
        return i;
    }

    public byte[] File2byte(File file) {
        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

    private TrackerServer init() {
        TrackerServer trackerServer = null;
        try {
            TrackerClient tracker = new TrackerClient();
            trackerServer = tracker.getConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return trackerServer;
    }

    private void close(StorageServer storageServer, TrackerServer trackerServer) {
        try {
            if (storageServer != null) {
                storageServer.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if (trackerServer != null) {
                trackerServer.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String getFileExt(String fileName) {
        if (StringUtils.isBlank(fileName) || !fileName.contains(".")) {
            return "";
        } else {
            return fileName.substring(fileName.lastIndexOf(".") + 1); // 不带最后的点

        }
    }

    public String getFileName(String fileName) {
        if (StringUtils.isBlank(fileName) || !fileName.contains("/")) {
            return "";
        } else {
            return fileName.substring(fileName.lastIndexOf("/") + 1); // 不带最后的点

        }
    }
}

编写测试类演示案例

import cn.cicoding.service.FastDFSService;
import org.apache.commons.io.IOUtils;
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.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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

    @Autowired
    private FastDFSService fastDFSService;

    @Test
    public void contextLoads() throws IOException {
        String local_filename = "C:\\Users\\Public\\Pictures\\Sample Pictures\\123.jpg";
        File f=new File(local_filename);
        String groupName= fastDFSService.upload(fastDFSService.File2byte(f),f.getName());
        System.out.println(groupName);
        IOUtils.write(fastDFSService.download(groupName), new FileOutputStream("D:/app/fastdfs/"+fastDFSService.getFileName(groupName)));
        System.out.println(fastDFSService.getFileInfo(groupName));
        System.out.println(fastDFSService.getFileMate(groupName));
        System.out.println(fastDFSService.delete(groupName)==0 ? "删除成功" : "删除失败");

    }

}

运行测试类得到结果

group1/M00/00/00/wKgfZF3vl6WAJDW5AAvWFlS1kOw230.jpg
123
.jpg
删除成功

源码联系我可以获取!

个人博客

https://www.cicoding.cn/

申请假如我的群聊: 

点击链接加入群聊【Java/Python架构师①群】:https://jq.qq.com/?_wv=1027&k=y2jkgX0q

qq群号码 : 415777345

 

QQ扫码添加群: 

SpringBoot2.x整合FastDFS

 


打赏

已有2人打赏

已注销用户的gravatar头像 spring全家桶的gravatar头像
最近浏览
人间蒸发  LV23 2020年9月22日
chenhuahao  LV18 2020年5月11日
已注销用户  LV34 2020年4月1日
spring全家桶  LV4 2020年3月17日
tsc1988  LV1 2020年2月1日
低调人  LV38 2019年12月28日
最代码官方  LV167 2019年12月28日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友