FastDFS Java API

项目github地址:https://github.com/happyfish100/fastdfs-client-java
maven仓库地址:https://mvnrepository.com/artifact/org.csource/fastdfs-client-java/1.27-RELEASE

使用maven架构,pom.xml引入依赖

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

添加配置文件fdfs_client.conf,增加以下配置

connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 80
http.anti_steal_token = no
http.secret_key = FastDFS1234567890

tracker_server = 192.168.169.129:22122

http.anti_steal_token:配置防盗链,no表示不开启
http.secret_key:防盗链密码
tracker_server:tracker连接信息,有多少tracker就配置几个

FastDFS Java Client测试

package com.hbk.test;

import java.io.FileOutputStream;
import java.io.OutputStream;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

public class FastDFS_TEST {

	
	public static void upload(StorageClient storageClient) throws Exception{
		String local_file = "E:/44.png";
		String file_ext_name = local_file.substring(local_file.lastIndexOf("."));
		String[] file = storageClient.upload_file(local_file, file_ext_name, null);// 不设置元数据
		System.out.println(file.length);
		System.out.println("组名:" + file[0]);
		System.out.println("路径:" + file[1]);
	}

	public static void download(StorageClient storageClient,String groupName,String filePath) throws Exception{
		byte[] bytes = storageClient.download_file(groupName, filePath);
		String outPath = "E:/download.png";
		OutputStream out = new FileOutputStream(outPath);
		out.write(bytes);
		out.close();
	}
	
	public static void getFileInfo(StorageClient storageClient,String groupName,String filePath) throws Exception{
		FileInfo info = storageClient.get_file_info(groupName, filePath);
		System.out.println("源IP:"+info.getSourceIpAddr());
		System.out.println("文件大小:"+info.getFileSize());
		System.out.println("上传时间:"+info.getCreateTimestamp());
		System.out.println(info.toString());
	}
	
	public static void getFileMetaData(StorageClient storageClient,String groupName,String filePath) throws Exception{
		NameValuePair pairs[] = storageClient.get_metadata(groupName, filePath);
		if(pairs!=null&&pairs.length>0){
			for(NameValuePair pair : pairs){
				System.out.println(pair.getName()+":"+pair.getValue());
			}
		}
	}
	
	public static void deleteFile(StorageClient storageClient,String groupName,String filePath) throws Exception{
		int result = storageClient.delete_file(groupName, filePath);
		System.out.println(result == 0 ? "删除成功" : "删除失败");
	}
	public static void main(String[] args) {
		try {
			// 加载配置文件
			String conf = FastDFS_TEST.class.getClassLoader().getResource("fdfs_client.conf").getPath();
			ClientGlobal.init(conf);
			// 建立连接
			TrackerClient tracker = new TrackerClient();
			TrackerServer trackerServer = tracker.getConnection();
			StorageServer storageServer = tracker.getStoreStorage(trackerServer);
			StorageClient storageClient = new StorageClient(trackerServer, storageServer);

			// 1、上传文件测试
			//upload(storageClient);
			/**输出如下
			 * 2
组名:group1
路径:M00/00/00/wKipgV3y-M2ARt1aAAC8prDWo7o74..png
			 */
			
			// 2、下载文件测试
			//String groupName = "group1";
			//String filePath = "M00/00/00/wKipgV3y-M2ARt1aAAC8prDWo7o74..png";
			//download(storageClient, groupName, filePath);

			// 3、获取文件信息测试(测试的时候,这个报了Input to decode not an even multiple of 4 characters; pad with =.其他测试都成功了)
			//String groupName = "group1";
			//String filePath = "M00/00/00/wKipgV3y-M2ARt1aAAC8prDWo7o74..png";
			//getFileInfo(storageClient, groupName, filePath);
			
			// 4、获取文件元信息
			//String groupName = "group1";
			//String filePath = "M00/00/00/wKipgV3y-M2ARt1aAAC8prDWo7o74..png";
			//getFileMetaData(storageClient, groupName, filePath);
			
			// 5、删除文件
			//String groupName = "group1";
			//String filePath = "M00/00/00/wKipgV3y-M2ARt1aAAC8prDWo7o74..png";
			//deleteFile(storageClient, groupName, filePath);
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄宝康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值