为了方便应用程序的访问FastDFS,官网提供了fastdfs-client-java,以便更好的与应用程序结合使用。

下载fastdfs-client-java源码添加到项目工程里面,添加配置文件:fdfs_client.conf

这个jar包在中央仓库是没有的,我们可以将源码下载下来,使用maven install安装到本地仓库。

附上pom.xml文件

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4.  
  5. <groupId>org.csource</groupId>
  6. <artifactId>fastdfs-client-java</artifactId>
  7. <version>1.25</version>
  8. <name>fastdfs-client-java</name>
  9. <description>fastdfs client with java</description>
  10. <packaging>jar</packaging>
  11.  
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  15. <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
  16. </properties>
  17.  
  18. <build>
  19. <sourceDirectory>src</sourceDirectory>
  20. <plugins>
  21. <plugin>
  22. <groupId>org.apache.maven.plugins</groupId>
  23. <artifactId>maven-compiler-plugin</artifactId>
  24. <version>2.5.1</version>
  25. <configuration>
  26. <encoding>UTF-8</encoding>
  27. <skip>true</skip>
  28. <source>1.5</source>
  29. <target>1.5</target>
  30. </configuration>
  31. </plugin>
  32. <plugin>
  33. <groupId>org.apache.maven.plugins</groupId>
  34. <artifactId>maven-jar-plugin</artifactId>
  35. <version>2.4</version>
  36. <executions>
  37. <execution>
  38. <phase>package</phase>
  39. <goals>
  40. <goal>jar</goal>
  41. </goals>
  42. <configuration>
  43. <excludes>
  44. <exclude>**/test/*.class</exclude>
  45. </excludes>
  46. </configuration>
  47. </execution>
  48. </executions>
  49. </plugin>
  50. </plugins>
  51. </build>
  52. </project>

在自己的maven项目中加入坐标

  1. <groupId>org.csource</groupId>
  2. <artifactId>fastdfs-client-java</artifactId>
  3. <version>1.25</version>

即可。

测试文件上传

项目的配置文件

fdfs_client.conf

  1. connect_timeout = 2
  2. network_timeout = 30
  3. #对这些设置还不是很清楚
  4. charset = ISO8859-1
  5. http.tracker_http_port = 8080
  6. http.anti_steal_token = no
  7. http.secret_key = FastDFS1234567890
  8.  
  9. #tracker_server的设置是必须的
  10. tracker_server = 192.168.0.111:22122
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.Arrays;
  6.  
  7. import org.csource.common.NameValuePair;
  8. import org.csource.fastdfs.ClientGlobal;
  9. import org.csource.fastdfs.StorageClient;
  10. import org.csource.fastdfs.StorageServer;
  11. import org.csource.fastdfs.TrackerClient;
  12. import org.csource.fastdfs.TrackerServer;
  13.  
  14. public class FastDFSTest {
  15. public static void main(String[] args) throws Exception {
  16. //加载配置文件的方式
  17. String configFileName = "D:\\IdeaProject\\taotao\\taotao-manager\\taotao-manager-web\\src\\              main\\resources\\properties\\fdfs_client.conf";
  18. try {
  19. ClientGlobal.init(configFileName);
  20. }catch(Exception e){
  21. e.printStackTrace();
  22. }
  23. File file = new File("C:/Users/Public/Pictures/Sample Pictures/qie.jpg");
  24. //返回储存路径:group1 M00/00/00/wKhuW1Vmj6KAZ09pAAC9przUxEk788.jpg
  25. String[] files = uploadFile(file, "test.jpg", file.length());
  26. System.out.println(Arrays.asList(files));
  27. }
  28. /**
  29. * 上传文件
  30. */
  31. public static String[] uploadFile(File file, String uploadFileName, long fileLength) throws IOException {
  32. byte[] fileBuff = getFileBuffer(new FileInputStream(file), fileLength);
  33. String[] files = null;
  34. String fileExtName = "";
  35. if (uploadFileName.contains(".")) {
  36. fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
  37. } else {
  38. System.out.println("Fail to upload file, because the format of filename is illegal.");
  39. return null;
  40. }
  41.  
  42. // 建立连接
  43. TrackerClient tracker = new TrackerClient();
  44. TrackerServer trackerServer = tracker.getConnection();
  45. StorageServer storageServer = null;
  46. StorageClient client = new StorageClient(trackerServer, storageServer);
  47.  
  48. // 设置元信息
  49. NameValuePair[] metaList = new NameValuePair[3];
  50. metaList[0] = new NameValuePair("fileName", uploadFileName);
  51. metaList[1] = new NameValuePair("fileExtName", fileExtName);
  52. metaList[2] = new NameValuePair("fileLength", String.valueOf(fileLength));
  53.  
  54. // 上传文件
  55. try {
  56. files = client.upload_file(fileBuff, fileExtName, metaList);
  57. } catch (Exception e) {
  58. System.out.println("Upload file \"" + uploadFileName + "\"fails");
  59. }
  60. trackerServer.close();
  61. return files;
  62. }
  63. private static byte[] getFileBuffer(InputStream inStream, long fileLength) throws IOException {
  64.  
  65. byte[] buffer = new byte[256 * 1024];
  66. byte[] fileBuffer = new byte[(int) fileLength];
  67.  
  68. int count = 0;
  69. int length = 0;
  70.  
  71. while ((length = inStream.read(buffer)) != -1) {
  72. for (int i = 0; i < length; ++i) {
  73. fileBuffer[count + i] = buffer[i];
  74. }
  75. count += length;
  76. }
  77. return fileBuffer;
  78. }
  79.  
  80. }

