docker安裝fastdfs與java客戶端測試


一、docker 安裝FastDFS

1、拉取鏡像

docker pull morunchang/fastdfs

2、創建並啟動tracker容器

docker run -d --name=tracker -v /home/fastdfs_docker/fdfs/tracker:/data/fast_data --privileged=true --net=host morunchang/fastdfs sh tracker.sh

 

3、創建並啟動storage容器、此處只做單機版測試

     注意:由於tracker容器使用host網絡模式、與宿主公用network namespace, 因此tracker容器ip與宿主機ip一致

docker run -d --name=storage -v /home/fastdfs_docker/fdfs/storage_data:/data/fast_data --privileged=rue --net=host -e TRACKER_IP=[宿主機ip]:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh

 

 

二、java客戶端測試

1、創建maven測試工程

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>xc-framework-parent</artifactId>
        <groupId>com.dehigher</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../xc-framework-parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test-fastdfs</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java -->
        <dependency>
            <groupId>net.oschina.zcx7878</groupId>
            <artifactId>fastdfs-client-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
        </dependency>
    </dependencies>

</project>

 

 

2、在classpath:config下創建fastdfs-client.properties文件

fastdfs-client.properties

fastdfs.connect_timeout_in_seconds = 5  #http連接超時時間
fastdfs.network_timeout_in_seconds = 60 #tracker 與 storage 通信連接超時時間
fastdfs.charset = UTF-8 #字符編碼
fastdfs.tracker_servers = [tracker_server_ip]:22122 #tracker_server_ip

 

3、上傳文件測試

    /**
     * 上傳文件
     * @throws Exception
     */
    @Test
    public void testUpload() throws Exception{
        
        ClientGlobal.initByProperties("config/fastdfs-client.properties");
        System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
        System.out.println("charset=" + ClientGlobal.g_charset);
        //創建客戶端
        TrackerClient tc = new TrackerClient();
        //連接tracker Server
        TrackerServer ts = tc.getConnection();
        if (ts == null) {
        System.out.println("getConnection return null");
        return;
        } 
        //獲取一個storage server
        StorageServer ss = tc.getStoreStorage(ts);
        if (ss == null) {
        System.out.println("getStoreStorage return null");
        } 
        //創建一個storage存儲客戶端
        StorageClient1 sc1 = new StorageClient1(ts, ss);
        NameValuePair[] meta_list = null; //new NameValuePair[0];
        String item = "C:\\Users\\degao\\Pictures\\111.png";
        String fileid;
        fileid = sc1.upload_file1(item, "png", meta_list);
        System.out.println("Upload local file " + item + " ok, fileid=" + fileid);
    }

 

 

4、查詢文件信息測試

    /**
     * 查詢文件
     * @throws Exception
     */
    @Test
    public void testQueryFile() throws Exception{
        ClientGlobal.initByProperties("config/fastdfs-client.properties");
        TrackerClient tracker = new TrackerClient();
        TrackerServer trackerServer = tracker.getConnection();
        StorageServer storageServer = null;
        StorageClient storageClient = new StorageClient(trackerServer,
        storageServer);
        FileInfo fileInfo = storageClient.query_file_info("group1",
                "M00/00/00/rBsAAlwM58eAcFPfAAFs5SjEXRM444.png");
        System.out.println(fileInfo);
    }

 

 

5、下載文件測試

    /**
     * 下載文件
     */
    @Test
    public void testDownloadFile() throws Exception {
    ClientGlobal.initByProperties("config/fastdfs-client.properties");
    TrackerClient tracker = new TrackerClient();
    TrackerServer trackerServer = tracker.getConnection();
    StorageServer storageServer = null;
    StorageClient1 storageClient1 = new StorageClient1(trackerServer,
    storageServer);
    byte[] result = storageClient1.download_file1("group1/M00/00/00/rBsAAlwM58eAcFPfAAFs5SjEXRM444.png");
    File file = new File("d:/1.png");
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    fileOutputStream.write(result);
    fileOutputStream.close();
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM