fastdfs java 客户端_FastDFS分布文件系统Java客户端集成

1 packagecom.cky.fastdfsdemo;2

3 importcom.cky.util.FileUtil;4 importorg.apache.commons.io.IOUtils;5 importorg.csource.common.NameValuePair;6 import org.csource.fastdfs.*;7

8 import java.io.*;9 importjava.util.HashMap;10 importjava.util.Iterator;11 importjava.util.Map;12

13 /**

14 * Created by chenkaiyang on 2017/11/30.15 */

16 public classFastDFSClient {17 private static final String CONFIG_FILENAME = "src/main/resources/fdfs/fdfs_client.conf";18 private static StorageClient1 storageClient1 = null;19

20 //初始化FastDFS Client

21 static{22 try{23 ClientGlobal.init(CONFIG_FILENAME);24 TrackerClient trackerClient = newTrackerClient(ClientGlobal.g_tracker_group);25 TrackerServer trackerServer =trackerClient.getConnection();26 if (trackerServer == null) {27 throw new IllegalStateException("getConnection return null");28 }29

30 StorageServer storageServer =trackerClient.getStoreStorage(trackerServer);31 if (storageServer == null) {32 throw new IllegalStateException("getStoreStorage return null");33 }34

35 storageClient1 = newStorageClient1(trackerServer,storageServer);36

37 } catch(Exception e) {38 e.printStackTrace();39 }40 }41

42 /**

43 * 上传文件44 *@paramfile 文件对象45 *@paramfileName 文件名46 *@return

47 */

48 public staticString uploadFile(File file, String fileName) {49 return uploadFile(file,fileName,null);50 }51

52 /**

53 * 上传文件54 *@paramfile 文件对象55 *@paramfileName 文件名56 *@parammetaList 文件元数据57 *@return

58 */

59 public static String uploadFile(File file, String fileName, MapmetaList) {60 try{61 byte[] buff = IOUtils.toByteArray(newFileInputStream(file));62 NameValuePair[] nameValuePairs = null;63 if (metaList != null) {64 nameValuePairs = newNameValuePair[metaList.size()];65 int index = 0;66 for (Iterator> iterator =metaList.entrySet().iterator(); iterator.hasNext();) {67 Map.Entry entry =iterator.next();68 String name =entry.getKey();69 String value =entry.getValue();70 nameValuePairs[index++] = newNameValuePair(name,value);71 }72 }73 returnstorageClient1.upload_file1(buff, FileUtil.getExtensionName(fileName),nameValuePairs);74 } catch(Exception e) {75 e.printStackTrace();76 }77 return null;78 }79

80 /**

81 * 获取文件元数据82 *@paramfileId 文件ID83 *@return

84 */

85 public static MapgetFileMetadata(String fileId) {86 try{87 NameValuePair[] metaList =storageClient1.get_metadata1(fileId);88 if (metaList != null) {89 HashMap map = new HashMap();90 for(NameValuePair metaItem : metaList) {91 map.put(metaItem.getName(),metaItem.getValue());92 }93 returnmap;94 }95 } catch(Exception e) {96 e.printStackTrace();97 }98 return null;99 }100

101 /**

102 * 删除文件103 *@paramfileId 文件ID104 *@return删除失败返回-1,否则返回0105 */

106 public static intdeleteFile(String fileId) {107 try{108 returnstorageClient1.delete_file1(fileId);109 } catch(Exception e) {110 e.printStackTrace();111 }112 return -1;113 }114

115 /**

116 * 下载文件117 *@paramfileId 文件ID(上传文件成功后返回的ID)118 *@paramoutFile 文件下载保存位置119 *@return

120 */

121 public static intdownloadFile(String fileId, File outFile) {122 FileOutputStream fos = null;123 try{124 byte[] content =storageClient1.download_file1(fileId);125 fos = newFileOutputStream(outFile);126 //IOUtils.copy(content,fos);

127 IOUtils.copy(newByteArrayInputStream(content), fos);128 return 0;129 } catch(Exception e) {130 e.printStackTrace();131 } finally{132 if (fos != null) {133 try{134 fos.close();135 } catch(IOException e) {136 e.printStackTrace();137 }138 }139 }140 return -1;141 }142

143 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值