Java fastdfs客戶端實(shí)現(xiàn)上傳下載文件
一、項(xiàng)目結(jié)構(gòu)
二、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連接超時(shí)時(shí)間fastdfs.connect_timeout_in_seconds=5#tracker和storage網(wǎng)絡(luò)通信超時(shí)時(shí)間fastdfs.network_timeout_in_seconds=30#字符編碼fastdfs.charset=utf-8#tracker服務(wù)器地址,多個(gè)地址中間用英文逗號(hào)分隔fastdfs.tracker_servers=192.168.2.105:22122
四、測(cè)試
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,用于請(qǐng)求TrackerServer TrackerClient trackerClient = new TrackerClient(); //連接tracker TrackerServer trackerServer = trackerClient.getConnection(); //獲取storage StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer); //創(chuàng)建storageClient StorageClient1 storageClient1 = new StorageClient1(trackerServer, storeStorage); //向storage服務(wù)器上傳文件 //本地文件的路徑 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,用于請(qǐng)求TrackerServer TrackerClient trackerClient = new TrackerClient(); //連接tracker TrackerServer trackerServer = trackerClient.getConnection(); //獲取storage StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer); //創(chuàng)建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(); } }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. PHP設(shè)計(jì)模式中工廠模式深入詳解2. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析3. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法4. 解決AJAX返回狀態(tài)200沒(méi)有調(diào)用success的問(wèn)題5. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)6. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明7. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法8. CSS hack用法案例詳解9. Ajax實(shí)現(xiàn)表格中信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù)10. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向
