同其他组件类似,有两种使用FastDFS的方法,一种是使用命令行的方式,另一种是通过API方式。
1.命令行
● 启动FastDFS的tracker服务
fdfs_trackerd /etc/fdfs/tracker.conf
● 重启tracker
fdfs_trackerd /etc/fdfs/tracker.conf restart
● 关闭tracker
fdfs_trackerd /etc/fdfs/tracker.conf stop
● 启动FastDFS的storage服务
fdfs_storaged /etc/fdfs/storage.conf
● 重启storage
fdfs_storaged /etc/fdfs/storage.conf restart
● 关闭storage
fdfs_storaged /etc/fdfs/storage.conf stop
或者可以采用kill命令关闭fastdfs,但不建议在线上使用 kill -9 强制关闭,因为可能会导致文件信息不同步问题
● 查看启动进程
ps –ef|grep fdfs
● 查看storage信息
fdfs_monitor /etc/fdfs/storage.conf
● 文件上传
fdfs_test /etc/fdfs/client.conf upload /root/aa.txt
执行后信息如下:
上传后的文件存储路径为URL中group名后的部分。
FastDFS生成的文件目录结构及名称示例:
其中M00对应storage配置中的store_path0,如果配置了store_path1(可能是另外一个磁盘,因为盘符可以不一致),那可能为M01。
● 文件删除
fdfs_delete_file /etc/fdfs/client.conf group1/要删除的文件路径
2.API方式
这里选择Springboot整合FastDFS的方案。
首先创建工程,这里不再赘述。
● 第一步:导入依赖
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.25.2-RELEASE</version>
</dependency>
● 第二步:导入fastdfs配置类
其中@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)注解是为了解决JMX重复注册bean的问题。
● 第三步:修改配置文件
#fdfs相关配置
#上传的超时时间
fdfs.so-timeout=1501
#连接超时时间
fdfs.connect-timeout=601
fdfs.thumb-image.width=150
fdfs.thumb-image.height=150
fdfs.tracker-list=192.168.2.232:22122
## 连接池最大数量
fdfs.pool.max-total=200
## 每个tracker地址的最大连接数
fdfs.pool.max-total-per-key=50
## 连接耗尽时等待获取连接的最大毫秒数
fdfs.pool.max-wait-millis=5000
● 第四步:使用客户端操作类操作FastDFS
@Autowired
private FastFileStorageClient fastFileStorageClient;
@Test
void contextLoads() throws FileNotFoundException {
File file = new File("D:\\picture\\1.png");
String fileName = file.getName();
String extName = fileName.substring(fileName.lastIndexOf(".")+1);
FileInputStream inputStream = new FileInputStream(file);
StorePath storePath = fastFileStorageClient.uploadFile(inputStream,file.length(),extName,null);
System.out.println(storePath.getFullPath());
}
至此完成FastDFS的上传文件操作。