FastDFS Java Client

前面一文描述了如何部署FastDFS服务器,本文在此基础上,介绍如何利用FastDFS的Java客户端与服务器交互完成文件管理

FastDFS系列

  • FastDFS服务器部署
  • FastDFS Java Client

本地安装

git clone https://github.com/happyfish100/fastdfs-client-java.git
cd fastdfs-client-java
mvn clean install

添加依赖

pom.xml


    org.csource
    fastdfs-client-java
    1.27-SNAPSHOT

配置FastDFS

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

配置加载

ClientGlobal.initByProperties("fastdfs-client.properties");
String configInfo = ClientGlobal.configInfo();
System.out.println(configInfo);

FastDFS API

  • 文件上传:upload_file
  • 文件下载:download_file
  • 文件信息:get_file_info
  • 用户自定义元信息:get_metadata
  • 删除文件:delete_file
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.junit.Assert;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class TestFastDfs {
    private static final String FASTDFS_CLIENT_PROPERTIES = "fastdfs-client.properties";
    private static String dfsFileName = "M00/00/00/wKg6ZVyM1dKAeC7_AAADxVvVFj019.java";

    @Test
    public void testUpload() throws Exception {    //上传文件
        TrackerServer trackerServer = null;
        try {
            ClientGlobal.initByProperties(FASTDFS_CLIENT_PROPERTIES);
            TrackerClient tracker = new TrackerClient();
            trackerServer = tracker.getConnection();
            String local_filename = "F://Debug.java";

            StorageClient storageClient = new StorageClient(trackerServer, null);
            NameValuePair valuePair = new NameValuePair();
            valuePair.setName("name");
            valuePair.setValue("tenmao test");
            NameValuePair[] pairs = {valuePair};
            String[] fileIds = storageClient.upload_file(local_filename, null, pairs);

            System.out.println("组名:" + fileIds[0]);
            System.out.println("路径: " + fileIds[1]);
        } finally {
            try {
                if (null != trackerServer) {
                    trackerServer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void testDownload() throws Exception {    //下载文件
        TrackerServer trackerServer = null;

        try {
            String groupName = "group1";
            String filePath = dfsFileName;
            ClientGlobal.initByProperties(FASTDFS_CLIENT_PROPERTIES);

            TrackerClient tracker = new TrackerClient();
            trackerServer = tracker.getConnection();

            StorageClient storageClient = new StorageClient(trackerServer, null);
            byte[] bytes = storageClient.download_file(groupName, filePath);

            Assert.assertNotNull(bytes);
            String storePath = "F://hello.java";
            try (OutputStream out = new FileOutputStream(storePath)) {
                out.write(bytes);
            }
        } finally {
            try {
                if (null != trackerServer) trackerServer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void testGetFileInfo() { //获取文件信息
        TrackerServer trackerServer = null;

        try {
            String groupName = "group1";
            String filePath = dfsFileName;
            ClientGlobal.initByProperties(FASTDFS_CLIENT_PROPERTIES);

            TrackerClient tracker = new TrackerClient();
            trackerServer = tracker.getConnection();

            StorageClient storageClient = new StorageClient(trackerServer, null);
            FileInfo file = storageClient.get_file_info(groupName, filePath);
            System.out.println("ip--->" + file.getSourceIpAddr());
            System.out.println("文件大小--->" + file.getFileSize());
            System.out.println("文件上传时间--->" + file.getCreateTimestamp());
            System.out.println(file.getCrc32());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != trackerServer) trackerServer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void testGetFileMate() throws Exception { //获取文件的原数据类型
        TrackerServer trackerServer = null;

        try {
            String groupName = "group1";
            String filePath = dfsFileName;
            ClientGlobal.initByProperties(FASTDFS_CLIENT_PROPERTIES);

            TrackerClient tracker = new TrackerClient();
            trackerServer = tracker.getConnection();

            StorageClient storageClient = new StorageClient(trackerServer, null);

            //这个值是上传的时候指定的NameValuePair
            NameValuePair[] pairs = storageClient.get_metadata(groupName, filePath);
            if (null != pairs && pairs.length > 0) {
                for (NameValuePair nvp : pairs) {
                    System.out.println(nvp.getName() + ":" + nvp.getValue());
                }
            }
        } finally {
            try {
                if (null != trackerServer) trackerServer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void testDelete() throws Exception { //删除文件
        TrackerServer trackerServer = null;

        try {
            String groupName = "group1";
            String filePath = dfsFileName;
            ClientGlobal.initByProperties(FASTDFS_CLIENT_PROPERTIES);

            TrackerClient tracker = new TrackerClient();
            trackerServer = tracker.getConnection();

            StorageClient storageClient = new StorageClient(trackerServer, null);
            int i = storageClient.delete_file(groupName, filePath);
            System.out.println(i == 0 ? "删除成功" : "删除失败:" + i);
        } finally {
            try {
                if (null != trackerServer) trackerServer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

参考

  • FastDFS java client SDK
  • FastDFS Java Api 操作

你可能感兴趣的:(FastDFS Java Client)