FastDFS搭建——坎坷之路
参考文档
https://blog.csdn.net/qq_37767455/article/details/91125714
https://note.clboy.cn/#/project/leyoumall/Introduction/FastDFS-install
https://blog.51cto.com/caibird/1704953
官方推荐参考文档:
https://github.com/happyfish100/fastdfs/wiki
我的环境
名称 | 说明 |
---|---|
centos | 8.x |
libfastcommon | FastDFS分离出的一些公用函数包 |
FastDFS | FastDFS本体 |
fastdfs-nginx-module | FastDFS和nginx的关联模块 |
nginx |
nginx1.15.4 |
需要的C++编译环境安装命令
//C语言编译 yum -y install gcc //压缩文件解压软件 yum install -y unzip zip //libevent工具包 yum -y install libevent
需要下载的软件(nginx选择在线安装,后边会提)
详细步骤按照官方推荐文档来就行
JAVA开发
Pom文件引入
<dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.1-RELEASE</version> </dependency>
yml配置
fdfs: so-timeout: 1501 connect-timeout: 601 thumb-image: # 缩略图 width: 60 height: 60 tracker-list: # tracker地址 - 192.168.119.128:22122
Java配置类
import com.github.tobato.fastdfs.FdfsClientConfig; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableMBeanExport; import org.springframework.context.annotation.Import; import org.springframework.jmx.support.RegistrationPolicy; @Configuration @Import(FdfsClientConfig.class) //解决jmx重复注册Bean的问题 @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) public class FastClientImporter { }
测试用例
import com.github.tobato.fastdfs.domain.StorePath; import com.github.tobato.fastdfs.domain.ThumbImageConfig; import com.github.tobato.fastdfs.service.FastFileStorageClient; import com.leyou.LeyouUploadApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @RunWith(SpringRunner.class) @SpringBootTest() public class FastDFSTest { @Autowired private FastFileStorageClient storageClient; @Autowired private ThumbImageConfig thumbImageConfig; @Test public void testUpload() throws FileNotFoundException { File file = new File("E:\\work_space\\uploadFile\\证件照.jpg"); // 上传并且生成缩略图,第一个参数是文件流,第二个参数是文件大小,第三个参数是文件后缀名,第四个参数是源数据信息,通常为null StorePath storePath = this.storageClient.uploadFile( new FileInputStream(file), file.length(), "jpg", null); // 带分组的路径 System.out.println(storePath.getFullPath()); // 不带分组的路径 System.out.println(storePath.getPath()); } }