FastDFS Java Api 操作

FastDFS Java Api 操作

1.用eclipse把源代码打成jar文件:(源代码下载地址: https://github.com/happyfish100/fastdfs-client-java)


2、把fastdfs_client.jar 添加到Maven仓库


 
 
  1. #执行 maven命令 添加fastdfs_client.jar 到maven 仓库
  2. mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java -Dversion=5.0.4 -Dpackaging=jar -Dfile=/Users/shenwei/Desktop/fastdfs_client.jar

3、查看  fastdfs_client.jar。可以看到jar文件


4、创建一个maven项目,在pom中添加 fastdfs_client 依赖。


 
 
  1. #添加依赖
  2. <dependency>
  3. <groupId>junit </groupId>
  4. <artifactId>junit </artifactId>
  5. <version>4.11 </version>
  6. <scope>test </scope>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.csource </groupId>
  10. <artifactId>fastdfs-client-java </artifactId>
  11. <version>5.0.4 </version>
  12. </dependency>

5、在src/main/resource 下创建 fdfs_client.conf 配置文件


 
 
  1. #fdfs_client.conf 配置
  2. connect_timeout = 10
  3. network_timeout = 30
  4. charset = UTF-8
  5. http.tracker_http_port = 8888
  6. http.anti_steal_token = no
  7. http.secret_key = FastDFS1234567890
  8. tracker_server = 100.98.22.253:22122
  9. #如果有多台服务,指定集群的IP
  10. #tracker_server = 192.168.10.250:22122

6、测试fastDFS 上传、下载、删除、获取文件信息


 
 
  1. import java.io.FileOutputStream;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import org.csource.common.NameValuePair;
  5. import org.csource.fastdfs.ClientGlobal;
  6. import org.csource.fastdfs.FileInfo;
  7. import org.csource.fastdfs.StorageClient;
  8. import org.csource.fastdfs.StorageClient1;
  9. import org.csource.fastdfs.StorageServer;
  10. import org.csource.fastdfs.TrackerClient;
  11. import org.csource.fastdfs.TrackerServer;
  12. import org.junit.Test;
  13. public class TestFastDfs {
  14. //fdfs_client 核心配置文件
  15. public String conf_filename = "src/main/resources/fdfs_client.conf";
  16. @Test
  17. public void testUpload() { //上传文件
  18. TrackerServer trackerServer = null;
  19. StorageServer storageServer = null;
  20. try {
  21. ClientGlobal.init(conf_filename);
  22. TrackerClient tracker = new TrackerClient();
  23. trackerServer = tracker.getConnection();
  24. StorageClient1 client = new StorageClient1(trackerServer, storageServer);
  25. //要上传的文件路径
  26. String local_filename = "/Users/shenwei/Desktop/1.png";
  27. // 这个参数可以指定,也可以不指定,如果指定了,可以根据 testGetFileMate()方法来获取到这里面的值
  28. // NameValuePair nvp [] = new NameValuePair[]{
  29. // new NameValuePair("age", "18"),
  30. // new NameValuePair("sex", "male")
  31. // };
  32. StorageClient storageClient = new StorageClient(trackerServer, storageServer);
  33. // String fileIds[] = storageClient.upload_file(local_filename, "png", nvp);
  34. String fileIds[] = storageClient.upload_file(local_filename, "png", null);
  35. System.out.println(fileIds.length);
  36. System.out.println( "组名:" + fileIds[ 0]);
  37. System.out.println( "路径: " + fileIds[ 1]);
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. } finally{
  41. try {
  42. if( null!=storageServer) storageServer.close();
  43. if( null!=trackerServer) trackerServer.close();
  44. } catch (IOException e) {
  45. // TODO Auto-generated catch block
  46. e.printStackTrace();
  47. }
  48. }
  49. }
  50. @Test
  51. public void testDownload() { //下载文件
  52. TrackerServer trackerServer = null;
  53. StorageServer storageServer = null;
  54. try {
  55. String groupName = "group1";
  56. String filePath = "M00/00/00/ZGIW_lpujW-ADvpRAAblmT4ACuo125.png";
  57. ClientGlobal.init(conf_filename);
  58. TrackerClient tracker = new TrackerClient();
  59. trackerServer = tracker.getConnection();
  60. StorageClient storageClient = new StorageClient(trackerServer, storageServer);
  61. byte[] bytes = storageClient.download_file(groupName, filePath);
  62. String storePath = "/Users/shenwei/Desktop/download.png";
  63. OutputStream out = new FileOutputStream(storePath);
  64. out.write(bytes);
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. } finally{
  68. try {
  69. if( null!=storageServer) storageServer.close();
  70. if( null!=trackerServer) trackerServer.close();
  71. } catch (IOException e) {
  72. // TODO Auto-generated catch block
  73. e.printStackTrace();
  74. }
  75. }
  76. }
  77. @Test
  78. public void testGetFileInfo(){ //获取文件信息
  79. TrackerServer trackerServer = null;
  80. StorageServer storageServer = null;
  81. try {
  82. String groupName = "group1";
  83. String filePath = "M00/00/00/ZGIW_lpujW-ADvpRAAblmT4ACuo125.png";
  84. ClientGlobal.init(conf_filename);
  85. TrackerClient tracker = new TrackerClient();
  86. trackerServer = tracker.getConnection();
  87. StorageClient storageClient = new StorageClient(trackerServer, storageServer);
  88. FileInfo file = storageClient.get_file_info(groupName, filePath);
  89. System.out.println( "ip--->"+file.getSourceIpAddr());
  90. System.out.println( "文件大小--->"+file.getFileSize());
  91. System.out.println( "文件上传时间--->"+file.getCreateTimestamp());
  92. System.out.println(file.getCrc32());
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. } finally{
  96. try {
  97. if( null!=storageServer) storageServer.close();
  98. if( null!=trackerServer) trackerServer.close();
  99. } catch (IOException e) {
  100. // TODO Auto-generated catch block
  101. e.printStackTrace();
  102. }
  103. }
  104. }
  105. @Test
  106. public void testGetFileMate(){ //获取文件的原数据类型
  107. TrackerServer trackerServer = null;
  108. StorageServer storageServer = null;
  109. try {
  110. String groupName = "group1";
  111. String filePath = "M00/00/00/ZGIW_lpujW-ADvpRAAblmT4ACuo125.png";
  112. ClientGlobal.init(conf_filename);
  113. TrackerClient tracker = new TrackerClient();
  114. trackerServer = tracker.getConnection();
  115. StorageClient storageClient = new StorageClient(trackerServer,
  116. storageServer);
  117. //这个值是上传的时候指定的NameValuePair
  118. NameValuePair nvps [] = storageClient.get_metadata(groupName, filePath);
  119. if( null!=nvps && nvps.length> 0){
  120. for(NameValuePair nvp : nvps){
  121. System.out.println(nvp.getName() + ":" + nvp.getValue());
  122. }
  123. }
  124. } catch (Exception e) {
  125. e.printStackTrace();
  126. } finally{
  127. try {
  128. if( null!=storageServer) storageServer.close();
  129. if( null!=trackerServer) trackerServer.close();
  130. } catch (IOException e) {
  131. // TODO Auto-generated catch block
  132. e.printStackTrace();
  133. }
  134. }
  135. }
  136. @Test
  137. public void testDelete(){ //删除文件
  138. TrackerServer trackerServer = null;
  139. StorageServer storageServer = null;
  140. try {
  141. String groupName = "group1";
  142. String filePath = "M00/00/00/ZGIW_lpujW-ADvpRAAblmT4ACuo125.png";
  143. ClientGlobal.init(conf_filename);
  144. TrackerClient tracker = new TrackerClient();
  145. trackerServer = tracker.getConnection();
  146. StorageClient storageClient = new StorageClient(trackerServer,
  147. storageServer);
  148. int i = storageClient.delete_file(groupName, filePath);
  149. System.out.println( i== 0 ? "删除成功" : "删除失败:"+i);
  150. } catch (Exception e) {
  151. e.printStackTrace();
  152. } finally{
  153. try {
  154. if( null!=storageServer) storageServer.close();
  155. if( null!=trackerServer) trackerServer.close();
  156. } catch (IOException e) {
  157. // TODO Auto-generated catch block
  158. e.printStackTrace();
  159. }
  160. }
  161. }
  162. }
阅读更多
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值