FastDFS应用

一、常用类与API

1.1、ClientGlobal

用于加载配置文件的公共客户端工具;
常用方法:
init(String conf_filename)
根据配置文件路径及命名,加载配置文件并设置客户端公共 参数,配置文件类型为 conf 文件,可以使用绝对路径或相对路径加载;
initByProperties(Properties props)
根据 Properties 对象设置客户端公共参数

1.2、TrackerClient

跟踪器客户端类型,创建此类型对象时,需传递跟踪器组,就是跟踪器的访问地址信息;
创建对象的方式:
new TrackerClient()
new TrackerClient(ClientGlobal.g_tracker_group)

1.3、TrackerServer

跟踪器服务类型,此类型的对象是通过跟踪器客户端对象构建的。实质上就是一个与 FastDFS Tracker Server 的链接对象,是代码中与 Tracker Server 链接的工具;
构建对象的方式:
trackerClient.getConnection()

1.4、StorageServer

存储服务类型,此类型的对象是通过跟踪器客户端对象构建的。实质上就是一个与 FastDFS Storage Server 的链接对象,是代码中与 StroageServer 链接的工具;获取的具体存储 服务链接,是由 Tracker Server 分配的,所以构建存储服务对象时,需要依赖跟踪器服务对象;
构建对象的方式:
trackerClient.getStoreStorage(trackerServer)

1.5、StorageClient

存储客户端类型,创建时,需传递跟踪服务对象 和存储服务对象。此对象实质上就是一个访问 FastDFS Storage Server 的客户端对象,用于实 现文件的读写操作;
创建对象的方式:
new StorageClient(trackerServer, storageServer)
常用方法:
upload_file(String local_filename, String file_ext_name, NameValuePair[] meta_list):上传文件的方法,参数 local_filename 为要上传的本地文件路径及文件名,可使用绝对路径或相对路径;参数 file_ext_name 为上传文件的扩展名,如果传递 null,则自动解析文件扩展名; 参数 meta_list 是用于设置上传文件的源数据的,如上传用户、上传描述等;
download_file(String group_name, String remote_file_name):下 载文件的方法 , 参数 group为组名/卷名 , 就是Storage Server中/etc/fdfs/storage.conf配置文件中配置的 group_name参数值,也是要下载的文件所在组/卷的命名;参数 remote_file_name 为要下载 的文件的路径及文件名;
delete_file(String group_name, String remote_file_name):删除文件的方法,参数含义同 download_file 方法参数;

二、示例操作

2.1、引入pom

		<dependency>
			<groupId>cn.bestwu</groupId>
			<artifactId>fastdfs-client-java</artifactId>
			<version>1.27</version>
		</dependency>

2.2、配置

# 连接超时时间(单位s)
connect_timeout = 10
# 网络超时时间(单位s)
network_timeout = 30
charset = UTF-8
# 必须与tracker server中的etc/fdfs/tracker.conf配置文件中的http.server_port=8080配置一致
http.tracker_http_port = 8080
# tracker server的IP域端口(如果有多个配置多行)
tracker_server = 192.168.48.128:22122

2.3、单服务测试

package com.sxt.fastdfs.singe;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.UUID;

import org.apache.commons.io.IOUtils;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.junit.Test;

public class TestSingle {

	private final String configFile = "src/test/resources/fdfs_client.conf";
	private final Properties props = new Properties();

	/**
	 * 根据相对路径,直接加载配置文件。
	 * 加载的是xxx.conf配置文件。
	 * 默认的加载路径是相对地址。是相对于项目的根开始寻址的。不是相对于classpath开始寻址的。
	 */
	private void init() throws IOException, MyException{
		ClientGlobal.init(configFile);
	}

	/**
	 * 根据Properties,加载配置文件。
	 * 加载properties配置文件,生成一个Properties对象。再实现环境的初始化。
	 */
	private void initByProperties() throws IOException, MyException{
		InputStream in = TestSingle.class.getClassLoader().getResourceAsStream("fdfs_client.properties");
		props.load(in);
		ClientGlobal.initByProperties(props);
	}

