FastDFS介绍
FastDFS是用c语言编写的一款开源的分布式文件系统,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合中小文件(建议范围:4KB < file_size <500MB
),对以文件为载体的在线服务,如相册网站、视频网站等。
使用
1.编辑pom.xml
<!-- Fastdfs -->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.6</version>
2.在配置文件application.properties中添加参数
# ===================================
# 分布式文件系统FDFS配置
# ===================================
# 连接超时时间
fdfs.connect-timeout=600
# 读取超时时间
fdfs.so-timeout=1500
# 缩略图的宽高
fdfs.thumb-image.height=150
fdfs.thumb-image.width=150
# tracker服务配置地址列表,替换成自己服务的IP地址,支持多个
fdfs.tracker-list=10.150.132.93:22122
#从池中借出的对象的最大数目(配置为-1表示不限制)
fdfs.pool.max-total=-1
#获取连接时的最大等待毫秒数(默认配置为5秒)
fdfs.pool.max-wait-millis=5000
#每个key最大连接数
fdfs.pool.max-total-per-key=50
#每个key对应的连接池最大空闲连接数
fdfs.pool.max-idle-per-key=10
#每个key对应的连接池最小空闲连接数
fdfs.pool.max_idle_per_key=5
#=====================================
3.配置管理
//FastDFS-Client 1.26.4版本以前引入方式
@Configuration
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastdfsConfig {
}
//我用的是1.26.6版本, 没有上面引入也可以使用
4.测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class FastdfsUtilsTest {
@Autowired
protected FastFileStorageClient fastFileStorageClient;
@Test
public void testFdfs() throws FileNotFoundException {
File file1 = new File("D:\\testzip\\Desktop.zip");
FileInputStream file = new FileInputStream(file1);
// 上传文件和Metadata
StorePath path = fastFileStorageClient.uploadFile(file, file1.length(), FilenameUtils.getExtension(file1.getName()),
null);
assertNotNull(path);
System.out.println("+++++" + path.getFullPath());
fastFileStorageClient.deleteFile(path.getFullPath());
}
}
结果:
+++++group1/M00/00/13/CpaEXV3KMb6AdHHQAAFOKD6OmBo557.zip
当然,你可以自己封装一下对应的函数.
下面是一个范例:
@Component
public class FastdfsUtils {
public static final String DEFAULT_CHARSET = "UTF-8";
@Autowired
private FastFileStorageClient fastFileStorageClient;
/**
* 上传
* @param file
* @return
* @throws IOException
*/
public StorePath upload(MultipartFile file) throws IOException {
// 设置文件信息
Set<MetaData> mataData = new HashSet<>();
mataData.add(new MetaData("author", "fastdfs"));
mataData.add(new MetaData("description",file.getOriginalFilename()));
// 上传
StorePath storePath = fastFileStorageClient.uploadFile(
file.getInputStream(), file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()),
null);
return storePath;
}
/**
* 删除
* @param path
*/
public void delete(String path) {
fastFileStorageClient.deleteFile(path);
}
/**
* 删除
* @param group
* @param path
*/
public void delete(String group,String path) {
fastFileStorageClient.deleteFile(group,path);
}
/**
* 文件下载
* @param path 文件路径,例如:/group1/path=M00/00/00/itstyle.png
* @param filename 下载的文件命名
* @return
*/
public void download(String path, String filename, HttpServletResponse response) throws IOException {
// 获取文件
StorePath storePath = StorePath.parseFromUrl(path);
if (StringUtils.isBlank(filename)) {
filename = FilenameUtils.getName(storePath.getPath());
}
byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
response.reset();
response.setContentType("applicatoin/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
ServletOutputStream out = response.getOutputStream();
out.write(bytes);
out.close();
}
}
参考网址:
https://github.com/tobato/FastDFS_Client
3.