FastDFS java api調用

介紹

本文講述如何通過java調用FastDFS的文件上傳、下載及刪除,提供示例代碼。

 

編譯fastdfs-client-java

需要環境git、jdk8、maven

git clone https://github.com/happyfish100/fastdfs-client-java.git

cd fastdfs-client-java/

mvn package

 

配置

target目錄中就會產生fastdfs-client-java-x.jar文件

複製該文件到項目中的src/main/resources/lib目錄中

添加maven依賴

        <dependency>
            <groupId>org.csource</groupId>
            <artifactId>fastdfs-client-java</artifactId>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/fastdfs-client-java-1.27-SNAPSHOT.jar</systemPath>
            <version>1.27-SNAPSHOT</version>
        </dependency>

 

resources配置目錄下添加配置文件fdfs_client.conf

tracker_server=192.168.81.143:22122

#注意ip和端口要指向服務器的fastdfs服務,如果有防火牆的要做好開放處理

 

代碼實現

 

-------FastDFSHelper.java

import lombok.extern.slf4j.Slf4j;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

@Slf4j
public final class FastDFSHelper {
    private static TrackerClient trackerClient;

    static {
        try {
            ClientGlobal.init("fdfs_client.conf");
            trackerClient = new TrackerClient();
        } catch (IOException | MyException e) {
            log.error("error", e);
        }
    }

    /**
     * 向FastDFS上傳文件
     *
     * @param localFilename 本地文件名
     * @return 上傳成功,返回組名和該文件在FastDFS中的名稱;上傳失敗,返回null
     */
    public static String[] uploadFile(String localFilename) {
        TrackerServer trackerServer;
        try {
            trackerServer = trackerClient.getConnection();
        } catch (IOException e) {
            log.error("error", e);
            return null;
        }
        StorageClient storageClient = new StorageClient(trackerServer, null);
        try {
            String[] arr = storageClient.upload_file(localFilename, null, null);
            if (arr == null || arr.length != 2) {
                log.error("向FastDFS上傳文件失敗");
            } else {
                log.info("向FastDFS上傳文件成功");
                log.info("id:{}/{}",arr);
                return arr;
            }
        } catch (IOException | MyException e) {
            log.error("error", e);
        } finally {
            closeTrackerServer(trackerServer);
        }
        return null;
    }

    /**
     * 從FastDFS下載文件
     *
     * @param localFilename  本地文件名
     * @param groupName      文件在FastDFS中的組名
     * @param remoteFilename 文件在FastDFS中的名稱
     */
    public static boolean downloadFile(String localFilename, String groupName, String remoteFilename) {
        TrackerServer trackerServer;
        try {
            trackerServer = trackerClient.getConnection();
        } catch (IOException e) {
            log.error("error", e);
            return false;
        }
        StorageClient storageClient = new StorageClient(trackerServer, null);
        File file = new File(localFilename);
        try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
            byte[] content = storageClient.download_file(groupName, remoteFilename);
            if (content == null || content.length == 0) {
                log.error("文件大小爲空!");
                boolean flag = file.delete();
                log.info("刪除文件結果:{}", flag);
                return false;
            }
            bos.write(content);
            log.info("成功下載文件: " + localFilename);
            return true;
        } catch (IOException | MyException e) {
            log.error("error", e);
        } finally {
            closeTrackerServer(trackerServer);
        }
        return false;
    }

    /**
     * 從FastDFS刪除文件
     *
     * @param groupName      文件在FastDFS中的組名
     * @param remoteFilename 文件在FastDFS中的名稱
     */
    public static boolean deleteFile(String groupName, String remoteFilename) {
        TrackerServer trackerServer;
        try {
            trackerServer = trackerClient.getConnection();
        } catch (IOException e) {
            log.error("error", e);
            return false;
        }
        StorageClient storageClient = new StorageClient(trackerServer, null);
        try {
            int i = storageClient.delete_file(groupName, remoteFilename);
            if (i == 0) {
                log.info("FastDFS刪除文件成功");
                return true;
            } else {
                log.info("FastDFS刪除文件失敗");
                return false;
            }
        } catch (IOException | MyException e) {
            log.error("error", e);
        } finally {
            closeTrackerServer(trackerServer);
        }
        return false;
    }

    private static void closeTrackerServer(TrackerServer trackerServer) {
        try {
            if (trackerServer != null) {
                log.info("關閉trackerServer連接");
                trackerServer.close();
            }
        } catch (IOException e) {
            log.error("error", e);
        }
    }
}

----------FastdfsdemoApplication.java

public class FastdfsdemoApplication {
    public static void main(String[] args) {
        String[] arr= FastDFSHelper.uploadFile("/home/whq/a.txt");
        if(arr!=null){
            boolean result= FastDFSHelper.downloadFile("/home/whq/a2.txt",arr[0],arr[1]);
            boolean result2=FastDFSHelper.deleteFile(arr[0],arr[1]);
        }
    }
}

 參考資料

https://blog.csdn.net/csdlwzy/article/details/83309831

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章