码迷,mamicode.com
首页 > 编程语言 > 详细

FastDFS分布文件系统Java客户端集成

时间:2017-12-02 12:59:01      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:color   load()   lan   测试   avg   index   class   协同   工具类   

参考博客:http://blog.csdn.net/xyang81/article/details/52847311

官网Java客户端源代码:

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

从上面地址下载压缩包,解压

技术分享图片

使用idea工具打开执行clean install将包打到本地的maven仓库

技术分享图片

技术分享图片

如果协同开发,可将该包上传到公司的私服,供大家一起下载,就不需要下载该工程进行本地打包,只需要引pom地址

工程目录结构

技术分享图片

 

根据官方提供的sdk封装了一个工具类

  1 package com.cky.fastdfsdemo;
  2 
  3 import com.cky.util.FileUtil;
  4 import org.apache.commons.io.IOUtils;
  5 import org.csource.common.NameValuePair;
  6 import org.csource.fastdfs.*;
  7 
  8 import java.io.*;
  9 import java.util.HashMap;
 10 import java.util.Iterator;
 11 import java.util.Map;
 12 
 13 /**
 14  * Created by chenkaiyang on 2017/11/30.
 15  */
 16 public class FastDFSClient {
 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 = new TrackerClient(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 = new StorageClient1(trackerServer,storageServer);
 36 
 37         } catch (Exception e) {
 38             e.printStackTrace();
 39         }
 40     }
 41 
 42     /**
 43      * 上传文件
 44      * @param file 文件对象
 45      * @param fileName 文件名
 46      * @return
 47      */
 48     public static String uploadFile(File file, String fileName) {
 49         return uploadFile(file,fileName,null);
 50     }
 51 
 52     /**
 53      * 上传文件
 54      * @param file 文件对象
 55      * @param fileName 文件名
 56      * @param metaList 文件元数据
 57      * @return
 58      */
 59     public static String uploadFile(File file, String fileName, Map<String,String> metaList) {
 60         try {
 61             byte[] buff = IOUtils.toByteArray(new FileInputStream(file));
 62             NameValuePair[] nameValuePairs = null;
 63             if (metaList != null) {
 64                 nameValuePairs = new NameValuePair[metaList.size()];
 65                 int index = 0;
 66                 for (Iterator<Map.Entry<String,String>> iterator = metaList.entrySet().iterator(); iterator.hasNext();) {
 67                     Map.Entry<String,String> entry = iterator.next();
 68                     String name = entry.getKey();
 69                     String value = entry.getValue();
 70                     nameValuePairs[index++] = new NameValuePair(name,value);
 71                 }
 72             }
 73             return storageClient1.upload_file1(buff, FileUtil.getExtensionName(fileName),nameValuePairs);
 74         } catch (Exception e) {
 75             e.printStackTrace();
 76         }
 77         return null;
 78     }
 79 
 80     /**
 81      * 获取文件元数据
 82      * @param fileId 文件ID
 83      * @return
 84      */
 85     public static Map<String,String> getFileMetadata(String fileId) {
 86         try {
 87             NameValuePair[] metaList = storageClient1.get_metadata1(fileId);
 88             if (metaList != null) {
 89                 HashMap<String,String> map = new HashMap<String, String>();
 90                 for (NameValuePair metaItem : metaList) {
 91                     map.put(metaItem.getName(),metaItem.getValue());
 92                 }
 93                 return map;
 94             }
 95         } catch (Exception e) {
 96             e.printStackTrace();
 97         }
 98         return null;
 99     }
