天天看點

FastDFS應用

目錄

    • 一、常用類與API
      • 1.1、ClientGlobal
      • 1.2、TrackerClient
      • 1.3、TrackerServer
      • 1.4、StorageServer
      • 1.5、StorageClient
    • 二、示例操作
      • 2.1、引入pom
      • 2.2、配置
      • 2.3、單服務測試
      • 2.4、測試效果
        • 2.4.1、上傳
        • 2.4.2、下載下傳
        • 2.4.3、删除

一、常用類與API

1.1、ClientGlobal

用于加載配置檔案的公共用戶端工具;

常用方法:

init(String conf_filename)

根據配置檔案路徑及命名,加載配置檔案并設定用戶端公共 參數,配置檔案類型為 conf 檔案,可以使用絕對路徑或相對路徑加載;

initByProperties(Properties props)

根據 Properties 對象設定用戶端公共參數

1.2、TrackerClient

跟蹤器用戶端類型,建立此類型對象時,需傳遞跟蹤器組,就是跟蹤器的通路位址資訊;

建立對象的方式:

new TrackerClient()

new TrackerClient(ClientGlobal.g_tracker_group)

1.3、TrackerServer

跟蹤器服務類型,此類型的對象是通過跟蹤器用戶端對象建構的。實質上就是一個與 FastDFS Tracker Server 的連結對象,是代碼中與 Tracker Server 連結的工具;

建構對象的方式:

trackerClient.getConnection()

1.4、StorageServer

存儲服務類型,此類型的對象是通過跟蹤器用戶端對象建構的。實質上就是一個與 FastDFS Storage Server 的連結對象,是代碼中與 StroageServer 連結的工具;擷取的具體存儲 服務連結,是由 Tracker Server 配置設定的,是以建構存儲服務對象時,需要依賴跟蹤器服務對象;

建構對象的方式:

trackerClient.getStoreStorage(trackerServer)

1.5、StorageClient

存儲用戶端類型,建立時,需傳遞跟蹤服務對象 和存儲服務對象。此對象實質上就是一個通路 FastDFS Storage Server 的用戶端對象,用于實 現檔案的讀寫操作;

建立對象的方式:

new StorageClient(trackerServer, storageServer)

常用方法:

upload_file(String local_filename, String file_ext_name, NameValuePair[] meta_list)

:上傳檔案的方法,參數 local_filename 為要上傳的本地檔案路徑及檔案名,可使用絕對路徑或相對路徑;參數 file_ext_name 為上傳檔案的擴充名,如果傳遞 null,則自動解析檔案擴充名; 參數 meta_list 是用于設定上傳檔案的源資料的,如上傳使用者、上傳描述等;

download_file(String group_name, String remote_file_name)

:下 載檔案的方法 , 參數 group為組名/卷名 , 就是Storage Server中/etc/fdfs/storage.conf配置檔案中配置的 group_name參數值,也是要下載下傳的檔案所在組/卷的命名;參數 remote_file_name 為要下載下傳 的檔案的路徑及檔案名;

delete_file(String group_name, String remote_file_name)

:删除檔案的方法,參數含義同 download_file 方法參數;

二、示例操作

2.1、引入pom

<dependency>
			<groupId>cn.bestwu</groupId>
			<artifactId>fastdfs-client-java</artifactId>
			<version>1.27</version>
		</dependency>
           

2.2、配置

# 連接配接逾時時間(機關s)
connect_timeout = 10
# 網絡逾時時間(機關s)
network_timeout = 30
charset = UTF-8
# 必須與tracker server中的etc/fdfs/tracker.conf配置檔案中的http.server_port=8080配置一緻
http.tracker_http_port = 8080
# tracker server的IP域端口(如果有多個配置多行)
tracker_server = 192.168.48.128:22122
           

2.3、單服務測試

package com.sxt.fastdfs.singe;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.UUID;

import org.apache.commons.io.IOUtils;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.junit.Test;

public class TestSingle {

	private final String configFile = "src/test/resources/fdfs_client.conf";
	private final Properties props = new Properties();

	/**
	 * 根據相對路徑,直接加載配置檔案。
	 * 加載的是xxx.conf配置檔案。
	 * 預設的加載路徑是相對位址。是相對于項目的根開始尋址的。不是相對于classpath開始尋址的。
	 */
	private void init() throws IOException, MyException{
		ClientGlobal.init(configFile);
	}

