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

FastDFS 上传 java 源码:点我下载源码

首先导入 POM 文件:解决 maven 不能下载 fastdfs-client-java JAR

<dependency>
    <groupId>org.csource</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27-SNAPSHOT</version>
</dependency>

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

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

或者使用:fastdfs.properties

#FastDFS配置begin-----------除了fastdfs.tracker_servers,其它配置项都是可选的
fastdfs.connect_timeout_in_seconds=5
fastdfs.network_timeout_in_seconds=30
fastdfs.charset=UTF-8
fastdfs.http_anti_steal_token=false
fastdfs.http_secret_key=FastDFS1234567890
fastdfs.http_tracker_http_port=80
fastdfs.tracker_servers=192.168.31.202:22122
#FastDFS配置end-----------

这里只测试了上传一张图片,然后下载这张图片;具体java代码:

package com.gdbd;

import org.apache.commons.io.IOUtils;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.junit.jupiter.api.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * @author asus
 */
public class FastdfsUpload {

    /***
     * 图片文件上传
     */
    @Test
    public void upload() {
        try {
            // 初始化全局配置。加载一个配置文件。
            //ClientGlobal.init("fdfs_client.conf");
            ClientGlobal.initByProperties("fastdfs.properties");
            // 创建一个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("imgs/002.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("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/wKgfylwIyoOATXT_AAA08M-EhgM3350.jpg");
            System.out.println(b);
            if (b != null) {
                // 将下载的文件流保存
                IOUtils.write(b, new FileOutputStream("DownLoadFile/" + 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)
View Code

连接超时问题,这是由于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,再测试上传就会成功,打印出上传信息:

 

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

 

 下载去指定文件夹中查看是否存在:

 

posted @ 2018-12-06 14:37  GDBD  阅读(395)  评论(0编辑  收藏  举报