程序的执行结果

  1. [group1, M00/00/00/wKgAb1dAU0WAQEhwAAvea_OGt2M139.jpg]

拼接之后就是图片的访问url了

http://192.168.0.111/group1/M00/00/00/wKgAb1dAU0WAQEhwAAvea_OGt2M139.jpg

FastDFS之java客户端使用的更多相关文章

  1. docker安装fastdfs与java客户端测试

    一.docker 安装FastDFS 1.拉取镜像 docker pull morunchang/fastdfs 2.创建并启动tracker容器 docker run -d --name=track ...

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

    参考博客:http://blog.csdn.net/xyang81/article/details/52847311 官网Java客户端源代码: https://github.com/happyfis ...

  3. FastDFS分布文件系统Java客户端使用

    原文链接:http://blog.csdn.net/xyang81/article/details/52847311 Java客户端源代码和jar:链接:http://pan.baidu.com/s/ ...

  4. FastDFS单机搭建以及java客户端Demo

    http://blog.csdn.net/u012453843/article/details/69951920 http://blog.csdn.net/xyang81/article/detail ...

  5. 从入门到精通(分布式文件系统架构)-FastDFS,FastDFS-Nginx整合,合并存储,存储缩略图,图片压缩,Java客户端

    导读 互联网环境中的文件如何存储? 不能存本地应用服务器 NFS(采用mount挂载) HDFS(适合大文件) FastDFS(强力推荐

  6. Java 客户端操作 FastDFS 实现文件上传下载替换删除

    FastDFS 的作者余庆先生已经为我们开发好了 Java 对应的 SDK.这里需要解释一下:作者余庆并没有及时更新最新的 Java SDK 至 Maven 中央仓库,目前中央仓库最新版仍旧是 1.2 ...

  7. 高可用高性能分布式文件系统FastDFS实践Java程序

    在前篇 高可用高性能分布式文件系统FastDFS进阶keepalived+nginx对多tracker进行高可用热备 中已介绍搭建高可用的分布式文件系统架构. 那怎么在程序中调用,其实网上有很多栗子, ...

  8. fastdfs5.x Java客户端简单例子

    下载源码, 使用maven编译并安装 https://github.com/happyfish100/fastdfs-client-java.git 新建maven工程,引入fastdfs-clien ...

  9. 由Memcached升级到 Couchbase的 Java 客户端的过程记录(一)

    背景: 在项目启动的选用了Memcached 作为缓存服务器,采用了Xmemcached作为客户端.在项目中使用了Shiro,为了给 Shiro 配置缓存的时候,采用了开源代码   https://g ...

随机推荐

  1. objective-c自学总结(二)---init/set/get方法

    一:类的声明和实现: 声明:(放在“类名+.h”文件中). 类的声明主要有两部分组成:实例变量和方法. 例 #import <Foundation/Foundation.h> @inter ...

  2. Android -- TouchEvent的分发和截获方式

    Android系统中的每个ViewGroup的子类都具有下面三个和TouchEvent处理密切相关的方法: public boolean dispatchTouchEvent(MotionEvent ...

  3. WEB相关文件的加载顺序

    一. 1.启动一个WEB项目,WEB容器会先去读取它的配置文件web.xml,读取<context-param>和<listener>两个节点. 2.接着,容器创建一个Serv ...

  4. JS 学习笔记--7---正则表达式

    正则表达式中的内容很多,也很深,下面只是一些基本的知识点,练习中使用的浏览器是IE10,若有不当处请各位朋友指正,我会在第一时间修改错误之处. 匹配的概念:是包含的意思,不是相等的意思 1.正则表达式 ...

  5. Poj2420 A Star not a Tree? 模拟退火算法

    题目链接:http://poj.org/problem?id=2420 题目大意:每组数据中给n个点(n<=100),求平面中一个点使得这个点到n个点的距离之和最小. 分析:一开始看到这个题想必 ...

  6. Winform 打印PDF顺序混乱,获取打印队列

    工作中PDF打印顺序混乱着实让我疼痛了好久,其实决绝方法非常简单,但没有想到这个点子的时候确实让我走了很多弯路 这里文章写出来并不是为了炫耀什么,只是觉得发现些好东西就分享出来而已,同时也做个记录,方 ...

  7. linux centos yum 安装 rar

    linux yum安装rar时,可能会出现无资源的错误,只需把配置好资源即可,具体操作如下: 1.# vi /etc/yum.repos.d/dag.repo 2.将以下内容写入文件中 [dag] n ...

  8. js获得浏览器页面高宽

    不同的浏览器可能会有一些差别,使用的时候请先进行测试. var s = ""; s += " 网页可见区域宽:"+ document.body.clientWi ...

  9. 在linux中使用phpize安装php扩展模块

    介绍:linux系统中,php安装成功后,在bin目录下会生成一个名叫phpize的可执行脚本,这个脚本的用途是动态安装php扩展模块.使用phpize脚本安装php扩展模块的好处:在安装php时没有 ...

  10. Swift-4-数组和字典

    // Playground - noun: a place where people can play import UIKit // 数组 字典 // 集合的可变性 赋值给var的集合是可变的mut ...