Java中簡單測試FastDFS的文件上傳

pom.xml文件內容如下:

<dependencies>
        <!-- fastdfs -->
        <dependency>
            <groupId>org.csource.fastdfs</groupId>
            <artifactId>fastdfs</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
    </dependencies>

配置文件內容如下:

 

測試代碼如下:

try {
            // 1、加載配置文件,配置文件中的內容就是tracker服務的地址(絕對路徑)
            ClientGlobal.init("E:\\IdeaProjects\\weiku_rab\\test_fdfs\\src\\main\\resources\\fdfs_client.conf");
            // 2、創建一個TrackerClient對象
            TrackerClient trackerClient = new TrackerClient();
            // 3、使用阿哥TrackerClient對象創建連接,獲得一個TrackerServer對象
            TrackerServer trackerServer = trackerClient.getConnection();
            // 4、創建一個StorageServer的引用,值爲null
            StorageServer storageServer = null;
            // 5、創建一個StorageClient對象,需要兩個參數TrackerServer對象、Storage、Server的引用
            StorageClient storageClient = new StorageClient(trackerServer, storageServer);
            // 6、使用StorageClient對象上傳圖片,擴展名不用帶“.”
            String [] strs = storageClient.upload_file("E:/bg2.jpg","jpg",null);
            // 7、返回數組,包含組名和圖片的路徑
            System.out.print("地址爲:");
            String path = "";
            for (String str : strs) {   // 組名+磁盤地址
                path = path + str + "/";
            }
            // 進行地址處理並輸出
            System.out.println(path.substring(0,path.length()-1));
        } catch (Exception e) {
            e.printStackTrace();
        }

運行結果如下:

 

 

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