Mr. Yu Qing, the author of FastDFS, has developed a Corresponding SDK for Java for us. Some clarification: Author Yu Qing did not update the latest Java SDK to Maven’s central repository, which is still version 1.27. So we need to go to Github: github.com/happyfish10… Download the project source code, and then run the command MVN clean install to compile the package and import the Maven local repository for use.

Next, we use Java API operation FastDFS to achieve file upload, download, replace, delete, query metadata, query details and other functions.

The cases in this paper have been synchronized to:

  • Github:github.com/imrhellowor…
  • Gitee:gitee.com/imrhellowor…

  

Create a project

  

  

Add the dependent

  

Add the following dependencies to the project’s POM.xml. Because we need some common toolkits and unit tests, we need to introduce them.

<! -- fastdfs java client -->
<dependency>
    <groupId>org.csource</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.29 the SNAPSHOT</version>
</dependency>
<! Apache Commons Lang3 toolkit -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.11</version>
</dependency>
<! -- Junit Unit Test -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
    <scope>test</scope>
</dependency>
Copy the code

  

Writing configuration files

  

fdfs_client.conf

Connect_timeout = 10 network_timeout = 30 charset = UTF-8 # tracker Specifies the server port exposed under the HTTP protocol Tracker_http_port = 8080 # tracker_server = 192.168.10.101:22122Copy the code

  

Utility class

  

package org.example.client;

import org.apache.commons.lang3.StringUtils;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

import java.io.*;

/** * FastDFS Java client tools for distributed file system */
public class FastDFSClient {

    // Get the configuration file address
    private static final String CONF_FILENAME = Thread.currentThread()
            .getContextClassLoader().getResource("").getPath() + "fdfs_client.conf";
    // Storage Storage server client
    private static StorageClient storageClient = null;

