1、配置内容:
## fastdfs-client.properties
fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key = FastDFS1234567890
fastdfs.http_tracker_http_port = 8080
fastdfs.tracker_servers = 192.168.18.130:22122
2、Java内容:
package com.jd;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.junit.jupiter.api.Test;
class Tests {
//上传文件
@Test
public void upload() {
try {
//加载FastDFS客户端配置文件
ClientGlobal.initByProperties("fastdfs-client.properties");
//创建tracker客户端
TrackerClient trackerClient = new TrackerClient();
//建立与Tracker服务器的连接
TrackerServer trackerServer = trackerClient.getConnection();
//建立与Storage服务器的连接
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
//获取storage客户端
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//定义文件元数据
NameValuePair[] metaList = new NameValuePair[2];
metaList[0] = new NameValuePair("id", "bbc3dc68-4f25-450c-8022-63d2c0dfd5e0");
metaList[1] = new NameValuePair("描述", "这是一张适用于Windows操作系统的桌面背景图片。");
String path = "C:/Windows/Web/Screen/img100.jpg";
String [] fileInfoes = storageClient.upload_file(path, "jpg", metaList);
System.out.println("groupName:"+fileInfoes[0]);//group1
System.out.println("remoteFileName:"+fileInfoes[1]);//M00/00/00/wKgSjGBpOU-AKLKOAAsZd_e-IiM892.jpg
storageServer.close();
trackerServer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//查询文件
@Test
public void query() {
try {
//加载FastDFS客户端配置文件
ClientGlobal.initByProperties("fastdfs-client.properties");
//创建tracker客户端
TrackerClient trackerClient = new TrackerClient();
//建立与Tracker服务器的连接
TrackerServer trackerServer = trackerClient.getConnection();
//建立与Storage服务器的连接
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
//获取storage客户端
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
NameValuePair[] nvps = storageClient.get_metadata("group1", "M00/00/00/wKgSjGBpOU-AKLKOAAsZd_e-IiM892.jpg");
for (NameValuePair nvp : nvps) {
System.out.println(nvp.getName() + ":" + nvp.getValue());
}
FileInfo fileInfo = storageClient.query_file_info("group1", "M00/00/00/wKgSjGBpOU-AKLKOAAsZd_e-IiM892.jpg");
System.out.println(fileInfo);
storageServer.close();
trackerServer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//下载文件
@Test
public void download() {
try {
//加载FastDFS客户端配置文件
ClientGlobal.initByProperties("fastdfs-client.properties");
//创建tracker客户端
TrackerClient trackerClient = new TrackerClient();
//建立与Tracker服务器的连接
TrackerServer trackerServer = trackerClient.getConnection();
//建立与Storage服务器的连接
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
//获取storage客户端
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
int result = storageClient.download_file("group1","M00/00/00/wKgSjGBpOU-AKLKOAAsZd_e-IiM892.jpg","D:/1.png");
if (result == 0) {
System.out.println("下载成功");
} else {
System.out.println("下载失败");
}
storageServer.close();
trackerServer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//删除文件
@Test
public void delete() {
try {
//加载FastDFS客户端配置文件
ClientGlobal.initByProperties("fastdfs-client.properties");
//创建tracker客户端
TrackerClient trackerClient = new TrackerClient();
//建立与Tracker服务器的连接
TrackerServer trackerServer = trackerClient.getConnection();
//建立与Storage服务器的连接
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
//获取storage客户端
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
Integer result = storageClient.delete_file("group1", "M00/00/00/wKgSjGBpOU-AKLKOAAsZd_e-IiM892.jpg");
if (result == 0) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
storageServer.close();
trackerServer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}