1. 程式人生 > >【FastDFS】maven專案上傳圖片到FastDFS伺服器

【FastDFS】maven專案上傳圖片到FastDFS伺服器

一:前言

  FastDFS伺服器的搭建都是運維人員完成的,開發人員只需知道tracker伺服器IP地址和埠號即可

二:環境裝備

  1.為了程式設計成員在應用程式中使用FastDFS,官網提供了java版的客戶端

  但是中央倉庫中沒有,我們可以下載fastdfs-client-java原始碼,這是個maven專案,可以`maven install`安裝到本地倉庫(因為maven專案引jar包是將jar先從中央倉庫下載到本地倉庫(在pom.xml檔案中配置好座標,儲存jar包就下載到本地倉庫了),然後專案引用時再根據座標在本地倉庫找依賴的jar包,如果中央倉庫沒有,想下載到本地倉庫,只能maven install直接安裝到本地倉庫)

  2.在pom.xml檔案中新增依賴

 <dependency> 
   <groupId>fastdfs_client</groupId>
  <artifactId>fastdfs_client</artifactId>
  <version>1.25</version>

  依賴根據原始檔的pom.xml得來

  3.儲存後,專案中就會新增該依賴了

三.使用FastDFS程式碼上傳圖片

  1.建立配置檔案,內容為tracker伺服器地址

  2.編寫測試類

	@Test
	public void testUpload() throws Exception {
		//1.建立一個配置檔案,檔名任意,內容是tracker伺服器的地址
		//2.使用全域性物件載入配置檔案
		ClientGlobal.init("G:程式碼/e3-manager-web/src/main/resources/conf/client.conf");
		//3.建立一個TrackClient
		TrackerClient trackerClient = new TrackerClient();
		//4.通過TrackClient獲得一個TrackerServer物件
		TrackerServer trackerServer = trackerClient.getConnection();
		//5.建立一個storageServer的引用,可以是null
		StorageServer storageServer = null;
		//6.建立一個storageClient,引數需要TrackerServer和StorageServer
		StorageClient storageClient = new StorageClient(trackerServer, storageServer);
		//7.使用StorageClient上傳檔案
		String[] strings = storageClient.upload_file("G:/圖片/1.jpg", "jpg", null);
		for (String string:strings){
			System.out.println(string);
		}
	}

四.封裝FastDFS工具類

  1.在正式的專案開發中,我們通常會將有關FastDFS操作封裝為一個工具類

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);
	}
}

  2.測試該工具類:

@Test
public void testFastDfsClient() throws Exception{
	FastDFSClient fastDFSClient = new FastDFSClient("G:/程式碼/e3-manager-web/src/main/resources/conf/client.conf");
	String string = fastDFSClient.uploadFile("G:/圖片/1.jpg");
	System.out.println(string);	
}

  3.返回檔案路徑表示工具類封裝成功: