一、FastDFS简介
分布式文件系统FastDFS:是一个开源的轻量级分布式文件系统,由跟踪服务器(tracker server)、存储服务器(storage server)和客户端(client)三个部分组成,作为一个分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题,特别适合中小文件(建议范围:4KB < file_size <500MB),对以文件为载体的在线服务,如相册网站、视频网站等等具有显著的效果。
FastDFS本身不支持自定义文件名,文件名是存储成功以后根据存储位置生成的一个file_id。很多应用场景不得不使用自定义文件名,在不修改其源码的情况下,可以在存储客户端fdfs_client增加一个用来存储自定义文件名和fastdfs的file_id之间的映射关系的数据库,间接实现自定义文件名的存取和访问,当然我们也可以选用reids。
二、整合 FastDFS_Client
基于tobato的fastdfs-client是一个功能完善的FastDFS客户端工具,它是在FastDFS作者YuQing发布的客户端基础上进行了大量的重构,提供了上传、下载、删除、生成缩略图等API。其源码在github上的链接
1.在项目Pom当中加入依赖
Maven依赖为
<!--FastDFS 客户端-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.2</version>
</dependency>
2.在application.yml当中配置Fdfs相关参数
spring:
servlet:
multipart:
# 默认最大上传文件大小为1M,单个文件大小
max-file-size: 10MB
# 默认最大请求大小为10M,总上传的数据大小
max-request-size: 50MB
# 默认支持文件上传
enabled: true
# 支持文件写入磁盘
file-size-threshold: 0
# 分布式文件系统FastDFS配置
fdfs:
# 读取时间
so-timeout: 1501
# 连接超时时间
connect-timeout: 601
# 缩略图生成参数
thumb-image:
width: 150
height: 150
# 前端访问url
webServerUrl: http://192.168.1.21:22120/
# Tracker服务,支持多个
tracker-list:
- 192.168.1.21:22122
3.将Fdfs配置引入项目
将FastDFS-Client客户端引入本地化项目的方式很是简单,在SpringBoot项目/src/[com.xxx.主目录]/conf
当中配置
/**
* 导入FastDFS-Client组件
*
* @author tobato
*/
@Configuration
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class ComponentImport {
// 导入依赖组件
}
4.文件工具类
基于FastFileStorageClient接口和springmvc提供的MultipartFile接口封装了一个简单的工具类,方便全局管理与调用。
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.MetaData;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;
/**
* FastDFS客户端包装类.
*
* @author arjun
* @date 2021/6/9
*/
@Slf4j
@Component
public class FastDfsClient {
@Autowired
private FastFileStorageClient storageClient;
@Autowired
private FdfsWebServer fdfsWebServer;
@Autowired
private ThumbImageConfig thumbImageConfig;
/**
* 文件上传.
*
* @param file MultipartFile文件对象
* @return 返回上传完成以后的文件路径
*/
public String uploadFile(MultipartFile file) throws IOException {
// 参1 文件输入流,参2 文件大小,参3 文件拓展名,参4 元数据集合
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()), null);
return storePath.getFullPath();
}
/**
* 上传文件.
*
* @param file File文件对象
* @return 文件访问地址
*/
public String uploadFile(File file) throws IOException {
FileInputStream inputStream =