java操作FastDFS


1、創建一個普通的Maven項目

2、pom文件中加入依賴

<dependency>
    <groupId>net.oschina.zcx7878</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27.0.0</version>
</dependency>

<!-- spring-core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.25.RELEASE</version>
</dependency>

3、創建fdfs_client.conf客戶端配置文件

connect_timeout=30
network_timeout=60
base_path=/home/fastdfs

#改為自己服務器的ip
tracker_server=192.168.81.130:22122
log_level=info
use_connection_pool = false
connection_pool_max_idle_time = 3600
load_fdfs_parameters_from_tracker=false
use_storage_id = false
storage_ids_filename = storage_ids.conf
http.tracker_server_port=80

4、測試類

public class TestFastDFS {
    public static void main(String[] args) throws Exception {

        String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();

        // 1、加載配置文件,配置文件中的內容就是 tracker 服務的地址。
        ClientGlobal.init(filePath);

        // 2、創建一個 TrackerClient 對象。直接 new 一個。
        TrackerClient trackerClient = new TrackerClient();

        // 3、使用 TrackerClient 對象創建連接,獲得一個 TrackerServer 對象。
        TrackerServer trackerServer = trackerClient.getConnection();

        // 4、創建一個 StorageServer 的引用,值為 null
        StorageServer storageServer = null;

        // 5、創建一個 StorageClient 對象,需要兩個參數 TrackerServer 對象、StorageServer 的引用
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);

        // 6、使用 StorageClient 對象上傳圖片。
        //擴展名不帶“.”

        String[] strings = storageClient.upload_file("C:/Users/LJH/Pictures/Camera Roll/bj1.jpg", "jpg",null);
        // 7、返回數組。包含組名和圖片的路徑。
        for (String string : strings) {
            System.out.println(string);
        }
        System.out.println("上傳完成");
    }
}


免責聲明!

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



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