本文主要介绍FastDFS的环境搭建以及一些常见的用法。
本文目录
-
-
- 1. FastDFS安装
- 2. 配置
- 3. 启动
- 4. FastDFS上传测试
- 5. 获取上传后的文件元数据
- 6. 下载和删除测试
-
1. FastDFS安装
依赖安装
# CentOS
yum install -y gcc gcc-c++ make libevent-devel pcre-devel zlib-devel openssl-devel
下载并编译 FastDFS
cd /usr/local/src
wget https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz
tar zxvf V6.06.tar.gz
cd fastdfs-6.06
# 编译并安装
./make.sh
./make.sh install
2. 配置
- Tracker Server 配置:
- 主要配置文件是 tracker.conf,一般位于 /etc/fdfs/ 目录下。
- 配置项如 base_path,指定 Tracker Server 存储数据和日志的基础路径。
- 还需配置端口号等信息,如 port 默认为 22122。
- Storage Server 配置:
- 主要配置文件是 storage.conf,也在 /etc/fdfs/ 目录下。
- 配置 base_path 用于存储数据和日志,store_path0 用于指定实际存储文件的路径。
- 配置 tracker_server,填写 Tracker Server 的 IP 地址和端口号,格式如 tracker_server=192.168.1.100:22122。
3. 启动
- 启动 Tracker Server:
- 执行命令 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start 启动 Tracker Server。
- 可以通过 ps -ef | grep fdfs_trackerd 查看 Tracker Server 进程是否启动成功。
- 启动 Storage Server:
- 执行命令 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start 启动 Storage Server。
- 同样可通过 ps -ef | grep fdfs_storaged 检查 Storage Server 进程状态。
4. FastDFS上传测试
- 使用命令行测试:
- 安装 fastdfs-utils 工具包,在 CentOS 上可使用 yum install fastdfs-utils 安装。
- 执行命令 fdfs_upload_file /etc/fdfs/client.conf test.jpg 进行文件上传测试,client.conf 是客户端配置文件,test.jpg 是要上传的文件。
import org.csource.common.SysException;
import org.csource.fastdfs.*;
import java.io.IOException;
public class FastDFSUploadTest {
public static void main(String[] args) {
try {
ClientGlobal.init("fastdfs_client.conf");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String[] uploadResults = storageClient.upload_file("test.jpg", "jpg", null);
if (uploadResults != null && uploadResults.length == 2) {
String groupName = uploadResults[0];
String remoteFileName = uploadResults[1];
System.out.println("文件上传成功,Group Name: " + groupName + ", 文件名: " + remoteFileName);
} else {
System.out.println("文件上传失败");
}
trackerServer.close();
} catch (IOException | SysException e) {
e.printStackTrace();
}
}
}
5. 获取上传后的文件元数据
FastDFS 会记录文件的一些元数据,如文件大小、创建时间、文件类型等。可以通过客户端 API 获取这些元数据信息。
import org.csource.common.SysException;
import org.csource.fastdfs.*;
import java.io.IOException;
public class FastDFSGetMetadata {
public static void main(String[] args) {
try {
ClientGlobal.init("fastdfs_client.conf");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
// 假设已经上传的文件 groupName 和 remoteFileName
String groupName = "group1";
String remoteFileName = "xxxx";
FileInfo fileInfo = storageClient.get_file_info(groupName, remoteFileName);
if (fileInfo != null) {
System.out.println("文件大小: " + fileInfo.getFileSize());
System.out.println("文件创建时间: " + fileInfo.getCreateTimestamp());
} else {
System.out.println("获取文件元数据失败");
}
trackerServer.close();
} catch (IOException | SysException e) {
e.printStackTrace();
}
}
}
6. 下载和删除测试
- 下载测试: 命令行下载:fdfs_download_file /etc/fdfs/client.conf group1 test.jpg,将 group1 中的 test.jpg 文件下载到本地。
import org.csource.common.SysException;
import org.csource.fastdfs.*;
import java.io.FileOutputStream;
import java.io.IOException;
public class FastDFSDownloadTest {
public static void main(String[] args) {
try {
ClientGlobal.init("fastdfs_client.conf");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String groupName = "group1";
String remoteFileName = "test.jpg";
byte[] fileBytes = storageClient.download_file(groupName, remoteFileName);
if (fileBytes != null) {
FileOutputStream fos = new FileOutputStream("local_test.jpg");
fos.write(fileBytes);
fos.close();
System.out.println("文件下载成功");
} else {
System.out.println("文件下载失败");
}
trackerServer.close();
} catch (IOException | SysException e) {
e.printStackTrace();
}
}
}
- 删除测试: 命令行删除:fdfs_delete_file /etc/fdfs/client.conf group1 test.jpg,删除 group1 中的 test.jpg 文件。
import org.csource.common.SysException;
import org.csource.fastdfs.*;
import java.io.IOException;
public class FastDFSDeleteTest {
public static void main(String[] args) {
try {
ClientGlobal.init("fastdfs_client.conf");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String groupName = "group1";
String remoteFileName = "test.jpg";
int result = storageClient.delete_file(groupName, remoteFileName);
if (result == 0) {
System.out.println("文件删除成功");
} else {
System.out.println("文件删除失败");
}
trackerServer.close();
} catch (IOException | SysException e) {
e.printStackTrace();
}
}
}
← 上一篇 AngularJS知识快速入门(上) |
记得点赞、关注、收藏哦! |
下一篇 Ajax——在OA系统提升性能的局部刷新 → |
评论前必须登录!
注册