    static {
        try {
            // Load the configuration file
            ClientGlobal.init(CONF_FILENAME);
            // Initialize the Tracker client
            TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
            // Initialize the Tracker server
            TrackerServer trackerServer = trackerClient.getTrackerServer();
            // Initialize the Storage server
            StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
            // Initialize the Storage client
            storageClient = new StorageClient(trackerServer, storageServer);
        } catch (IOException e) {
            e.printStackTrace();
        } catch(MyException e) { e.printStackTrace(); }}/** * File upload **@paramInputStream the byte inputStream of the uploaded file *@paramFileName Specifies the original name of the uploaded file *@return* /
    public static String[] uploadFile(InputStream inputStream, String fileName) {
        try {
            // Prepare the byte array
            byte[] fileBuff = null;
            // File metadata
            NameValuePair[] metaList = null;
            if(inputStream ! =null) {
                // Check the length of the file
                int len = inputStream.available();
                // Initialize the metadata array
                metaList = new NameValuePair[2];
                // The first set of metadata, the original name of the file
                metaList[0] = new NameValuePair("file_name", fileName);
                // The second set of metadata, the length of the file
                metaList[1] = new NameValuePair("file_length", String.valueOf(len));
                // Create an array of bytes of the corresponding length
                fileBuff = new byte[len];
                // Read bytes from the input stream into the byte array
                inputStream.read(fileBuff);
            }
            /* Upload file. Parameter meanings: The content of the file to be uploaded (passed using a byte array), the type of the file to be uploaded (extension), and the metadata */
            String[] fileids = storageClient.upload_file(fileBuff, getFileExt(fileName), metaList);
            return fileids;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return null;
    }

    /** * File upload **@paramFile Uploaded file *@paramFileName Specifies the original name of the uploaded file *@return* /
    public static String[] uploadFile(File file, String fileName) {
        try (FileInputStream fis = new FileInputStream(file)) {
            return uploadFile(fis, fileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /** * Gets the file name extension (without a dot) **@param fileName
     * @returnFor example: "JPG" or "*/
    private static String getFileExt(String fileName) {
        if(StringUtils.isBlank(fileName) || ! fileName.contains(".")) {
            return "";
        }
        return fileName.substring(fileName.lastIndexOf(".") + 1); // Without the last dot
    }

    /** * Get file details **@paramGroupName Group/volume name. Default value: group1 *@paramRemoteFileName filename, for example: "M00/00/00 / wKgKZl9tkTCAJAanAADhaCZ_RF0495. JPG" *@returnFile details */
    public static FileInfo getFileInfo(String groupName, String remoteFileName) {
        try {
            return storageClient.get_file_info(groupName == null ? "group1" : groupName, remoteFileName);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return null;
    }

    /** * get metadata **@paramGroupName Group/volume name. Default value: group1 *@paramRemoteFileName filename, for example: "M00/00/00 / wKgKZl9tkTCAJAanAADhaCZ_RF0495. JPG" *@returnFile metadata array */
    public static NameValuePair[] getMetaData(String groupName, String remoteFileName) {
        try {
            // Obtain the metadata array of the file from the Storage client based on the group name and file name
            return storageClient.get_metadata(groupName == null ? "group1" : groupName, remoteFileName);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return null;
    }

    /** * File download **@paramGroupName Group/volume name. Default value: group1 *@paramRemoteFileName filename, for example: "M00/00/00 / wKgKZl9tkTCAJAanAADhaCZ_RF0495. JPG" *@returnFile's byte input stream */
    public static InputStream downloadFile(String groupName, String remoteFileName) {
        try {
            // Get the byte array of the file from the Storage client based on the group name and filename
            byte[] bytes = storageClient.download_file(groupName == null ? "group1" : groupName, remoteFileName);
            // Returns the byte stream object
            InputStream inputStream = new ByteArrayInputStream(bytes);
            return inputStream;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return null;
    }

    /** * Delete file **@paramGroupName Group/volume name. Default value: group1 *@paramRemoteFileName filename, for example: "M00/00/00 / wKgKZl9tkTCAJAanAADhaCZ_RF0495. JPG" *@return0 indicates success; non-0 indicates failure */
    public static int deleteFile(String groupName, String remoteFileName) {
        int result = -1;
        try {
            // Delete a file from the Storage client based on the group name and file name
            result = storageClient.delete_file(groupName == null ? "group1" : groupName, remoteFileName);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return result;
    }

    /** * Modifies an existing file **@paramOldGroupName oldGroupName *@paramOldFileName oldFileName *@paramFile New file *@paramFileName indicates a new fileName *@return* /
    public static String[] modifyFile(String oldGroupName, String oldFileName, File file, String fileName) {
        / / upload first
        String[] fileids = uploadFile(file, fileName);
        if (fileids == null) {
            return null;
        }
        / / delete again
        int delResult = deleteFile(oldGroupName, oldFileName);
        if(delResult ! =0) {
            return null;
        }
        returnfileids; }}Copy the code

  

test

  

File upload

  

// File upload
@Test
public void testUploadFile(a) {
    String[] fileids = FastDFSClient.uploadFile(new File("D:/china.jpg"), "china.jpg");
    for (String fileid : fileids) {
        System.out.println("fileid = "+ fileid); }}Copy the code

The return value:

fileid = group1
fileid = M00/00/00/wKgKZl9xMdiAcOLdAADhaCZ_RF0096.jpg
Copy the code

  

File for details

  

// View file details
@Test
public void testGetFileInfo(a) {
    FileInfo fileInfo = FastDFSClient.getFileInfo("group1"."M00/00/00/wKgKZl9xMdiAcOLdAADhaCZ_RF0096.jpg");
    System.out.println("fileInfo = " + fileInfo);
}
Copy the code

The return value:

FileInfo = fetch_froM_server = false, file_type = 1, source_IP_ADDR = 192.168.10.102, file_size = 57704, create_timestamp = 2020-09-28 08:44:08, crc32 = 645874781Copy the code

  

File metadata

  

// Get file data
@Test
public void testGetMetaData(a) {
    NameValuePair[] metaDatas = FastDFSClient.getMetaData("group1"."M00/00/00/wKgKZl9xMdiAcOLdAADhaCZ_RF0096.jpg");
    for (NameValuePair metaData : metaDatas) {
        System.out.println(metaData.getName() + "-"+ metaData.getValue()); }}Copy the code

The return value:

file_length---57704
file_name---china.jpg
Copy the code

  

File download

  

// File download
@Test
public void testDownloadFile(a) {
    InputStream is = FastDFSClient.downloadFile("group1"."M00/00/00/wKgKZl9xMdiAcOLdAADhaCZ_RF0096.jpg");
    try (FileOutputStream fos = new FileOutputStream("D:/wKgKZl9xMdiAcOLdAADhaCZ_RF0096.jpg")) {
        int len = 0;
        byte[] bytes = new byte[1024];
        while((len = is.read(bytes)) ! = -1) {
            fos.write(bytes, 0, len); fos.flush(); }}catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch(IOException e) { e.printStackTrace(); }}Copy the code

  

File deletion

  

// Delete files
@Test
public void testDeleteFile(a) {
    int result = FastDFSClient.deleteFile("group1"."M00/00/00/wKgKZl9xMdiAcOLdAADhaCZ_RF0096.jpg");
    System.out.println("result = " + result);
}
Copy the code

The return value:

result = 0
Copy the code

  

File to replace

  

// File replacement
@Test
public void testModifyFile(a) {
    String[] fileids = FastDFSClient.modifyFile("group1"."M00/00/00/wKgKZl9xOS2ASdu8AADhaCZ_RF0898.jpg".new File("D:/mhw.jpg"), "mhw.jpg");
    for (String fileid : fileids) {
        System.out.println("fileid = "+ fileid); }}Copy the code

The return value:

fileid = group1
fileid = M00/00/00/wKgKZl9xOeaAFO00AACmo7QBGtA298.jpg
Copy the code

So far the Java client FastDFS operation to achieve file upload, download, replace and delete operations to here, next we take you to build FastDFS cluster environment, multiple Tracker multiple Storage and then through the Nginx agent.

This article is licensed under a Creative Commons attribution – Noncommercial – No Deductive 4.0 International license.

You can check out more FastDFS articles in the category below.

  

🤗 your likes and retweets are the biggest support for me.

📢 follow the public account Mr. Hello Ward “document + video” each article is equipped with a special video explanation, learning more easily oh ~