FastDFS連接Tracker服務應用Demo

背景配置

 這是FastDFS連接Tracker服務應用在java程序應用的Demo。多以不管是什麼框架的問題,我這使用的springboot。簡單快捷
 1.  java項目,springboot,javaweb什麼的都可以。
 2. CentOS安裝的fastDFS

1.引用jar

<dependency>
     <groupId>org.csource</groupId>
	 <artifactId>fastdfs-client-java</artifactId>
	 <version>1.27-SNAPSHOT</version>
</dependency>

踩坑ps:fastdfs-client-java-1.27-SNAPSHOT.jar maven不能成功引入依賴。
解決方案
運行git工具,GitHub下載到本地,放入本地的maven倉庫中
步驟 一: git clone https://github.com/happyfish100/fastdfs-client-java.git
      二: cd maven的本地倉庫目錄/fastdfs-client-java
      三: mvn clean install (命令語句)
      四: 引入項目,項目下直接建立lib文件夾,導入jar, add依賴

2.fdfs_client.conf 配置文件

fdfs_client.conf 配置文件內容

# connect timeout in seconds
# default value is 30s
connect_timeout=30

# network timeout in seconds
# default value is 30s
network_timeout=60

# the base path to store log files
base_path=/opt/fastdfs_tracker

# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
tracker_server=118.25.215.105:22122

#HTTP settings
http.tracker_server_port=80

解析
      base_path=/opt/fastdfs_tracker #tracker服務器文件路徑
      tracker_server=118.25.215.105:22122 #tracker服務器IP地址和端口號
      http.tracker_server_port=6666 # tracker 服務器的 http 端口號,必須和tracker的設置對應起來

總結:和安裝時/etc/fdfs目錄下的client.conf 配置是一致的

3.FastDFSUtil 工具類

package com.fastDFS;

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;

/*
 * FastDFS 工具類
 */
public class FastDFSUtil {

	/*
	 * 加載連接Tracker 服務
	 */
	public StorageClient loadTracker(){
		StorageClient storageClient = null;
		try {
			// 1.加載配置文件,配置:tracker 服務地址	
			String pathName = "E:\\work\\testSpace\\demo\\src\\main\\resources\\fdfs_client.conf";
			ClientGlobal.init(pathName);
			
			// 2.創建TrackerClient對象
			TrackerClient trackerClient = new TrackerClient();
			
			// 3.使用 TrackerClient對象 創建連接
			TrackerServer trackerServer = trackerClient.getConnection();
			
			// 4.創建 StorageServer 的引用
			StorageServer storageServer = null;
			
			// 5.創建StorageClient 對象, 需要兩個參數 TrackerServer 對象、 StorageServer的引用
			storageClient = new StorageClient(trackerServer, storageServer);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return storageClient;
	}
	
	/*
	 * 上傳file
	 */
	public String[] uploadFile(String pathName,String suffixName){
		String[] loadFile = null ;
		try {
			// StorageClient 對象上傳圖片
			loadFile = loadTracker().upload_appender_file(pathName, suffixName, null);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MyException e) {
			e.printStackTrace();
		}
		return loadFile;
	}
	
	/*
	 * 刪除file
	 */
	public boolean delFile(String groups ,String pathName){
		boolean flag = false;
		try {
			int i = loadTracker().delete_file(groups, pathName);
			if(i == 0){flag = true;}			
		} catch (IOException e) {
			flag = false;
			e.printStackTrace();
		} catch (MyException e) {
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}
	
}

4.測試

效果:
在這裏插入圖片描述
在這裏插入圖片描述
附:
FsdtDFS 上傳file測試代碼

//上傳圖片
FastDFSUtil sDfsUtil = new FastDFSUtil();
String[] loadFile = sDfsUtil.uploadFile("C:\\Users\\jlf\\Desktop\\io.jpg", "jpg");

// 打印測試
for (String msg : loadFile){
	System.out.println(msg);
}

FsdtDFS 刪除file測試代碼

//刪除圖片
boolean flag = sDfsUtil.delFile("group1", "M00/00/00/rBsABl3gyuyAFZuQAACn_gKysM4454.jpg");
if(flag == true){
    System.out.println("刪除成功");
}else{
	System.out.println("刪除失敗");
}

5.優化升級

package com.fastDFS;

import java.io.IOException;

import org.csource.common.MyException;
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;

/*
 * FastDFS 工具類
 */
public class FastDFSUtil {
	
	private static TrackerClient trackerClient = null;
	private static TrackerServer trackerServer = null;
	private static StorageServer storageServer = null;
	private static StorageClient1 storageClient = null;
	
	//TRACKER_SERVER的IP
	private static String TRACKER_SERVER = "118.25.215.105:22122";
	// 創建文件服務器連接
	static {
		try {
			ClientGlobal.initByTrackers(TRACKER_SERVER);
			ClientGlobal.setG_charset("UTF-8");
			ClientGlobal.setG_connect_timeout(3000); //連接超時
			ClientGlobal.setG_network_timeout(3000); //網絡超時
			trackerClient = new TrackerClient();
			trackerServer = trackerClient.getConnection();
			storageServer = null;
			storageClient = new StorageClient1(trackerServer, storageServer);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MyException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * FastDFS上傳file
	 * @param groups
	 * @param pathName
	 * @return loadFile
	 */
	public static String[] uploadFile(String pathName,String suffixName){
		String[] uploadFile = null ;
		try {
			synchronized (storageClient) {
				// StorageClient 對象文件
				uploadFile = storageClient.upload_appender_file(pathName, suffixName, null);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MyException e) {
			e.printStackTrace();
		}
		return uploadFile;
	}
	
	/**
	 * FastDFS刪除file
	 * @param groups
	 * @param pathName
	 * @return flag
	 */
	public boolean delFile(String groups ,String pathName){
		boolean flag = false;
		try {
			int i = storageClient.delete_file(groups, pathName);
			if(i == 0){flag = true;}			
		} catch (IOException e) {
			flag = false;
			e.printStackTrace();
		} catch (MyException e) {
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}
	
	/**
	 * 通過FastDFS下載文件
	 * @param filePath
	 * @return
	 * @throws Exception
	 */
	public static String downloadFileByFastDFS(String filePath) throws Exception {
		//以下對文件名進行處理  分佈式服務器上輪詢方式取出對應文件
		String [] file_name  = filePath.split("\\/");
		String remote_filename = filePath.substring(file_name[0].length()+1, filePath.length());
		byte str_byte  [] = storageClient.download_file(file_name[0], remote_filename);
		String cellValue = new String(str_byte);
		return cellValue;
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章