即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

Maven_Fastdfs 实例

编程语言 zhiguoliu11 12℃ 0评论

环境:Vmware Workstation 10,CentOS-7-x86_64-DVD-1511.iso,Xshell 4.0,ip:192.168.216.140.

这里写图片描述

org.csource.common和org.csource.fastdfs两个包来自于fastdfs-client-java

fdfs_client.conf

connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890

tracker_server = 192.168.216.140:22122

FastdfsTest.java

package com.demo.fastdfs.test;

import java.io.IOException;

import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class FastdfsTest {
    private static final String upload_file = "D:\\tortoise.jpg";
    private StorageClient1 client;
    private TrackerServer trackerServer;

    @Before
    public void init() throws IOException, MyException {
        ClientGlobal.init("fdfs_client.conf");

        trackerServer = new TrackerClient().getConnection();
        client = new StorageClient1(trackerServer, null);
    }

    @Test
    public void fdfs() throws IOException, MyException {

        NameValuePair[] metaList = new NameValuePair[1];
        metaList[0] = new NameValuePair("fileName", upload_file);

        // 上传文件
        String fileId = client.upload_file1(upload_file, null, metaList);
        System.out.println("upload success. file id is: " + fileId);

        // 下载文件
        byte[] result = client.download_file1(fileId);
        System.out.println("download result is: " + result.length);

        // 删除文件
        int code = client.delete_file1(fileId);
        System.out.println("0 for success, none zero for fail (error code): " + code);

        closeFastdfs();
    }

    @After
    public void closeFastdfs() throws IOException {
        if (trackerServer != null) {
            trackerServer.close();
        }

    }

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>com.demogroupId>
    <artifactId>maven-fastdfsartifactId>
    <version>0.0.1-SNAPSHOTversion>

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>
    dependencies>

project>

maven-fastfds 实例源码

转载请注明:CodingBlog » Maven_Fastdfs 实例

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情