一、背景
上篇部落格我介紹了FastDFS的概念、原理以及安裝步驟,這篇文章我們來聊一聊如何在java中使用FastDFSClient進行靜態資源的上傳。
二、使用步驟
1.開發環境
spring+springmvc+maven
2.首先在maven的pom.xml中引入依賴fastdfs-client的依賴
1 <dependency> 2 <groupId>org.csource</groupId> 3 <artifactId>fastdfs-client-java</artifactId> 4 <version>5.0.4</version> 5 </dependency>
3.接著我們來指定一個fastdfs-client.conf配置檔案,裡面內容如下:
tracker_server=host:port(這裡指trackerServer伺服器的ip和埠)
4.然後寫一個單元測試類來測試服務
package com.hafiz.fastdfs; import java.io.FileNotFoundException; import java.io.IOException; import org.csource.common.MyException; import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.StorageClient; import org.csource.fastdfs.StorageServer; import org.csource.fastdfs.TrackerClient; import org.csource.fastdfs.TrackerServer; import org.junit.Test; import com.taotao.common.utils.FastDFSClient; public class FastdfsTest { private static final String CONFIGLOCATION = "D:\\fastdfs_client.conf"; @Test public void testUploadImg () { try { // 初始化全域性配置。載入client配置檔案 ClientGlobal.init(CONFIGLOCATION); // 建立一個TrackerClient物件 TrackerClient trackerClient = new TrackerClient(); // 建立一個TrackerServer物件 TrackerServer trackerServer = trackerClient.getConnection(); // 宣告一個StorageServer物件並初始為null StorageServer storageServer = null; // 獲得StorageClient物件 StorageClient storageClient = new StorageClient(trackerServer, storageServer); // 直接呼叫StorageClient物件方法上傳檔案即可 String[] result = storageClient.upload_file("D:\\Documents\\Downloads\\高圓圓2.jpg", "jpg", null); for(String item : result) { System.out.println(item); } trackerServer.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } } @Test public void fastDfsClientTest() { try { FastDFSClient client = new FastDFSClient(CONFIGLOCATION); String imgUrl = client.uploadFile("D:\\Documents\\Downloads\\高圓圓1.jpg", "jpg", null); System.out.println(imgUrl); } catch (Exception e) { e.printStackTrace(); } } }
5.為了以後在專案中使用方便,我們不能每次都寫這麼一大串東西,所以我們來對該客戶端進行以下封裝:
package com.hafiz.common.utils; 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:")) { String url = this.getClass().getResource("/").getPath(); url = url.substring(1); conf = conf.replace("classpath:", url); } ClientGlobal.init(conf); trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageServer = null; storageClient = new StorageClient1(trackerServer, storageServer); } public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception { return storageClient.upload_file1(fileName, extName, metas); } public String uploadFile(String fileName, String extName) throws Exception { return storageClient.upload_file1(fileName, extName, null); } public String uploadFile(String fileName) throws Exception { return storageClient.upload_file1(fileName, null, null); } public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception { return storageClient.upload_file1(fileContent, extName, metas); } public String uploadFile(byte[] fileContent, String extName) throws Exception { return storageClient.upload_file1(fileContent, extName, null); } public String uploadFile(byte[] fileContent) throws Exception { return storageClient.upload_file1(fileContent, null, null); } }
三、總結
通過以上的步驟,我們就完成在java中使用fastdfs客戶端進行靜態資源上傳的功能,這裡面我們得到一個最重要的思想就是:DRY(Don't Repeat Yourself!),要有封裝的思想。