fastdfs-client-java操作fastdfs

一、在https://github.com/happyfish100/fastdfs-client-java 下载客户端,解压后并执行ant命令,在E:\tools\libs\fastdfs\fastdfs-client-Java-master\src\build下会生成fastdfs_client.jar如图示

二、mvn安装fastdfs_client.jar,在cmd中执行命令

mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java -Dversion=5.0.4 -Dpackaging=jar -Dfile=D:\fastdfs_client.jar 

三、在eclipse中新建maven项目fastdfs-demo,在pom.xml中加入依赖如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
< project  xmlns = "http://maven.apache.org/POM/4.0.0"  xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
   < modelVersion >4.0.0</ modelVersion >
 
   < groupId >com.leech</ groupId >
   < artifactId >fastdfs-demo</ artifactId >
   < version >0.0.1-SNAPSHOT</ version >
   < packaging >jar</ packaging >
 
   < name >fastdfs-demo</ name >
   < url >http://maven.apache.org</ url >
 
   < properties >
     < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding >
   </ properties >
 
   < dependencies >
     < dependency >
       < groupId >junit</ groupId >
       < artifactId >junit</ artifactId >
       < version >4.11</ version >
       < scope >test</ scope >
     </ dependency >
     
     < dependency >
       < groupId >org.csource</ groupId >
       < artifactId >fastdfs-client-java</ artifactId >
       < version >5.0.4</ version >
     </ dependency >
     
     < dependency >
         < groupId >commons-io</ groupId >
         < artifactId >commons-io</ artifactId >
         < version >2.4</ version >
     </ dependency >
     
   </ dependencies >
</ project >

 

在src/main/resources中新建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.17.112:22122

#tracker_server = 192.168.0.119:22122

在src/main/java中创建TestFastDfs.java如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package  com.leech.fastdfs.demo;
 
import  java.io.FileNotFoundException;
import  java.io.FileOutputStream;
import  java.io.IOException;
import  java.util.UUID;
 
import  org.apache.commons.io.IOUtils;
import  org.csource.common.MyException;
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;
import  org.junit.After;
import  org.junit.Before;
import  org.junit.Test;
 
public  class  TestFastDfs {
     
     public  String conf_filename =  "D:\\stsworkspace\\fastdfs-demo\\src\\main\\resources\\fdfs_client.conf"
     //public String local_filename = "D:\\stsworkspace\\fastdfs-demo\\src\\main\\resources\\fdfs_client.conf";
     
     public  String local_filename =  "D:\\stsworkspace\\fastdfs-demo\\src\\main\\resources\\sprites.png" ;
 
     @Before
     public  void  setUp()  throws  Exception {
     }
 
     @After
     public  void  tearDown()  throws  Exception {
     }
 
