Java fastdfs客戶端實現上傳下載檔案
阿新 • • 發佈:2020-10-09
一、專案結構
二、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"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>A01fastdfs</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java --> <dependency> <groupId>net.oschina.zcx7878</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27.0.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.9</source> <target>1.9</target> </configuration> </plugin> </plugins> </build> </project>
三、fastdfs-client.properties
#http連線超時時間
fastdfs.connect_timeout_in_seconds=5
#tracker和storage網路通訊超時時間
fastdfs.network_timeout_in_seconds=30
#字元編碼
fastdfs.charset=utf-8
#tracker伺服器地址,多個地址中間用英文逗號分隔
fastdfs.tracker_servers=192.168.2.105:22122
四、測試
package com.wuxi.test; import org.csource.fastdfs.*; import org.junit.Test; import java.io.File; import java.io.FileOutputStream; public class MyTest { //上傳檔案 @Test public void testUpload() { try { //載入fastdfs-client.properties配置檔案 ClientGlobal.initByProperties("config/fastdfs-client.properties"); //定義TrackerClient,用於請求TrackerServer TrackerClient trackerClient = new TrackerClient(); //連線tracker TrackerServer trackerServer = trackerClient.getConnection(); //獲取storage StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer); //建立storageClient StorageClient1 storageClient1 = new StorageClient1(trackerServer,storeStorage); //向storage伺服器上傳檔案 //本地檔案的路徑 String path = "F:/java/resource/data.txt"; //上傳成功後拿到檔案Id String fileId = storageClient1.upload_file1(path,"txt",null); System.out.println(fileId);//group1/M00/00/00/wKgCaV9vaSaARBTKAAAAGjJpL2g017.txt } catch (Exception e) { e.printStackTrace(); } } //下載檔案 @Test public void testDownload() { try { //載入fastdfs-client.properties配置檔案 ClientGlobal.initByProperties("config/fastdfs-client.properties"); //定義TrackerClient,用於請求TrackerServer TrackerClient trackerClient = new TrackerClient(); //連線tracker TrackerServer trackerServer = trackerClient.getConnection(); //獲取storage StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer); //建立storageClient StorageClient1 storageClient1 = new StorageClient1(trackerServer,storeStorage); //下載檔案 //檔案id String fileId = "group1/M00/00/00/wKgCaV9vaSaARBTKAAAAGjJpL2g017.txt"; byte[] bytes = storageClient1.download_file1(fileId); //使用輸出流儲存檔案 FileOutputStream fileOutputStream = new FileOutputStream(new File("F:/data.txt")); fileOutputStream.write(bytes); } catch (Exception e) { e.printStackTrace(); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。