日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

fastDFS分布式文件系统--文件上传/下载/查询完整代码实现

發布時間:2025/1/21 windows 59 豆豆
生活随笔 收集整理的這篇文章主要介紹了 fastDFS分布式文件系统--文件上传/下载/查询完整代码实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

搭建環境

這里我們使用javaApi測試文件的上傳,java版本的fastdfs-client地址在: https://github.com/happyfish100/fastdfs-client-java,參考此工程編寫測試用例。

1)創建maven工程

pom.xml

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring‐boot‐starter‐parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <groupId>com.xuecheng</groupId> <artifactId>test‐fastDSF</artifactId> <version>1.0‐SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring‐boot‐starter‐web</artifactId> </dependency> <!‐‐ 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>org.springframework.boot</groupId> <artifactId>spring‐boot‐starter‐test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons‐io</artifactId> <version>1.3.2</version> </dependency> </dependencies> </parent>

2) 配置 文件

在classpath:config下創建fastdfs-client.properties文件

fastdfs.connect_timeout_in_seconds = 5 #http連接超時時間 fastdfs.network_timeout_in_seconds = 30 #tracker與storage網絡通信超時時間 fastdfs.charset = UTF‐8 #字符編碼 fastdfs.tracker_servers = 192.168.101.64:22122 #tracker服務器地址,多個地址中間用英文逗號分隔

文件上傳

//上傳文件@Testpublic void testUpload() {try {ClientGlobal.initByProperties("config/fastdfs‐client.properties");System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");System.out.println("charset=" + ClientGlobal.g_charset);//創建客戶端TrackerClient tc = new TrackerClient();//連接tracker ServerTrackerServer ts = tc.getConnection();if (ts == null) {System.out.println("getConnection return null");return;}//獲取一個storage serverStorageServer ss = tc.getStoreStorage(ts);if (ss == null) {System.out.println("getStoreStorage return null");}//創建一個storage存儲客戶端StorageClient1 sc1 = new StorageClient1(ts, ss);NameValuePair[] meta_list = null; //new NameValuePair[0];String item = "C:\\Users\\admin\\Desktop\\1.png";String fileid;fileid = sc1.upload_file1(item, "png", meta_list);System.out.println("Upload local file " + item + " ok, fileid=" + fileid);} catch (Exception ex) {ex.printStackTrace();}}

文件查詢

//查詢文件 @Test public void testQueryFile() throws IOException, MyException {ClientGlobal.initByProperties("config/fastdfs‐client.properties");TrackerClient tracker = new TrackerClient();TrackerServer trackerServer = tracker.getConnection();StorageServer storageServer = null;StorageClient storageClient = new StorageClient(trackerServer,storageServer);FileInfo fileInfo = storageClient.query_file_info("group1","M00/00/01/wKhlQFrKBSOAW5AWAALcAg10vf4862.png");System.out.println(fileInfo); }

文件下載

//下載文件@Testpublic void testDownloadFile() throws IOException, MyException {ClientGlobal.initByProperties("config/fastdfs‐client.properties");TrackerClient tracker = new TrackerClient();TrackerServer trackerServer = tracker.getConnection();StorageServer storageServer = null;StorageClient1 storageClient1 = new StorageClient1(trackerServer,storageServer);byte[] result =storageClient1.download_file1("group1/M00/00/01/wKhlQFrKBSOAW5AWAALcAg10vf4862.png");File file = new File("d:/1.png");FileOutputStream fileOutputStream = new FileOutputStream(file);fileOutputStream.write(result);fileOutputStream.close();}

?

總結

以上是生活随笔為你收集整理的fastDFS分布式文件系统--文件上传/下载/查询完整代码实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。