二、fastDFS入門
2.1fastDFS安裝
tracker和storage使用相同的安裝包,fastDFS的下載地址在:https://github.com/happyfish100/FastDFS,FastDFS是C語言開發,建議在linux上運行。安裝細節本篇博文就不做贅述了,感興趣的朋友可以評論區留言我會單獨寫一篇博文來介紹fastDFS在Linux中的安裝和配置。
2.2文件上傳下載測試
2.2.1搭建環境
這裏我們使用javaApi測試文件的上傳,java版本的fastdfs-client地址在:https://github.com/happyfish100/fastdfs-client-java,參考此工程編寫測試用例。
此工程採用SpringBoot創建,導入資料下邊的test-fastdfs.zip,下邊是一些配置文件介紹:
1)pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>xc-framework-parent</artifactId>
<groupId>com.fastDFS</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../xc-framework-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-fastdfs</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java -->
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</project>
2) 配置文件
在classpath:config下創建fastdfs-client.properties文件
fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.tracker_servers = 192.168.101.65:22122
fastdfs.connect_timeout_in_seconds: #http連接超時時間
fastdfs.network_timeout_in_seconds: #tracker與storage網絡通信超時時間
fastdfs.charset:#字符編碼
fastdfs.tracker_servers:tracker#服務器地址,多個地址中間用英文逗號分隔
2.2.2 文件上傳
//上傳文件
@Test
public void testUpload() {
try {
ClientGlobal.initByProperties("config/fastdfs-client.properties");
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
System.out.println("charset=" + ClientGlobal.g_charset);
//創建客戶端
TrackerClient tc = new TrackerClient();
//連接tracker Server
TrackerServer ts = tc.getConnection();
if (ts == null) {
System.out.println("getConnection return null");
return;
}
//獲取一個storage server
StorageServer ss = tc.getStoreStorage(ts);
//創建一個storage存儲客戶端
StorageClient1 sc1 = new StorageClient1(ts, ss);
//本地文件路徑
String item = "C:\\Users\\admin\\Desktop\\1.png";
String fileid;
fileid = sc1.upload_file1(item, "png", null);
System.out.println("Upload local file " + item + " ok, fileid=" + fileid);
} catch (Exception ex) {
ex.printStackTrace();
}
}
2.2.3 文件查詢
//查詢文件
@Test
public void testQueryFile() throws IOException, MyException {
ClientGlobal.initByProperties("config/fastdfs-client.properties");
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer,
storageServer);
FileInfo fileInfo = storageClient.query_file_info("group1", "M00/00/01/wKhlQFrKBSOAW5AWAALcAg10vf4862.png");
System.out.println(fileInfo);
}
2.2.4 文件下載
//下載文件
@Test
public void testDownloadFile() throws IOException, MyException {
ClientGlobal.initByProperties("config/fastdfs-client.properties");
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient1 storageClient1 = new StorageClient1(trackerServer,
storageServer);
byte[] result = storageClient1.download_file1("group1/M00/00/01/wKhlQFrKBSOAW5AWAALcAg10vf4862.png");
File file = new File("d:/1.png");
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(result);
fileOutputStream.close();
}