圖片伺服器的使用(FastDFS)
阿新 • • 發佈:2018-12-16
- 把FastDFS提供的jar包新增到工程中
- 初始化全域性配置。載入一個配置檔案。
- 建立一個TrackerClient物件。
- 建立一個TrackerServer物件。
- 宣告一個StorageServer物件,null。
- 獲得StorageClient物件。
- 直接呼叫StorageClient物件方法上傳檔案即可。
測試一、text測試
@Test publicvoidtestUpload() throws Exception { // 1、把FastDFS提供的jar包新增到工程中 // 2、初始化全域性配置。載入一個配置檔案。 ClientGlobal.init("D:\\workspaces\\xiafei-manager\\xiafei-manager-web\\src\\main\\resources\\properties\\client.conf"); // 3、建立一個TrackerClient物件。 TrackerClient trackerClient = new TrackerClient(); // 4、建立一個TrackerServer物件。 TrackerServer trackerServer = trackerClient.getConnection(); // 5、宣告一個StorageServer物件,null。 StorageServer storageServer = null; // 6、獲得StorageClient物件。 StorageClient storageClient = new StorageClient(trackerServer, storageServer); // 7、直接呼叫StorageClient物件方法上傳檔案即可。 String[] strings = storageClient.upload_file("D:\\Documents\\Pictures\\images\\2f2eb938943d.jpg", "jpg", null); for (String string : strings) { System.out.println(string); } }
Client.conf
tracker_server=192.168.198.129:22122
測試二:建立工具類實現上傳
1、書寫工具類
package com.yunhe.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:")) { System.out.println("before conf======"+conf); conf = conf.replace("classpath:", this.getClass().getResource("/").getPath()); System.out.println("after conf======"+conf); } 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); } }
2、呼叫工具類
@Test publicvoid testFastDfsClient() throws Exception { FastDFSClientclient = newFastDFSClient("D:\\workspaces\\xiafei-manager\\xiafei-manager-web\\src\\main\\resources\\properties\\client.conf"); String uploadFile = client.uploadFile("D:\\Documents\\Pictures\\images\\200811281555127886.jpg", "jpg"); System.out.println(uploadFile); }