FastDFS java 整合实例

Posted Doker 多克

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FastDFS java 整合实例相关的知识,希望对你有一定的参考价值。

FastDFS提供的三个主要功能

  • upload:上传文件
  • download:下载文件
  • delete:删除文件

一、开发环境准备

1、使用maven从源码安装

mvn clean install

2、使用maven从jar文件安装

mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java -Dversion=$version -Dpackaging=jar -Dfile=fastdfs-client-java-$version.jar

3、在pom.xml中添加依赖

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

4、配置fdfs_client.conf文件

文件所在位置可以是项目classpath(或OS文件系统目录比如/opt/): /opt/fdfs_client.conf C:\\Users\\James\\config\\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 = 10.0.11.247:22122
tracker_server = 10.0.11.248:22122
tracker_server = 10.0.11.249:22122

connection_pool.enabled = true
connection_pool.max_count_per_entry = 500
connection_pool.max_idle_time = 3600
connection_pool.max_wait_time_in_ms = 1000
注1:tracker_server指向您自己IP地址和端口,1-n个
注2:除了tracker_server,其它配置项都是可选的

5、在项目中配置 fastdfs-client.properties

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 = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122

fastdfs.connection_pool.enabled = true
fastdfs.connection_pool.max_count_per_entry = 500
fastdfs.connection_pool.max_idle_time = 3600
fastdfs.connection_pool.max_wait_time_in_ms = 1000

 注1:properties 配置文件中属性名跟 conf 配置文件不尽相同,并且统一加前缀"fastdfs.",便于整合到用户项目配置文件
注2:fastdfs.tracker_servers 配置项不能重复属性名,多个 tracker_server 用逗号","隔开
注3:除了fastdfs.tracker_servers,其它配置项都是可选的

二、开发实例

public class FdfsTest 

    private static final Logger LOGGER = LoggerFactory.getLogger(FdfsTest.class);

    private static final String CONF_NAME = "fastdfs-client.properties";

    private StorageClient storageClient;

    private TrackerServer trackerServer;

    @Before
    public void initStorageClient() throws Exception 
        ClientGlobal.init(CONF_NAME);
        LOGGER.info("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
        LOGGER.info("charset=" + ClientGlobal.g_charset);
        TrackerClient tracker = new TrackerClient();
        trackerServer = tracker.getTrackerServer();
        StorageServer storageServer = null;
        storageClient = new StorageClient(trackerServer, storageServer);
    

    @After
    public void closeClient() 
        LOGGER.info("close connection");
        if(storageClient != null)
            try 
               storageClient.close();
            catch (Exception e)
                e.printStackTrace();
            catch (Throwable e)
                e.printStackTrace();
            
        
    

    public void writeByteToFile(byte[] fbyte, String fileName) throws IOException 
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = new File(fileName);
        try 
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(fbyte);
         catch (Exception e) 
            e.printStackTrace();
         finally 
            if (bos != null) 
                bos.close();
            
            if (fos != null) 
                fos.close();
            
        
    

    
    public void upload() throws Exception
        NameValuePair[] metaList = new NameValuePair[1];
        String local_filename = "build.PNG";
        metaList[0] = new NameValuePair("fileName", local_filename);
        File file = new File("C:/Users/chengdu/Desktop/build.PNG");
        InputStream inputStream = new FileInputStream(file);
        int length = inputStream.available();
        byte[] bytes = new byte[length];
        inputStream.read(bytes);
        String[] result = storageClient.upload_file(bytes, null, metaList);
        LOGGER.info("result ", Arrays.asList(result));
        Assert.assertEquals(2, result.length);
    

   
    public void download() throws Exception 
        String[] uploadresult = "group1", "M00/00/00/J2fL12PVypeAWiGcAAM_gDeWVyw5817085";
        byte[] result = storageClient.download_file(uploadresult[0], uploadresult[1]);
        String local_filename = "commitment.d2f57e10.jpg";
        writeByteToFile(result, local_filename);
        File file = new File(local_filename);
        Assert.assertTrue(file.isFile());
    

    
    public void testUploadDownload() throws Exception 
        NameValuePair[] metaList = new NameValuePair[1];
        String local_filename = "commitment.d2f57e10 (2).jpg";
        metaList[0] = new NameValuePair("fileName", local_filename);
        File file = new File("/Users/iyw/Downloads/commitment.d2f57e10 (2).jpg");
        InputStream inputStream = new FileInputStream(file);
        int length = inputStream.available();
        byte[] bytes = new byte[length];
        inputStream.read(bytes);
        String[] result = storageClient.upload_file(bytes, null, metaList);
        //Assert.assertTrue(storageClient.isConnected());
        // pool testOnborrow  isAvaliable
       // Assert.assertTrue(storageClient.isAvaliable());
        LOGGER.info("result ", Arrays.asList(result));
        byte[] resultbytes = storageClient.download_file(result[0], result[1]);
        writeByteToFile(resultbytes, local_filename);
        File downfile = new File(local_filename);
        Assert.assertTrue(downfile.isFile());
    

FastDFS - 文件服务器学习资料

FastDFS搭建及java整合代码

CentOS 6.5下 FastDFS结合Nginx插件实现图片http访问

图片服务器fastDFS的搭建以及配置

nginx fastdfs 配置后 上传成功 但访问报404 bad request 两种解决方法

使用FastDFS搭建图片服务器单实例篇

使用FastDFS搭建图片服务器(单实例)

 

fastdfs 环境搭建使用及最佳实践

分布式文件系统FastDFS架构剖析

 

以上是关于FastDFS java 整合实例的主要内容,如果未能解决你的问题,请参考以下文章

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

fastDFS与java整合文件上传下载

FastDFS - 文件服务器学习资料

FastDFS整合普通Maven项目

java互联网架构

fastdfs分布式文件系统之与dubbo整合实现分布式服务接口