Springboot整合fastdfs-分布式文件存储

一、快速开始

1、添加依赖
xml 复制代码
 <dependency>
    <groupId>com.luhuiguo</groupId>
    <artifactId>fastdfs-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>
<dependency>
    <groupId>org.csource</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27-SNAPSHOT</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
	<optional>true</optional>
</dependency>
2、添加配置项
yaml 复制代码
# fdfs配置
fdfs:
  connect-timeout: 2000 # 连接服务器超时时间
  so-timeout: 3000    
  tracker-list: ${ip}:${port}
  http:
    path: http://${ip}:${port}/ #http链接前缀   
3、新建 fdfs_client.conf(可忽略)
properties 复制代码
connect_timeout = 60
network_timeout = 60
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = 123456
tracker_server = 192.168.53.85:22122
2、FastDFS客户端工具
java 复制代码
@Slf4j
public class FastDFSClient {
	//分片客户端
    private static TrackerClient trackerClient;
    //分片服务端
	private static TrackerServer trackerServer;
    //存储桶客户端
	private static StorageClient storageClient;
    //存储桶服务端
	private static StorageServer storageServer;
	
	static {
		try {
            //引入配置
			String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();;
			ClientGlobal.init(filePath);
			trackerClient = new TrackerClient();
			trackerServer = trackerClient.getConnection();
			storageServer = trackerClient.getStoreStorage(trackerServer);
		} catch (Exception e) {
			logger.error("FastDFS Client Init Fail!",e);
		}
	}
    
    //文件上传
    @SneakyThrows
	public static String[] upload(FastDFSFile file) {
		NameValuePair[] meta_list = new NameValuePair[1];
		meta_list[0] = new NameValuePair("author", file.getAuthor());
		long startTime = System.currentTimeMillis();
		String[] uploadResults = null;
		storageClient = new StorageClient(trackerServer, storageServer);
        uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);
        if(null == uploadResults) return null;
		String groupName = uploadResults[0];
		String remoteFileName = uploadResults[1];
		return uploadResults;
	}
    
    //获取File
    @SneakyThrows
	public static FileInfo getFile(String groupName, String remoteFileName) {
		storageClient = new StorageClient(trackerServer, storageServer);
        return storageClient.get_file_info(groupName, remoteFileName);
	}
    
    //下载文件
    @SneakyThrows
	public static InputStream downFile(String groupName, String remoteFileName) {
		storageClient = new StorageClient(trackerServer, storageServer);
        byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
        InputStream ins = new ByteArrayInputStream(fileByte);
        return ins;
	}
    
    //删除文件
    @SneakyThrows
	public static void deleteFile(String groupName, String remoteFileName) {
		storageClient = new StorageClient(trackerServer, storageServer);
		int i = storageClient.delete_file(groupName, remoteFileName);
		logger.info("delete file successfully!!!" + i);
	}
    
    //获取存储器
    @SneakyThrows
	public static StorageServer[] getStoreStorages(String groupName){
		return trackerClient.getStoreStorages(trackerServer, groupName);
	}
    
    //获取FetchStorages
    @SneakyThrows
	public static ServerInfo[] getFetchStorages(String groupName,String remoteFileName) {
		return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
	}
    
    // 获取存储器的URL
    @SneakyThrows
	public static String getTrackerUrl() {
		return "http://"+trackerServer.getInetSocketAddress().getHostString()+":"
            +ClientGlobal.getG_tracker_http_port()+"/";
	}
    @Data
    public static class FastDFSFile {
        //文件名
        private String name;
        //文件内容
        private byte[] content;
        //文件拓展名
        private String ext;
        //文件的md5
        private String md5;
        //文件做着
        private String author;
    }
}
相关推荐
m0_748238635 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
深度Linux1 小时前
深入探讨Ceph:分布式存储架构的未来
分布式·ceph·架构·c/c++
菜鸟阿达1 小时前
spring boot 2.7 + seata +微服务 降级失败问题修复
spring boot·后端·微服务
清河__1 小时前
【Go】十七、grpc 服务的具体功能编写
开发语言·后端·golang
张声录11 小时前
国密算法Sm2工具类--golang实现版
开发语言·后端·golang
張葒兵3 小时前
记一次命令行启动springboot项目的问题 java -jar的问题
java·spring boot·jar
后季暖4 小时前
kafka stream对比flink
分布式·flink·kafka
一小路一10 小时前
从0-1学习Mysql第五章: 索引与优化
数据库·后端·学习·mysql·面试
mah66610 小时前
SpringBoot项目连接Oracle视图报错整理
spring boot·后端·oracle
IT学长编程10 小时前
计算机毕业设计 基于SpringBoot的智慧社区管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·后端·毕业设计·课程设计·论文笔记·1024程序员节