栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

分布式文件存储FastDFS使用总结,从搭建到文件上传完整过程

分布式文件存储FastDFS使用总结,从搭建到文件上传完整过程

文章目录

一、搭建FastDFS二、项目配置三、文件操作四、总结

一、搭建FastDFS
    安装镜像、启动容器
#镜像拉取
docker pull morunchang/fastdfs

#运行tracker
docker run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh

#运行storage
docker run -d --name storage --net=host -e TRACKER_IP=(宿主机ip地址):22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh
    开机启动设置
docker update --restart=always tracker
docker update --restart=always storage
    开放端口
二、项目配置
    引入依赖

      net.oschina.zcx7878
      fastdfs-client-java
      1.27.0.0

    在resources文件夹增加FastDFS配置文件:fdfs_client.conf文件
connect_timeout=60
network_timeout=60
charset=UTF-8
http.tracker_http_port=8080
tracker_server=(宿主机ip地址):22122

    配置文件yml
# max-file-size是单个文件大小,max-request-size是设置总上传的数据大小
spring:
  servlet:
    multipart:
      max-file-size: 10MB
      max-request-size: 10MB
  application:
    name: file
三、文件操作
    文件信息类
public class FastDFSFile implements Serializable {

    //文件名字
    private String name;
    //文件内容
    private byte[] content;
    //文件扩展名
    private String ext;
    //文件MD5摘要值
    private String md5;
    //文件创建作者
    private String author;

    public FastDFSFile(String name, byte[] content, String ext, String md5, String author) {
        this.name = name;
        this.content = content;
        this.ext = ext;
        this.md5 = md5;
        this.author = author;
    }

    public FastDFSFile(String name, byte[] content, String ext) {
        this.name = name;
        this.content = content;
        this.ext = ext;
    }

    public FastDFSFile() {
    }

    //..get..set..toString
}
    文件工具类
package com.changgou.util;

import com.changgou.file.FastDFSFile;
import org.csource.common.NamevaluePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;


public class FastDFSClient {
    
    static {
        try {
            //获取tracker的配置文件fdfs_client.conf的位置
            String filePath = new ClassPathResource("fdfs_client.conf").getPath();
            //加载tracker配置信息
            ClientGlobal.init(filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    
    public static String[] upload(FastDFSFile file){
        //获取文件作者
        NamevaluePair[] meta_list = new NamevaluePair[1];
        meta_list[0] =new NamevaluePair(file.getAuthor());

        
        String[] uploadResults = null;
        try {
            StorageClient storageClient = getStorageClient();
            uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return uploadResults;
    }
    
    public static FileInfo getFile(String groupName, String remoteFileName){
        try {
            //获取StorageClient对象
            StorageClient storageClient = getStorageClient();
            //获取文件信息
            return storageClient.get_file_info(groupName,remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    
    public static InputStream downFile(String groupName, String remoteFileName){
        try {
            //获取StorageClient对象
            StorageClient storageClient = getStorageClient();
            //通过StorageClient下载文件
            byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
            //将字节数组转换成字节输入流
            return new ByteArrayInputStream(fileByte);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    
    public static void deleteFile(String groupName,String remoteFileName){
        try {
            //获取StorageClient对象
            StorageClient storageClient = getStorageClient();
            //通过StorageClient删除文件
            storageClient.delete_file(groupName,remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    
    public static StorageServer getStorages(String groupName){
        try {
            //创建TrackerClient对象
            TrackerClient trackerClient = new TrackerClient();
            //通过TrackerClient获取TrackerServer对象
            TrackerServer trackerServer = trackerClient.getConnection();
            //通过trackerClient获取Storage组信息
            return trackerClient.getStoreStorage(trackerServer,groupName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    
    public static ServerInfo[] getServerInfo(String groupName, String remoteFileName){
        try {
            //创建TrackerClient对象
            TrackerClient trackerClient = new TrackerClient();
            //通过TrackerClient获取TrackerServer对象
            TrackerServer trackerServer = trackerClient.getConnection();
            //获取服务信息
            return trackerClient.getFetchStorages(trackerServer,groupName,remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    
    public static String getTrackerUrl(){
        try {
            //获取TrackerServer对象
            TrackerServer trackerServer = getTrackerServer();
            //获取Tracker地址
            return "http://"+trackerServer.getInetSocketAddress().getHostString()+":"+ClientGlobal.getG_tracker_http_port();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static TrackerServer getTrackerServer() throws IOException {
        //获取TrackerServer
        TrackerClient trackerClient=new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        return trackerServer;
    }
    public static StorageClient getStorageClient() throws IOException {
        //获取TrackerServer
        TrackerServer trackerServer = getTrackerServer();
        //创建StorageClient
        StorageClient storageClient=new StorageClient(trackerServer,null);
        return storageClient;
    }
}

    工具调用
	@PostMapping("/upload")
    public String upload(@RequestParam("file")MultipartFile file) throws IOException {
        //文件名、文件字节数组、文件扩展名
        FastDFSFile fastDFSFile=new FastDFSFile(file.getOriginalFilename(),
                file.getBytes(),
                StringUtils.getFilenameExtension(file.getOriginalFilename()));
        String[] uploads = FastDFSClient.upload(fastDFSFile);
        //组装文件上传地址
        return FastDFSClient.getTrackerUrl()+"/"+uploads[0]+"/"+uploads[1];

    }
    填写Headers,body,测试,复制返回连接打开即可看到原文件。
Key:Content-Type
Value:multipart/form-data

四、总结
    搭建FastDFS,开放端口。引入依赖,编写application配置文件,编写FastDFS配置文件,编写文件信息类,文件工具类。调用工具类方法进行上传等文件操作。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/700937.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号