FastDFS的配置、部署與API使用解讀(2)以位元組方式上傳檔案的客戶端程式碼
阿新 • • 發佈:2019-01-10
本文來自 詩商·柳驚鴻 Poechant CSDN部落格,轉載請註明源地址:FastDFS的配置、部署與API使用解讀(2)上傳檔案到FastDFS分散式檔案系統的客戶端程式碼
在閱讀本文之前,請您先通過《FastDFS的配置、部署與API使用解讀(1)Get Started with FastDFS》一文中給出的參考博文中的部署篇和測試篇來完成前期的準備工作。
1、下載FastDFS的API
FastDFS提供Java和PHP等語言的客戶端API。可以到FastDFS在Google Code的專案主頁 http://code.google.com/p/fastdfs/downloads/list 下載。本文以Java API為例。
2、呼叫API的上傳介面
通過Servlet得到InputStream、檔名稱和檔案長度,然後通過呼叫FastDFS提供的Java API把檔案上傳到FastDFS伺服器。下段程式碼中的getFileBuffer可參考本部落格上一篇博文。(by Poechant)
3、呼叫方式詳解
(1)客戶端與Tracker Server通訊
根據《FastDFS的配置、部署與API使用解讀(1)Get Started with FastDFS》一文中提供的FastDFS的工作原理,結合上面的程式碼,首先通過TrackerClient建構函式從全域性配置中獲取Tracker Servers的IP和埠初始化一個TrackerClient物件tracker,並與其建立連線,我們可以從API的原始碼中看到:
上述程式碼中ClientGlobal是一個提供很多靜態成員供外部讀取的類。通過tracker這個TrackerClient建立的與Tracker Server的連線,例項化了一個trackerServer物件。
(3)呼叫檔案操作API
這些操作包括upload、download、append、delete等。上例中提供的是上傳的例項。
在閱讀本文之前,請您先通過《FastDFS的配置、部署與API使用解讀(1)Get Started with FastDFS》一文中給出的參考博文中的部署篇和測試篇來完成前期的準備工作。
1、下載FastDFS的API
FastDFS提供Java和PHP等語言的客戶端API。可以到FastDFS在Google Code的專案主頁 http://code.google.com/p/fastdfs/downloads/list 下載。本文以Java API為例。
2、呼叫API的上傳介面
通過Servlet得到InputStream、檔名稱和檔案長度,然後通過呼叫FastDFS提供的Java API把檔案上傳到FastDFS伺服器。下段程式碼中的getFileBuffer可參考本部落格上一篇博文。(by Poechant)
/** * Upload File to DFS. * @param fileBuff, file to be uploaded. * @param uploadFileName, the name of the file. * @param fileLength, the length of the file. * @return the file ID in DFS. * @throws IOException */ public String uploadFile(InputStream inStream, String uploadFileName, long fileLength) throws IOException { byte[] fileBuff = getFileBuffer(inStream, fileLength); String fileId = ""; String fileExtName = ""; if (uploadFileName.contains(".")) { fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1); } else { logger.warn("Fail to upload file, because the format of filename is illegal."); return fileId; } //建立連線 TrackerClient tracker = new TrackerClient(); TrackerServer trackerServer = tracker.getConnection(); StorageServer storageServer = null; StorageClient1 client = new StorageClient1(trackerServer, storageServer); //設定元資訊 NameValuePair[] metaList = new NameValuePair[3]; metaList[0] = new NameValuePair("fileName", uploadFileName); metaList[1] = new NameValuePair("fileExtName", fileExtName); metaList[2] = new NameValuePair("fileLength", String.valueOf(fileLength)); //上傳檔案 try { fileId = client.upload_file1(fileBuff, fileExtName, metaList); } catch (Exception e) { logger.warn("Upload file \"" + uploadFileName + "\"fails"); } trackerServer.close(); return fileId; }
3、呼叫方式詳解
(1)客戶端與Tracker Server通訊
根據《FastDFS的配置、部署與API使用解讀(1)Get Started with FastDFS》一文中提供的FastDFS的工作原理,結合上面的程式碼,首先通過TrackerClient建構函式從全域性配置中獲取Tracker Servers的IP和埠初始化一個TrackerClient物件tracker,並與其建立連線,我們可以從API的原始碼中看到:
/** * constructor with global tracker group */ public TrackerClient() { this.tracker_group = ClientGlobal.g_tracker_group; } /** * constructor with specified tracker group * @param tracker_group the tracker group object */ public TrackerClient(TrackerGroup tracker_group) { this.tracker_group = tracker_group; }
上述程式碼中ClientGlobal是一個提供很多靜態成員供外部讀取的類。通過tracker這個TrackerClient建立的與Tracker Server的連線,例項化了一個trackerServer物件。
(2)客戶端與Storage Server通訊
通過trackerServer取得某一個可用的Storage Server的地址並用其例項化一個StorageClient1物件。這樣就完成了FastDFS的客戶端呼叫上傳、下載、刪除等所有操作的前期建立連線的工作。(3)呼叫檔案操作API
這些操作包括upload、download、append、delete等。上例中提供的是上傳的例項。
-