	/**
	 * 根據Properties,加載配置檔案。
	 * 加載properties配置檔案,生成一個Properties對象。再實作環境的初始化。
	 */
	private void initByProperties() throws IOException, MyException{
		InputStream in = TestSingle.class.getClassLoader().getResourceAsStream("fdfs_client.properties");
		props.load(in);
		ClientGlobal.initByProperties(props);
	}

	/**
	 * 測試上傳
	 */
	@Test
	public void testUpload(){
		try{
			this.init(); // 使用conf配置檔案初始化環境。就是加載連結逾時,網絡逾時,tracker伺服器清單等。
			// 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);
			// 要上傳的檔案的中繼資料。可以自定義。
			NameValuePair[] nvp = null;
	        /*
	        NameValuePair nvp [] = new NameValuePair[]{
	                new NameValuePair("filename", "xxxxx"),
	                new NameValuePair("filelength", "xxxx")
	        };
	        */
			// 檔案上傳工作。參數分别是: (要上傳的本地檔案,上傳的檔案的類型,上傳的檔案的中繼資料)
			// 檔案類型可以不傳遞,也就是傳遞一個null。FastDFS可以自動的在檔案名中截取檔案類型。
			// 傳回的結果是一個字元串數組。長度為2。0位置是卷名-group1,1位置是檔案名-M00/00/00/xxxxxx
			String fileIds[] = storageClient.upload_file("testFile/1.jpg", "jpg", nvp);

			System.out.println(fileIds.length);
			System.out.println("組名/卷名:" + fileIds[0]);
			System.out.println("路徑: " + fileIds[1]);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 使用StorageClient1子類型測試檔案上傳
	 */
	@Test
	public void testUploadWithStorageClient1(){
		try{
			// this.init();
			this.initByProperties();
			
			TrackerClient trackerClient = new TrackerClient();
			TrackerServer trackerServer = trackerClient.getConnection();
			StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
			
			StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
			
			NameValuePair[] nvp = null;
			
			InputStream in = new FileInputStream("testFile/2.jpg");
			byte[] buff = null;
			if(in != null){
				buff = new byte[in.available()];
				in.read(buff);
			}
			// 參數分别是檔案的位元組數組,檔案的類型,檔案的中繼資料
			// 傳回結果是卷名/檔案名
			String fileId = storageClient1.upload_file1(buff, "jpg", nvp);
			
			System.out.println(fileId);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	

	/**
	 * 測試下載下傳
	 */
	@Test
	public void testDownload(){
		try{
			this.init();

			TrackerClient trackerClient = new TrackerClient();
			TrackerServer trackerServer = trackerClient.getConnection();
			StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);

			StorageClient storageClient = new StorageClient(trackerServer, storageServer);
			// 下載下傳方法參數為卷名和檔案名。傳回值是要下載下傳的檔案的位元組數組。
			byte[] buff = storageClient.download_file("group1", "M00/00/00/wKgwgV8xae-AeDAIAADW-MWxcrw656.jpg");

			IOUtils.write(buff, new FileOutputStream("testFile/"+UUID.randomUUID().toString()+".jpg"));
			System.out.println("下載下傳完成");
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * 測試删除
	 */
	@Test
	public void testDelete(){
		try{
			this.init();

			TrackerClient trackerClient = new TrackerClient();
			TrackerServer trackerServer = trackerClient.getConnection();
			StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);

			StorageClient storageClient = new StorageClient(trackerServer, storageServer);

			// 0為成功删除
			// 兩個參數分别是卷名和檔案名。
			int flag = storageClient.delete_file("group1", "M00/00/00/wKgCbltToWWAK4FiAADW-MWxcrw927.jpg");
			System.out.println(flag == 0 ? "删除成功" : "删除失敗");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
}
           

2.4、測試效果

2.4.1、上傳

輸出日志

2
組名/卷名:group1
路徑: M00/00/00/wKgwgV8xae-AeDAIAADW-MWxcrw656.jpg
           

存儲路徑

[[email protected] 00]# ll
總用量 56
-rw-r--r--. 1 root root 55032 8月  10 15:38 wKgwgV8xae-AeDAIAADW-MWxcrw656.jpg
[[email protected] 00]# pwd
/fastdfs/storage/store/data/00/00
           

2.4.2、下載下傳

下載下傳完成
           

2.4.3、删除

删除成功
           

繼續閱讀