	/**
	 * 测试上传
	 */
	@Test
	public void testUpload(){
		try{
			this.init(); // 使用conf配置文件初始化环境。就是加载链接超时,网络超时,tracker服务器列表等。
			// this.initByProperties();
			TrackerClient trackerClient = new TrackerClient(); // new TrackerClient(TrackerGlobal.g_tracker_group);
			// 创建tracker服务器的链接对象
			TrackerServer trackerServer = trackerClient.getConnection();
			// 创建storage服务器的链接对象
			StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
			// 创建storage服务器的客户端操作对象。可以实现文件的读写操作。
			StorageClient storageClient = new StorageClient(trackerServer, storageServer);
			// 要上传的文件的元数据。可以自定义。
			NameValuePair[] nvp = null;
	        /*
	        NameValuePair nvp [] = new NameValuePair[]{
	                new NameValuePair("filename", "xxxxx"),
	                new NameValuePair("filelength", "xxxx")
	        };
	        */
			// 文件上传工作。参数分别是: (要上传的本地文件,上传的文件的类型,上传的文件的元数据)
			// 文件类型可以不传递,也就是传递一个null。FastDFS可以自动的在文件名中截取文件类型。
			// 返回的结果是一个字符串数组。长度为2。0位置是卷名-group1,1位置是文件名-M00/00/00/xxxxxx
			String fileIds[] = storageClient.upload_file("testFile/1.jpg", "jpg", nvp);

			System.out.println(fileIds.length);
			System.out.println("组名/卷名:" + fileIds[0]);
			System.out.println("路径: " + fileIds[1]);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 使用StorageClient1子类型测试文件上传
	 */
	@Test
	public void testUploadWithStorageClient1(){
		try{
			// this.init();
			this.initByProperties();
			
			TrackerClient trackerClient = new TrackerClient();
			TrackerServer trackerServer = trackerClient.getConnection();
			StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
			
			StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
			
			NameValuePair[] nvp = null;
			
			InputStream in = new FileInputStream("testFile/2.jpg");
			byte[] buff = null;
			if(in != null){
				buff = new byte[in.available()];
				in.read(buff);
			}
			// 参数分别是文件的字节数组,文件的类型,文件的元数据
			// 返回结果是卷名/文件名
			String fileId = storageClient1.upload_file1(buff, "jpg", nvp);
			
			System.out.println(fileId);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	

	/**
	 * 测试下载
	 */
	@Test
	public void testDownload(){
		try{
			this.init();

			TrackerClient trackerClient = new TrackerClient();
			TrackerServer trackerServer = trackerClient.getConnection();
			StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);

			StorageClient storageClient = new StorageClient(trackerServer, storageServer);
			// 下载方法参数为卷名和文件名。返回值是要下载的文件的字节数组。
			byte[] buff = storageClient.download_file("group1", "M00/00/00/wKgwgV8xae-AeDAIAADW-MWxcrw656.jpg");

			IOUtils.write(buff, new FileOutputStream("testFile/"+UUID.randomUUID().toString()+".jpg"));
			System.out.println("下载完成");
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * 测试删除
	 */
	@Test
	public void testDelete(){
		try{
			this.init();

			TrackerClient trackerClient = new TrackerClient();
			TrackerServer trackerServer = trackerClient.getConnection();
			StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);

			StorageClient storageClient = new StorageClient(trackerServer, storageServer);

			// 0为成功删除
			// 两个参数分别是卷名和文件名。
			int flag = storageClient.delete_file("group1", "M00/00/00/wKgCbltToWWAK4FiAADW-MWxcrw927.jpg");
			System.out.println(flag == 0 ? "删除成功" : "删除失败");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
}

2.4、测试效果

2.4.1、上传

输出日志

2
组名/卷名:group1
路径: M00/00/00/wKgwgV8xae-AeDAIAADW-MWxcrw656.jpg

存储路径

[root@localhost 00]# ll
总用量 56
-rw-r--r--. 1 root root 55032 810 15:38 wKgwgV8xae-AeDAIAADW-MWxcrw656.jpg
[root@localhost 00]# pwd
/fastdfs/storage/store/data/00/00
2.4.2、下载
下载完成
2.4.3、删除
删除成功
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值