FastDFS接続トラッカーサービスアプリケーションのデモ

FastDFS接続トラッカーサービスアプリケーションのデモ

背景配置

 这是FastDFS连接Tracker服务应用在java程序应用的Demo。多以不管是什么框架的问题,我这使用的springboot。简单快捷
 1.  java项目,springboot,javaweb什么的都可以。
 2. CentOS安装的fastDFS

1.参照ジャー

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

段付ピットPS:fastdfsクライアント-1.27-SNAPSHOT.jarにJavaの-Mavenは成功導入に頼ることはできません。
ソリューション
実行gitのツールは、GitHubには、ローカルのMavenリポジトリに、ローカルにダウンロード
ステップ1:gitのクローンhttps://github.com/happyfish100/fastdfs-client-java.git
      2:CD Mavenのローカルリポジトリディレクトリ/ fastdfs-クライアント-javaの
      3:MVNクリーンインストール(コマンド文)
      IV:プロジェクトの導入、LIBフォルダの直下に設立されたプロジェクト、jarファイルをインポートし、追加依存を

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#トラッカーサーバーのHTTPポート、それが関連トラッカーに提供されなければなりません

概要:あなたはインストールし、/ 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ファイルアップロードテストコード

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

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

FsdtDFS削除ファイルのテストコード

//删除图片
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;
	}

}

公開された16元の記事 ウォンの賞賛3 ビュー527

おすすめ

転載: blog.csdn.net/outdata/article/details/103362143