前言
FastDFS是用c語言編寫的一款開源的分佈式文件系統。FastDFS爲互聯網量身定製,充分考慮了冗餘備份、負載均衡、線性擴容等機制,並注重高可用、高性能等指標,使用FastDFS很容易搭建一套高性能的文件服務器集羣提供文件上傳、下載等服務。
java
引入jar包
須要使用fastdfs_client_v1.20.jar,點擊下載,並將此jar加入到工程中。編程
使用步驟
public void upload() throws Exception{ //建立一個配置文件內容爲tracker服務器的地址 //使用全局對象來加載配置文件 ClientGlobal.init("全局配置文件的全路徑,該配置文件內容爲<tracker_server=FastDFS服務器ip:端口號>"); //建立一個trackerClient對象 TrackerClient trackerClient = new TrackerClient(); //經過trackerClient得到一個trackerServer對象 TrackerServer trackerServer = trackerClient.getConnection(); //建立一個strorageServer的引用,能夠爲null StorageServer storageServer = null; //建立一個storageClient,參數須要TrackerServer和StrorageServer StorageClient storageClient = new StorageClient(trackerServer,storageServer); //使用StorageClient上傳文件 String[] strings = storageClient.upload_file("待上傳圖片全路徑", "圖片格式,如jpg,不須要加.", null); for(String string : strings) { System.out.println(string); } }
執行此方法後,控制檯打印上傳後圖片的地址以及名稱,以下:數組
文件索引信息包括服務器
(1)組名:文件上傳後所在的storage組名稱,在文件上傳成功後有storage服務器返回,須要客戶端自行保存。負載均衡
(2)虛擬磁盤路徑:storage配置的虛擬路徑,與磁盤選項store_path*對應。若是配置了store_path0則是M00,若是配置了store_path1則是M01,以此類推。分佈式
(3)數據兩級目錄:storage服務器在每一個虛擬磁盤路徑下建立的兩級目錄,用於存儲數據文件。工具
(4)文件名:與文件上傳時不一樣。是由存儲服務器根據特定信息生成,文件名包含:源存儲服務器IP地址、文件建立時間戳、文件大小、隨機數和文件拓展名等信息。性能
上傳成功後,訪問http://FastDFS文件服務器ip/該全路徑便可。this
使用工具類
FastDFSClient.java
package cn.itcast.fastdfs.cliennt;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
public class FastDFSClient {
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null;
public FastDFSClient(String conf) throws Exception {
if (conf.contains("classpath:")) {
conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
}
/**
* 上傳文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileName 文件全路徑
* @param extName 文件擴展名,不包含(.)
* @param metas 文件擴展信息
* @return
* @throws Exception
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
}
public String uploadFile(String fileName) throws Exception {
return uploadFile(fileName, null, null);
}
public String uploadFile(String fileName, String extName) throws Exception {
return uploadFile(fileName, extName, null);
}
/**
* 上傳文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileContent 文件的內容,字節數組
* @param extName 文件擴展名
* @param metas 文件擴展信息
* @return
* @throws Exception
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileContent, extName, metas);
return result;
}
public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
}
public String uploadFile(byte[] fileContent, String extName) throws Exception {
return uploadFile(fileContent, extName, null);
}
}
客戶端使用
public void fastDfsClient() throws Exception{ FastDFSClient fastDFSClient = new FastDFSClient("配置文件地址,可用classpath相對路徑,工具類提供轉換"); String string = fastDFSClient.uploadFile("待上傳圖片全路徑"); System.out.println(string); }
總結
工具類的使用能極大的方便咱們的編程,工具類的思想也就是面向對象的特性之——封裝。
url