100 
101     /**
102      * 删除文件
103      * @param fileId 文件ID
104      * @return 删除失败返回-1,否则返回0
105      */
106     public static int deleteFile(String fileId) {
107         try {
108             return storageClient1.delete_file1(fileId);
109         } catch (Exception e) {
110             e.printStackTrace();
111         }
112         return -1;
113     }
114 
115     /**
116      * 下载文件
117      * @param fileId 文件ID(上传文件成功后返回的ID)
118      * @param outFile 文件下载保存位置
119      * @return
120      */
121     public static int downloadFile(String fileId, File outFile) {
122         FileOutputStream fos = null;
123         try {
124             byte[] content = storageClient1.download_file1(fileId);
125             fos = new FileOutputStream(outFile);
126            // IOUtils.copy(content,fos);
127             IOUtils.copy(new ByteArrayInputStream(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 }

 

 Java客户端配置文件(fdfs_client.conf):

 

connect_timeout = 10
network_timeout = 20
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890

tracker_server = 192.168.0.204:22122

Java客户端文件上传、下载、删除和元数据获取测试FastDFSClientTest文件

 1 package com.cky.fastdfsdemo;
 2 
 3 import org.junit.Test;
 4 
 5 import java.io.File;
 6 import java.util.HashMap;
 7 import java.util.Iterator;
 8 import java.util.Map;
 9 
10 /**
11  * Created by chenkaiyang on 2017/11/30.
12  */
13 public class FastDFSClientTest {
14 
15     /**
16      * 文件上传测试
17      */
18     @Test
19     public void testUpload() {
20         File file = new File("F:\\img\\edc.jpg");
21         Map<String,String> metaList = new HashMap<String, String>();
22         metaList.put("width","1024");
23         metaList.put("height","768");
24         metaList.put("author","陈冠希");
25         metaList.put("date","20171130");
26         String fid = FastDFSClient.uploadFile(file,file.getName(),metaList);
27         System.out.println("upload local file " + file.getPath() + " ok, fileid=" + fid);
28         //上传成功返回的文件ID: group1/M00/00/00/wKgAyVgFk9aAB8hwAA-8Q6_7tHw351.jpg
29     }
30 
31     /**
32      * 文件下载测试
33      */
34     @Test
35     public void testDownload() {
36         int r = FastDFSClient.downloadFile("group1/M00/00/00/wKgAzFogMFKAVgnMAABIYRjp760474.jpg", new File("F:\\cccDownloadFile_fid.jpg"));
37         System.out.println(r == 0 ? "下载成功" : "下载失败");
38     }
39 
40     /**
41      * 获取文件元数据测试
42      */
43     @Test
44     public void testGetFileMetadata() {
45         Map<String,String> metaList = FastDFSClient.getFileMetadata("group1/M00/00/00/wKgAzFogMFKAVgnMAABIYRjp760474.jpg");
46         for (Iterator<Map.Entry<String,String>> iterator = metaList.entrySet().iterator(); iterator.hasNext();) {
47             Map.Entry<String,String> entry = iterator.next();
48             String name = entry.getKey();
49             String value = entry.getValue();
50             System.out.println(name + " = " + value );
51         }
52     }
53 
54     /**
55      * 文件删除测试
56      */
57     @Test
58     public void testDelete() {
59         int r = FastDFSClient.deleteFile("group1/M00/00/00/wKgAzFogMFKAVgnMAABIYRjp760474.jpg");
60         System.out.println(r == 0 ? "删除成功" : "删除失败");
61     }
62 }

 

获取文件扩展名的工具类

FileUtils

 1 package com.cky.util;
 2 
 3 /**
 4  * Created by chenkaiyang on 2017/11/30.
 5  */
 6 public class FileUtil {
 7     /*
 8  * Java文件操作 获取文件扩展名
 9  *
10  *  Created on: 2011-8-2
11  *      Author: blueeagle
12  */
13     public static String getExtensionName(String filename) {
14         if ((filename != null) && (filename.length() > 0)) {
15             int dot = filename.lastIndexOf(‘.‘);
16             if ((dot >-1) && (dot < (filename.length() - 1))) {
17                 return filename.substring(dot + 1);
18             }
19         }
20         return filename;
21     }
22     /*
23      * Java文件操作 获取不带扩展名的文件名
24      *
25      *  Created on: 2011-8-2
26      *      Author: blueeagle
27      */
28     public static String getFileNameNoEx(String filename) {
29         if ((filename != null) && (filename.length() > 0)) {
30             int dot = filename.lastIndexOf(‘.‘);
31             if ((dot >-1) && (dot < (filename.length()))) {
32                 return filename.substring(0, dot);
33             }
34         }
35         return filename;
36     }
37 
38 }

 pom文件

 <dependency>
      <groupId>org.csource</groupId>
      <artifactId>fastdfs-client-java</artifactId>
      <version>1.27-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-io</artifactId>
      <version>1.3.2</version>
    </dependency>
    
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>

附送项目地址

FastDFS分布文件系统Java客户端集成

标签:color   load()   lan   测试   avg   index   class   协同   工具类   

原文地址:http://www.cnblogs.com/edison20161121/p/7953697.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!