什么是FastDFS?
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。
相信大家既然是要整合FastDFS 那应该对它有个基础的认识吧
既然要整合FastDFS那就要安装FastDFS服务,安装FastDFS系统时会配置相应的端口,后续请求文件系统时会用到,具体配置大家百度如何安装配置FastDFS,这里就不介绍如何安装FastDFS服务了。
以下是Spring Boot2 整合FastDFS步骤
1.pom引入相关依赖包
<!--fstdfs client-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.5</version>
</dependency>
2.增加fastdfs相关配置
application.yml中增加属性
fdfs:
so-timeout: 1501
connect-timeout: 601
thumb-image: #缩略图生成参数
width: 200
height: 200
tracker-list: #TrackerList参数,支持多个
- 192.168.1.178:22122
3.注入FdfsClientConfig类
@Configuration
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class ComponetImport {
}
4.编写fastdfs工具类封装相关操作FastdfsClientUtil
package org.guajava.common.utils.fastdfs;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import org.tianshun.common.utils.StringUtils;
import java.io.IOException;
import java.io.InputStream;
/**
* Fastdfs工具类
*/
@Component
public class FastdfsClientUtil {
private final Logger logger = LoggerFactory.getLogger(FastdfsClientUtil.class);
@Autowired
private FastFileStorageClient storageClient;
@Autowired
private ThumbImageConfig thumbImageConfig;
//上传文件
public String upload(MultipartFile myfile) throws Exception{
//文件名
String originalFilename = myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf(".") + 1);
// 文件扩展名
String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());
StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(myfile.getInputStream(), myfile.getSize(),originalFilename , null);
String path = storePath.getFullPath();
return path;
}
/**
* 删除文件
* @Param fileUrl 文件访问地址
*/
public void deleteFile(String fileUrl) {
if (StringUtils.isEmpty(fileUrl)) {
return;
}
try {
StorePath storePath = StorePath.parseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (FdfsUnsupportStorePathException e) {
logger.warn(e.getMessage());
}
}
}
5.编写测试类,测试上传图片功能
@RunWith(SpringRunner.class)
//classes =后面的类是 SpringBoot 启动类的类名,需要更改成你实际SpringBoot启动类的类名
@SpringBootTest(classes = TestApplication.class)
public class FdfsTest {
@Autowired
private FastdfsClientUtil fastdfsClientUtil;
@Test
public void testUpload() throws FileNotFoundException {
File file = new File("D:\\123.jpg");
System.out.println(fastdfsClientUtil.upload(file));
}
}
可以看到后台会输出图片的路径
访问地址
http://192.168.111.130:8888/group1/M00/00/00/wKhvglu4_TqAadEtAABs63TOJBQ567.jpg
图片显示出来。