     @Test
     public  void  testUpload() {
 
         try 
             ClientGlobal.init(conf_filename);
 
             TrackerClient tracker =  new  TrackerClient(); 
             TrackerServer trackerServer = tracker.getConnection(); 
             StorageServer storageServer =  null ;
 
             StorageClient storageClient =  new  StorageClient(trackerServer, storageServer); 
//          NameValuePair nvp = new NameValuePair("age", "18"); 
             NameValuePair nvp [] =  new  NameValuePair[]{ 
                     new  NameValuePair( "age" "18" ), 
                     new  NameValuePair( "sex" "male"
             }; 
             String fileIds[] = storageClient.upload_file(local_filename,  "png" , nvp);
             
             System.out.println(fileIds.length); 
             System.out.println( "组名:"  + fileIds[ 0 ]); 
             System.out.println( "路径: "  + fileIds[ 1 ]);
 
         catch  (FileNotFoundException e) { 
             e.printStackTrace(); 
         catch  (IOException e) { 
             e.printStackTrace(); 
         catch  (MyException e) { 
             e.printStackTrace(); 
        
     }
 
     @Test 
     public  void  testDownload() {
         try  {
 
             ClientGlobal.init(conf_filename);
 
             TrackerClient tracker =  new  TrackerClient(); 
             TrackerServer trackerServer = tracker.getConnection(); 
             StorageServer storageServer =  null ;
 
             StorageClient storageClient =  new  StorageClient(trackerServer, storageServer); 
             byte [] b = storageClient.download_file( "group1" "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf" ); 
             System.out.println(b); 
             IOUtils.write(b,  new  FileOutputStream( "D:/" +UUID.randomUUID().toString()+ ".conf" ));
         catch  (Exception e) { 
             e.printStackTrace(); 
        
     }
     
     @Test 
     public  void  testGetFileInfo(){ 
         try 
             ClientGlobal.init(conf_filename);
 
             TrackerClient tracker =  new  TrackerClient(); 
             TrackerServer trackerServer = tracker.getConnection(); 
             StorageServer storageServer =  null ;
 
             StorageClient storageClient =  new  StorageClient(trackerServer, storageServer); 
             FileInfo fi = storageClient.get_file_info( "group1" "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf" ); 
             System.out.println(fi.getSourceIpAddr()); 
             System.out.println(fi.getFileSize()); 
             System.out.println(fi.getCreateTimestamp()); 
             System.out.println(fi.getCrc32()); 
         catch  (Exception e) { 
             e.printStackTrace(); 
        
    
     
     @Test 
     public  void  testGetFileMate(){ 
         try 
             ClientGlobal.init(conf_filename);
 
             TrackerClient tracker =  new  TrackerClient(); 
             TrackerServer trackerServer = tracker.getConnection(); 
             StorageServer storageServer =  null ;
 
             StorageClient storageClient =  new  StorageClient(trackerServer, 
                     storageServer); 
             NameValuePair nvps [] = storageClient.get_metadata( "group1" "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf" ); 
             for (NameValuePair nvp : nvps){ 
                 System.out.println(nvp.getName() +  ":"  + nvp.getValue()); 
            
         catch  (Exception e) { 
             e.printStackTrace(); 
        
    
     
     @Test 
     public  void  testDelete(){ 
         try 
             ClientGlobal.init(conf_filename);
 
             TrackerClient tracker =  new  TrackerClient(); 
             TrackerServer trackerServer = tracker.getConnection(); 
             StorageServer storageServer =  null ;
 
             StorageClient storageClient =  new  StorageClient(trackerServer, 
                     storageServer); 
             int  i = storageClient.delete_file( "group1" "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf" ); 
             System.out.println( i== 0  "删除成功"  "删除失败:" +i); 
         catch  (Exception e) { 
             e.printStackTrace(); 
        
     }
}

 

执行testUpload方法如下

 

 

注意:源码和maven仓库 下载地址:http://download.youkuaiyun.com/download/qingchunwuxian1993/9897535

 

转载于:https://www.cnblogs.com/beyondcj/p/7160250.html

FastDFS服务端有两个角色:跟踪器(tracker)和存储节点(storage)。跟踪器主要做调度工作,在访问上起负载均衡的作用。 存储节点存储文件,完成文件管理的所有功能:就是这样的存储、同步和提供存取接口,FastDFS同时对文件的metadata进行管理。所谓文件的meta data就是文件的相关属性,以键值对(key value)方式表示,如:width=1024,其中的key为width,value为1024。文件metadata是文件属性列表,可以包含多个键值对。 跟踪器和存储节点都可以由一台或多台服务器构成。跟踪器和存储节点中的服务器均可以随时增加或下线而不会影响线上服务。其中跟踪器中的所有服务器都是对等的,可以根据服务器的压力情况随时增加或减少。 为了支持大容量,存储节点(服务器)采用了分卷(或分组)的组织方式。存储系统由一个或多个卷组成,卷与卷之间的文件是相互独立的,所有卷的文件容量累加就是整个存储系统中的文件容量。一个卷可以由一台或多台存储服务器组成,一个卷下的存储服务器中的文件都是相同的,卷中的多台存储服务器起到了冗余备份和负载均衡的作用。 在卷中增加服务器时,同步已有的文件由系统自动完成,同步完成后,系统自动将新增服务器切换到线上提供服务。 当存储空间不足或即将耗尽时,可以动态添加卷。只需要增加一台或多台服务器,并将它们配置为一个新的卷,这样就扩大了存储系统的容量。 FastDFS中的文件标识分为两个部分:卷名和文件名,二者缺一不可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值