fastDFS圖片伺服器工具類
阿新 • • 發佈:2018-12-09
package com.hr.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;
/** * * @ClassName FastDFSClient * @Description TODO(FastDFS檔案上傳工具) * @author Perfree * @Date 2018年8月22日 上午10:55:45 * @version 1.0.0 */ 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); } }