Java 客户端 API 简单应用
1 依赖
<dependency>
<groupId>cn.bestwu</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
2 创建客户端
2.1 根据 conf 文件创建客户端
2.1.1conf 配置文件
# 连接超时, 单位是秒
connect_timeout = 10
# 网络超时, 单位是秒
network_timeout = 30
# 字符集设置
charset = UTF-8
# 必须和tracker server中的/etc/fdfs/tracker.conf配置文件中的http.server_port=8080配置一致
http.tracker_http_port = 8080
# tracker server 的访问 IP 和 Port。
tracker_server = 192.168.199.224:22122
2.1.2 代码
package singe;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.junit.Test;
import java.io.IOException;
public class TestInitClientByConf {
// 配置文件所在位置。
private final String configFile = "src/main/resources/test/fdfs_client.conf";
/**
* 根据相对路径,直接加载配置文件。
* 加载的是 xxx.conf 配置文件。
* 默认的加载路径是相对地址。是相对于项目的根开始寻址的。不是相对于 classpath
开始寻址的。
*/
@Test
public void initClient() throws IOException, MyException {
// 使用 conf 配置文件初始化环境。MyException 是 FastDFS 定义的自定义异常,其
处理的就是加载链接超时,网络超时,tracker 服务器列表加载异常等。
ClientGlobal.init(configFile);
// this.initByProperties();
TrackerClient trackerClient = new TrackerClient(); // new
TrackerClient(TrackerGlobal.g_tracker_group);
// 创建 tracker 服务器的链接对象
TrackerServer trackerServer = trackerClient.getConnection();
// 创建 storage 服务器的链接对象
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
// 创建 storage 服务器的客户端操作对象。可以实现文件的读写操作。
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
System.out.println("TrackerClient : " + trackerClient);
System.out.println("TrackerServer : " + trackerServer);
System.out.println("StorageServer : " + storageServer);
System.out.println("StorageClient : " + storageClient);
}
}
2.2 根据 properties 文件创建客户端
2.2.1properties 配置文件
# 连接超时, 单位是秒
fastdfs.connect_timeout_in_seconds=10
# 网络超时, 单位是秒
fastdfs.network_timeout_in_seconds=30
# 字符集设置
fastdfs.charset=UTF-8
# 必须和tracker server中的/etc/fdfs/tracker.conf配置文件中的http.server_port=8080配置一致
fastdfs.http_tracker_http_port=8080
# tracker server 的访问 IP 和 Port。
fastdfs.tracker_servers=192.168.89.152:22122
2.2.2 代码
package singe;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class TestInitClientByProperties {
private final Properties props = new Properties();
/**
* 根据 Properties,加载配置文件。
* 加载 properties 配置文件,生成一个 Properties 对象。再实现环境的初始化。
*/
@Test
public void initClient() throws IOException, MyException {
// 使用 Properties 配置文件初始化环境。
InputStream in = TestSingle.class.getClassLoader().getResourceAsStream("test/fdfs_client.properties");
props.load(in);
ClientGlobal.initByProperties(props);
// this.initByProperties();
TrackerClient trackerClient = new TrackerClient(); // new
TrackerClient(TrackerGlobal.g_tracker_group);
// 创建 tracker 服务器的链接对象
TrackerServer trackerServer = trackerClient.getConnection();
// 创建 storage 服务器的链接对象
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
// 创建 storage 服务器的客户端操作对象。可以实现文件的读写操作。
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
System.out.println("TrackerClient : " + trackerClient);
System.out.println("TrackerServer : " + trackerServer);
System.out.println("StorageServer : " + storageServer);
System.out.println("StorageClient : " + storageClient);
}
}
3 定义 FastDFS 工具类
package singe;
import org.csource.fastdfs.*;
public class FastDFSClientUtils {
// 配置文件所在位置。
private static final String configFile = "src/main/resources/test/fdfs_client.conf";
private static StorageClient storageClient;
static{
try {
// 使用 conf 配置文件初始化环境。就是加载链接超时,网络超时,tracker 服
务器列表等。
ClientGlobal.init(configFile);
// this.initByProperties();
TrackerClient trackerClient = new TrackerClient(); // new
TrackerClient(TrackerGlobal.g_tracker_group);
// 创建 tracker 服务器的链接对象
TrackerServer trackerServer = trackerClient.getConnection();
// 创建 storage 服务器的链接对象
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
// 创建 storage 服务器的客户端操作对象。可以实现文件的读写操作。
storageClient = new StorageClient(trackerServer, storageServer);
}catch(Exception e){
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
public static StorageClient getStorageClient(){
return storageClient;
}
public static void main(String[] args) {
System.out.println(getStorageClient());
}
}
4 文件上传
4.1 代码
package singe;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.StorageClient;
import org.junit.Test;
public class TestFileUpload {
@Test
public void test() {
String fileIds[] = testUpload("testFile", "1.jpg", "jpg");
if(fileIds == null){
System.out.println("文件上传失败!");
}
System.out.println(fileIds.length);
System.out.println("组名/卷名:" + fileIds[0]);
System.out.println("路径: " + fileIds[1]);
}
public String[] testUpload(String dirName, String fileName, String fileType){
StorageClient storageClient = FastDFSClientUtils.getStorageClient();
try{
// 要上传的文件的元数据。可以自定义。
NameValuePair nvp [] = new NameValuePair[]{
new NameValuePair("filename", fileName)
};
/*
* String[] upload_file(String fileName, String fileExtName, NameValuePair[] metaList)
* fileName - 要上传的本地文件,包含路径地址。
* fileExtName - 文件后缀名;可以不传递,也就是传递一个 null。FastDFS可以自动的在文件名中截取文件类型。
* metaList - 文件的描述元数据。
* 返回的结果是一个字符串数组。长度为 2。0 位置是卷名-group1,1 位置是文件名-M00/00/00/xxxxxx
*/
String fileIds[] = storageClient.upload_file(dirName+"/"+fileName, fileType, nvp);
return fileIds;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
4.2 结果
5 文件下载
5.1 代码
package singe;
import org.apache.commons.io.IOUtils;
import org.csource.fastdfs.StorageClient;
import org.junit.Test;
import java.io.FileOutputStream;
import java.util.UUID;
public class TestFileDownload {
@Test
public void test(){
testDownload("group1","M00/00/00/wKhZmV2j38aATN0PAADW-MWxcrw140.jpg");
}
public void testDownload(String groupName, String remoteFileName){
StorageClient storageClient = FastDFSClientUtils.getStorageClient();
try{
/*
* byte[] download_file(String groupName, String remoteFileName)
* groupName - 要下载的文件的卷名
* remoteFileName - 要下载的文件的远程文件名
* 返回值是要下载的文件的字节数组。
*/
byte[] buff = storageClient.download_file(groupName, remoteFileName);
/*
* NameValuePair[] get_metadata(String groupName, String remoteFileName)
* groupName - 要查找元数据的文件的卷名
* remoteFileName - 要查找元数据的文件的远程文件名
* 返回值是要查找元数据的文件的元数据数组。
*/
NameValuePair[] metaList = storageClient.get_metadata(groupName,remoteFileName);
String fileName = null;
for(int i = 0; i < metaList.length; i++){
NameValuePair nvp = metaList[i];
if(nvp.getName().equals("fileName"))
fileName = nvp.getValue();
}
IOUtils.write(buff, new FileOutputStream("downloadFile/"+ fileName));
System.out.println("下载完成");
}catch(Exception e){
e.printStackTrace();
}
}
}
5.2 结果
6 删除文件
6.1 代码
package singe;
import org.csource.fastdfs.StorageClient;
import org.junit.Test;
public class TestFileDelete {
@Test
public void test(){
testDelete("group1", "M00/00/00/wKhZmV2j38aATN0PAADW-MWxcrw140.jpg");
}
public void testDelete(String groupName, String remoteFileName){
StorageClient storageClient = FastDFSClientUtils.getStorageClient();
try{
/*
* int delete_file(String groupName, String remoteFileName)
* groupName - 要删除的文件卷名
* remoteFileName - 要删除的文件的远程文件名
* 返回 0 为成功删除, 其他为删除失败。
*/
int flag = storageClient.delete_file(groupName, remoteFileName);
System.out.println(flag == 0 ? "删除成功" : "删除失败");
}catch(Exception e){
e.printStackTrace();
}
}
}
6.2