要想搭建fastDFS网上有相近的文章:

分布式文件系统 - FastDFS 在 CentOS 下配置安装部署

分布式文件系统 - FastDFS 配置 Nginx 模块及上传测试

首先下载fastdfs-client-java  jar包,我是下载fastdfs-client-java源码,自己打的jar包,fastdfs-client-java源码

首先创建java项目

创建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.206.139:22122 #对应自己的tracker服务IP地址

这里只测试了上传一张图片,然后下载这张图片

具体java代码:

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.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.junit.Test; public class FastDFS { @Test
public void upload(){
try {// 初始化全局配置。加载一个配置文件。
ClientGlobal.init("D:\\workspace-12306\\Test\\src\\test\\resources\\fdfs_client.conf");
// 创建一个TrackerClient对象。
TrackerClient trackerClient = new TrackerClient();
// 创建一个TrackerServer对象。
TrackerServer trackerServer = trackerClient.getConnection();
// 声明一个StorageServer对象,null。
StorageServer storageServer = null;
// 获得StorageClient对象。
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
// 直接调用StorageClient对象方法上传文件即可。
String[] strings;
strings = storageClient.upload_file("D:\\1.jpg", "jpg", null);
for (String string : strings) {
System.out.println(string);
}
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
} @Test
public void download(){
try {
// 初始化全局配置。加载一个配置文件
ClientGlobal.init("D:\\workspace-12306\\Test\\src\\test\\resources\\fdfs_client.conf");
// 创建一个TrackerClient对象
TrackerClient tracker = new TrackerClient();
// 创建一个TrackerServer对象。
TrackerServer trackerServer = tracker.getConnection();
// 声明一个StorageServer对象,
StorageServer storageServer = null;
// 获得StorageClient对象
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
byte[] b = storageClient.download_file("group1", "M00/00/00/wKjOi1gi0nOAcSo8AAA63pKOZZ0312.jpg");
System.out.println(b);
// 将下载的文件流保存
IOUtils.write(b, new FileOutputStream("D:/"+UUID.randomUUID().toString()+".jpg"));
} catch (Exception e) {
e.printStackTrace();
}
}
}

在开始测试时遇到一个异常:

connect to server 192.168.206.139:22122 fail
java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at org.csource.fastdfs.TrackerGroup.getConnection(TrackerGroup.java:47)
at org.csource.fastdfs.TrackerGroup.getConnection(TrackerGroup.java:72)
at org.csource.fastdfs.TrackerClient.getConnection(TrackerClient.java:58)
at FastDFS.upload(FastDFS.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
connect to server 192.168.206.139:22122 fail
java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at org.csource.fastdfs.TrackerGroup.getConnection(TrackerGroup.java:47)
at org.csource.fastdfs.TrackerGroup.getConnection(TrackerGroup.java:72)
at org.csource.fastdfs.TrackerClient.getConnection(TrackerClient.java:58)
at org.csource.fastdfs.TrackerClient.getStoreStorage(TrackerClient.java:91)
at org.csource.fastdfs.StorageClient.newWritableStorageConnection(StorageClient.java:1938)
at org.csource.fastdfs.StorageClient.do_upload_file(StorageClient.java:703)
at org.csource.fastdfs.StorageClient.upload_file(StorageClient.java:164)
at org.csource.fastdfs.StorageClient.upload_file(StorageClient.java:132)
at org.csource.fastdfs.StorageClient.upload_file(StorageClient.java:114)
at FastDFS.upload(FastDFS.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
org.csource.common.MyException: getStoreStorage fail, errno code: 0
at org.csource.fastdfs.StorageClient.newWritableStorageConnection(StorageClient.java:1941)
at org.csource.fastdfs.StorageClient.do_upload_file(StorageClient.java:703)
at org.csource.fastdfs.StorageClient.upload_file(StorageClient.java:164)
at org.csource.fastdfs.StorageClient.upload_file(StorageClient.java:132)
at org.csource.fastdfs.StorageClient.upload_file(StorageClient.java:114)
at FastDFS.upload(FastDFS.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

连接超时问题,这是由于linux没有开放22122端口和23000端口所致,我们可以关闭linux的防火墙iptables,但是不推荐这样做,会有安全问题,建议将这两个端口添加到防火墙开放端口中:

#关闭防火墙命令
service iptables stop
#将22111和23000端口添加到防火墙中
/sbin/iptables -I INPUT -p tcp --dport 22122 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 23000 -j ACCEPT #保存iptables
/etc/rc.d/init.d/iptables save #重启iptables
service iptables restart

配置完iptables,再测试上传就会成功,打印出上传信息:

可以将打印的上传信息在浏览器中查看是否上传成功:

在浏览器中能够正常显示,说明长传成功。

在D盘能够找到保存的图片,说明下载成功。

fastDFS 上传 java源码的更多相关文章

  1. 分享知识-快乐自己:FastDFS 上传 java 源码

    FastDFS 上传 java 源码:点我下载源码 首先导入 POM 文件:解决 maven 不能下载 fastdfs-client-java JAR <dependency> <g ...

  2. Hadoop之HDFS原理及文件上传下载源码分析(下)

    上篇Hadoop之HDFS原理及文件上传下载源码分析(上)楼主主要介绍了hdfs原理及FileSystem的初始化源码解析, Client如何与NameNode建立RPC通信.本篇将继续介绍hdfs文 ...

  3. php实现文件上传的源码

    php实现文件上传的源码,更多php技术开发就去php教程网,http://php.662p.com <?php ##author :Androidyue ##sina @androidyue ...

  4. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(32)-swfupload多文件上传[附源码]

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(32)-swfupload多文件上传[附源码] 文件上传这东西说到底有时候很痛,原来的asp.net服务器 ...

  5. swfupload多文件上传[附源码]

    swfupload多文件上传[附源码] 文件上传这东西说到底有时候很痛,原来的asp.net服务器控件提供了很简单的上传,但是有回传,还没有进度条提示.这次我们演示利用swfupload多文件上传,项 ...

  6. asp.net+swfupload 多图片批量上传(附源码下载)

    asp.net的文件上传都是单个文件上传方式,无法执行一次性多张图片批量上传操作,要实现多图片批量上传需要借助于flash,通过flash选取多个图片(文件),然后再通过后端服务进行上传操作. 本次教 ...

  7. Hadoop之HDFS原理及文件上传下载源码分析(上)

    HDFS原理 首先说明下,hadoop的各种搭建方式不再介绍,相信各位玩hadoop的同学随便都能搭出来. 楼主的环境: 操作系统:Ubuntu 15.10 hadoop版本:2.7.3 HA:否(随 ...

  8. ASP.NET MVC5+EF6+EasyUI 后台管理系统(32)-swfupload多文件上传[附源码]

    系列目录 文件上传这东西说到底有时候很痛,原来的asp.net服务器控件提供了很简单的上传,但是有回传,还没有进度条提示.这次我们演示利用swfupload多文件上传,项目上文件上传是比不可少的,大家 ...

  9. element-ui Upload 上传组件源码分析整理笔记(十四)

    简单写了部分注释,upload-dragger.vue(拖拽上传时显示此组件).upload-list.vue(已上传文件列表)源码暂未添加多少注释,等有空再补充,先记下来... index.vue ...

随机推荐

  1. mysqlimport导入简单测试

    1    创建一个文本文档:[mysql@xxxycrdb]$ more /tmp/ldcmd1.txt 1,abc,abc@qq.com1,abc,abc@qq.com1,abc,abc@qq.co ...

  2. HTTPS科普(转)

    为什么需要https HTTP是明文传输的,也就意味着,介于发送端.接收端中间的任意节点都可以知道你们传输的内容是什么.这些节点可能是路由器.代理等. 举个最常见的例子,用户登陆.用户输入账号,密码, ...

  3. 【洛谷5280】[ZJOI2019] 线段树(线段树大力分类讨论)

    点此看题面 大致题意: 给你一棵线段树,两种操作.一种操作将每棵线段树复制成两个,然后在这两个线段树中的一个上面进行\(Modify(l,r)\).另一种操作询问所有线段树的\(tag\)总和. 大力 ...

  4. 2016 China Collegiate Programming Contest Final

    2016 China Collegiate Programming Contest Final Table of Contents 2016 China Collegiate Programming ...

  5. 随机森林算法-Deep Dive

    0-写在前面 随机森林,指的是利用多棵树对样本进行训练并预测的一种分类器.该分类器最早由Leo Breiman和Adele Cutler提出.简单来说,是一种bagging的思想,采用bootstra ...

  6. sudo: Sorry, you must have a tty to run

    The requiretty option in sudoers file The requiretty if set in sudo config file sudoers, sudo will o ...

  7. Hibernate的优缺点:

    1.Hibernate的优缺点:优点:1.程序更加面向对象:2.提高了生产率:3.方便移植(修改配置文件):4.无侵入性.缺点:1.效率比JDBC略差:2.不适合批量操作. 总的来说,hibernat ...

  8. 【洛谷P3627】[APIO2009]抢掠计划

    抢掠计划 题目链接 比较水的缩点模板题,Tarjan缩点,重新建图,记录联通块的钱数.是否有酒吧 DAG上记忆化搜索即可 #include<iostream> #include<cs ...

  9. linux 使用sqlite3

    :c中使用sqlite3需要调用函数接口操作: sqlite3 *db; int status=sqlite_open("dbname",&db);//打开或者创建数据库 ...

  10. 如何关闭Eclipse智障的变量命名自动补全功能

    找到了最最完美的解决方案,无需下载源码. 详见:https://zhidao.baidu.com/question/1451659429285222820.html