第一章:常用命令
一、服务操作命令:
# tracker服务的启动,重启,停止
fdfs_trackerd /etc/fdfs/tracker.conf start/restart/stop
# storage服务的启动,重启,停止
fdfs_storaged /etc/fdfs/storage.conf start/restart/stop
二、测试命令:
fdfs_test & fdfs_test1
#operation(必填参数): upload(上传), download(下载), getmeta, setmeta, delete(删除) and query_servers
#config_file(必填参数): client配置文件
fdfs_test <config_file> <operation>
# 示例上传(返回文件服务器中所上传文件的路径)---http://127.0.0.1/group1/M00/00/00/ClhYzlyQnduAQKIQAAeckd7Z_4M999.jpg
fdfs_test /etc/fdfs/client.conf upload /opt/test.jpg
#示例下载
fdfs_test1 /etc/fdfs/client.conf download group1/M00/00/00/ClhYzlyQnm6AfHqCAAeckd7Z_4M842_big.jpg /opt/download.jpg
#示例删除
fdfs_test1 /etc/fdfs/client.conf delete group1/M00/00/00/ClhYzlyQnm6AfHqCAAeckd7Z_4M842_big.jpg
三、文件上传命令:
upload_file与upload_appender_file
前者上传的文件上传后不能修改,后者可以修改
#config_file(必填参数):client配置文件
#local_filename(必填参数):要上传的本地文件
#storage_ip:port(可选参数):存储的服务器节点
fdfs_upload_file <config_file> <local_filename> [storage_ip:port]
#示例(返回文件路径group1/M00/00/00/ClhYzlyQoJWADlM_AAeckd7Z_4M895.jpg)
fdfs_upload_file /etc/fdfs/client.conf /opt/test.jpg
四、文件下载命令
fdfs_download_file
#config_file(必填参数):client配置文件
#file_id(必填参数):例group1/M00/00/00/ClhYzlyQoJWADlM_AAeckd7Z_4M895.jpg
#local_filename(可选参数):下载到本地的文件
fdfs_download_file <config_file> <file_id> [local_filename]
#示例
fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/ClhYzlyQoJWADlM_AAeckd7Z_4M895.jpg /opt/download1.jpg
五、文件删除命令
fdfs_delete_file
#config_file(必填参数):client配置文件
#file_id(必填参数):例group1/M00/00/00/ClhYzlyQoJWADlM_AAeckd7Z_4M895.jpg
fdfs_delete_file <config_file> <file_id>
#示例
fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/ClhYzlyQoJWADlM_AAeckd7Z_4M895.jpg
六、查看文件信息
fdfs_file_info
#config_file(必填参数):client配置文件
#file_id(必填参数):例group1/M00/00/00/ClhYzlyQoJWADlM_AAeckd7Z_4M895.jpg
fdfs_file_info<config_file> <file_id>
#示例
fdfs_file_info/etc/fdfs/client.conf group1/M00/00/00/ClhYzlyQoJWADlM_AAeckd7Z_4M895.jpg
七、集群信息查看
fdfs_monitor
#查看storage是否注册到tracker server
fdfs_monitor /etc/fdfs/storage.conf
八、修改已存在文件
fdfs_append_file
#config_file(必填参数):client配置文件
#appender_file_id(必填参数):例group1/M00/00/00/ClhYzlyQoJWADlM_AAeckd7Z_4M895.jpg,必须是使用upload_appender_file方式上传的文件
#local_filename(必填参数):本地文件
fdfs_append_file <config_file> <appender_file_id> <local_filename>
第二章、JAVA操作FastDFS
一、添加依赖
该jar包我是在163的maven仓库里边下载的
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
二、添加配置文件
fastdfs.properties(自己命名)
fastdfs.tracker_servers=127.0.0.1:22122
三、封装公共方法
public class FastDFSClient {
private TrackerClient trackerClient;
private TrackerServer trackerServer;
private StorageServer storageServer;
private StorageClient1 storageClient;
public FastDFSClient() {
try {
Properties properties = PropertiesLoaderUtils.loadAllProperties("fastdfs.properties");
ClientGlobal.initByProperties(properties);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 上传文件方法
*
* @param fileName 文件全路径
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(String fileName, String extName) throws Exception {
return storageClient.upload_file1(fileName, extName, null);
}
/**
* 文件下载到磁盘
*
* @param path 文件路径
* @param output 输出流 中包含要输出到磁盘的路径
* @return -1失败,0成功
*/
public int download(String path, BufferedOutputStream output) throws Exception {
int result = -1;
byte[] b = storageClient.download_file1(path);
try {
if (b != null) {
output.write(b);
result = 0;
}
} catch (Exception e) {
throw e;
}
finally {
if (output != null)
try {
output.close();
} catch (IOException e) {
throw e;
}
}
return result;
}
/**
* @param storagePath 文件的全部路径 如:group1/M00/00/00/ClhYzlyQoJWADlM_AAeckd7Z_4M895.jpg
* @return -1失败,0成功
*/
public Integer deleteFile(String storagePath) {
int result = -1;
try {
result = storageClient.delete_file1(storagePath);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 获取远程服务器文件资源信息
* @param groupName 文件组名 如:group1
* @param remoteFileName M00/00/00/ClhYzlyQoJWADlM_AAeckd7Z_4M895.jpg
* @return
*/
public FileInfo getFile(String groupName,String remoteFileName){
try {
return storageClient.get_file_info(groupName, remoteFileName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
OK!可以实现你的业务了。。。