分布式文件服务器FastDFS入门小案例

xl_echo编辑整理,欢迎转载,转载请声明文章来源。更多IT、编程案例、资料请联系QQ:1280023003
百战不败,依不自称常胜,百败不颓,依能奋力前行。——这才是真正的堪称强大!!


前言:
本案例不提供fastDFS的服务安装,一下是调用服务实现步骤

第一步:创建Maven工程,使用jar的方式即可

第二步:在pom.xml文件中导入fastDFS依赖(注意:Maven本地仓库一般没有FastDFS的jar包,所以,需要手动安装mvn install:install-file -DgroupId=org.csource.fastdfs -DartifactId=fastdfs -Dversion=1.2
-Dpackaging=jar -Dfile=d:\setup\fastdfs_client_v1.20.jar)

<dependency>
        <groupId>org.csource.fastdfsgroupId>
        <artifactId>fastdfsartifactId>
        <version>1.2version>
dependency>

第三步:导入配置文件,用于连接tracker_server。配置文件详情

# connect timeout in seconds
# default value is 30s
connect_timeout=30

# network timeout in seconds
# default value is 30s
network_timeout=60

# the base path to store log files
base_path=/home/fastdfs

# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
tracker_server=192.168.25.133:22122

#standard log level as syslog, case insensitive, value list:
### emerg for emergency
### alert
### crit for critical
### error
### warn for warning
### notice
### info
### debug
log_level=info

# if use connection pool
# default value is false
# since V4.05
use_connection_pool = false

# connections whose the idle time exceeds this time will be closed
# unit: second
# default value is 3600
# since V4.05
connection_pool_max_idle_time = 3600

# if load FastDFS parameters from tracker server
# since V4.05
# default value is false
load_fdfs_parameters_from_tracker=false

# if use storage ID instead of IP address
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# default value is false
# since V4.05
use_storage_id = false

# specify storage ids filename, can use relative or absolute path
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# since V4.05
storage_ids_filename = storage_ids.conf


#HTTP settings
http.tracker_server_port=80

#use "#include" directive to include HTTP other settiongs
##include http.conf

第三步:编写一个测试类Test

第四步:环境就是类的编写了,基本的代码相对固定,只需要记住以下几个步骤即可

  • 加载配置文件
  • 构建一个管理者客户端
  • 链接管理者客户端
  • 声明存储的服务端
  • 获取服务器的客户端对象
  • 上传文件
  • 显示上传结果

代码展示例:

package com.echo.fast;

import java.io.FileNotFoundException;
import java.io.IOException;

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;

public class Test {

    public static void main(String[] args) throws FileNotFoundException, IOException, Exception {

        // 1、加载配置文件
        ClientGlobal.init("H:\\developEclipse\\fastFSD\\src\\main\\resources\\fdfs_client.conf");

        // 2、构建一个管理者客户端
        TrackerClient client = new TrackerClient();

        // 3、连接管理者服务端
        TrackerServer trackerServer = client.getConnection();

        // 4、声明存储的服务端
        StorageServer storageServer = null;

        // 5、获取服务器的客户端对象
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);

        // 6、上传文件
        String[] strings = storageClient.upload_file("D:\\imageTemp\\1.jpg", "jpg", null);


        StringBuilder s = new StringBuilder();
        // 7、显示上传结果
        for(String str : strings){
            s.append(str + "/");
        }

        //打印出来就是访问地址
        System.out.println("http://192.168.25.133/" + s.substring(0, s.length()-1));
    }

}

第五步:在浏览器打印的访问地址(地址示例:http://192.168.25.133/group1/M00/00/00/wKgZhVrcA7GAA2nDAAGUn2ObOZI781.jpg)如果能够访问到,证明已经上传成功

你可能感兴趣的:(技术介绍)