Java管理FastDFS分布文件系统

下载Java类库

https://github.com/happyfish100/fastdfs-client-java

编译并安装到本地Maven仓库

maven clean install

在/src/main/resources新建配置文件fdfs_client.conf

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

tracker_server = 192.168.25.100:22122
#tracker_server = 10.0.11.248:22122
#tracker_server = 10.0.11.249:22122

fastdfs.tracker_servers = 192.168.25.100:22122

设计工具类

package com.gf.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

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;
import org.csource.fastdfs.UploadCallback;

public class FastDfsUtil {
	public void uploadFile(String localFile)
	{
		try{
			InputStream is = this.getClass().getClassLoader().getResourceAsStream("fdfs_client.conf");
			Properties prop = new Properties();
			prop.load(is);
			System.out.println("prop==="+prop);
			ClientGlobal.initByProperties(prop);
			System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
			System.out.println("charset=" + ClientGlobal.g_charset);
			
			TrackerClient tracker = new TrackerClient();
			TrackerServer trackerServer = tracker.getConnection();
			StorageServer storageServer = null;
			StorageClient1 client = new StorageClient1(trackerServer, storageServer);
			
			NameValuePair[] metaList = new NameValuePair[1];
			metaList[0] = new NameValuePair("fileName", localFile);
			String fileId = client.upload_file1(localFile, null, metaList);
			System.out.println("upload success. file id is: " + fileId);
			
			trackerServer.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public void uploadFileCallback(String localFile,UploadCallback callback)
	{
		try{
			InputStream is = this.getClass().getClassLoader().getResourceAsStream("fdfs_client.conf");
			Properties prop = new Properties();
			prop.load(is);
			System.out.println("prop==="+prop);
			ClientGlobal.initByProperties(prop);
			File f = new File(localFile);
			long fileSize = f.length();
			
			TrackerClient tracker = new TrackerClient();
			TrackerServer trackerServer = tracker.getConnection();
			StorageServer storageServer = null;
			StorageClient1 client = new StorageClient1(trackerServer, storageServer);
			
			NameValuePair[] metaList = new NameValuePair[1];
			metaList[0] = new NameValuePair("fileName", localFile);
			String fileId = client.upload_file1(null,fileSize,callback,localFile, metaList);
			System.out.println("upload success. file id is: " + fileId);
			
			trackerServer.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public byte[] downloadFile(String fileId)
	{
		try
		{
			InputStream is = this.getClass().getClassLoader().getResourceAsStream("fdfs_client.conf");
			Properties prop = new Properties();
			prop.load(is);
			System.out.println("prop==="+prop);
			ClientGlobal.initByProperties(prop);
			
			System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
			System.out.println("charset=" + ClientGlobal.g_charset);
			
			TrackerClient tracker = new TrackerClient();
			TrackerServer trackerServer = tracker.getConnection();
			StorageServer storageServer = null;
			StorageClient1 client = new StorageClient1(trackerServer, storageServer);
			
			byte[] result = client.download_file1(fileId);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args)
	{
		String conf = "D:\\workspace2\\FiProj\\src\\main\\resources\\fdfs_client.conf";
		String file = "H:\\my.png";
		UploadbackImpl upback = new UploadbackImpl();
		
		FastDfsUtil fdu = new FastDfsUtil();
		fdu.uploadFile(file);
		
		byte[] data = fdu.downloadFile("group1/M00/00/00/wKgZg1xIRLiAApePAAH9u1jEkSs437.png");
		try
		{
			FileOutputStream fos = new FileOutputStream("h:/my3.png");
			fos.write(data);
			fos.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值