fastdfs java 客户端_FastDFS分布文件系统Java客户端使用

packagecom.digi_zones.fdfs;importorg.apache.commons.io.FileUtils;importorg.apache.commons.io.IOUtil;importorg.csource.common.NameValuePair;import org.csource.fastdfs.*;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.HashMap;importjava.util.Iterator;importjava.util.Map;/***

Description: FastDFS文件上传下载工具类

*

Copyright: Copyright (c) 2016

*

*@authoryangxin

*@version1.0

* @date 2016/10/19*/

public classFastDFSClient {private static final String CONFIG_FILENAME = "src/main/resources/fdfs/fdfs_client.conf";private static StorageClient1 storageClient1 = null;//初始化FastDFS Client

static{try{

ClientGlobal.init(CONFIG_FILENAME);

TrackerClient trackerClient= newTrackerClient(ClientGlobal.g_tracker_group);

TrackerServer trackerServer=trackerClient.getConnection();if (trackerServer == null) {throw new IllegalStateException("getConnection return null");

}

StorageServer storageServer=trackerClient.getStoreStorage(trackerServer);if (storageServer == null) {throw new IllegalStateException("getStoreStorage return null");

}

storageClient1= newStorageClient1(trackerServer,storageServer);

}catch(Exception e) {

e.printStackTrace();

}

}/*** 上传文件

*@paramfile 文件对象

*@paramfileName 文件名

*@return

*/

public staticString uploadFile(File file, String fileName) {return uploadFile(file,fileName,null);

}/*** 上传文件

*@paramfile 文件对象

*@paramfileName 文件名

*@parammetaList 文件元数据

*@return

*/

public static String uploadFile(File file, String fileName, MapmetaList) {try{byte[] buff = IOUtil.toByteArray(newFileInputStream(file));

NameValuePair[] nameValuePairs= null;if (metaList != null) {

nameValuePairs= newNameValuePair[metaList.size()];int index = 0;for (Iterator> iterator =metaList.entrySet().iterator(); iterator.hasNext();) {

Map.Entry entry =iterator.next();

String name=entry.getKey();

String value=entry.getValue();

nameValuePairs[index++] = newNameValuePair(name,value);

}

}returnstorageClient1.upload_file1(buff,FileUtils.getExtension(fileName),nameValuePairs);

}catch(Exception e) {

e.printStackTrace();

}return null;

}/*** 获取文件元数据

*@paramfileId 文件ID

*@return

*/

public static MapgetFileMetadata(String fileId) {try{

NameValuePair[] metaList=storageClient1.get_metadata1(fileId);if (metaList != null) {

HashMap map = new HashMap();for(NameValuePair metaItem : metaList) {

map.put(metaItem.getName(),metaItem.getValue());

}returnmap;

}

}catch(Exception e) {

e.printStackTrace();

}return null;

}/*** 删除文件

*@paramfileId 文件ID

*@return删除失败返回-1,否则返回0*/

public static intdeleteFile(String fileId) {try{returnstorageClient1.delete_file1(fileId);

}catch(Exception e) {

e.printStackTrace();

}return -1;

}/*** 下载文件

*@paramfileId 文件ID(上传文件成功后返回的ID)

*@paramoutFile 文件下载保存位置

*@return

*/

public static intdownloadFile(String fileId, File outFile) {

FileOutputStream fos= null;try{byte[] content =storageClient1.download_file1(fileId);

fos= newFileOutputStream(outFile);

IOUtil.copy(content,fos);return 0;

}catch(Exception e) {

e.printStackTrace();

}finally{if (fos != null) {try{

fos.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}return -1;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值