012_Java操作FastDFS
生活随笔
收集整理的這篇文章主要介紹了
012_Java操作FastDFS
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1. 創(chuàng)建一個名為FastDFS的Java工程, 并導(dǎo)入Jar包
1.1. fastdfs-client-java-1.29-SNAPSHOT.jar是FastDFS的Java API包
1.2. commons-io-2.4.jar是Apache的一個處理IO的工具類包
2. fdfs_client.conf配置
connect_timeout = 2 #網(wǎng)絡(luò)建立連接超時時間 network_timeout = 30 #網(wǎng)絡(luò)發(fā)送和接收數(shù)據(jù)超時時間 charset = UTF-8 #編碼方式 http.tracker_http_port = 8888 #http的端口號 http.anti_steal_token = no http.secret_key = FastDFS1234567890 # tracker服務(wù)器 tracker_server = 192.168.25.135:22122 tracker_server = 192.168.25.137:22122 tracker_server = 192.168.25.138:221223. 上傳文件
package com.lywgames.fastdfs;import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; import org.apache.commons.io.FileUtils; import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.StorageClient; import org.csource.fastdfs.StorageServer; import org.csource.fastdfs.TrackerClient; import org.csource.fastdfs.TrackerServer; import org.csource.fastdfs.UploadCallback;/*** public String[] upload_file(byte[] file_buff, String file_ext_name, NameValuePair[] meta_list)* public String[] upload_file(String local_filename, String file_ext_name, NameValuePair[] meta_list)* public String[] upload_file(String group_name, byte[] file_buff, String file_ext_name, NameValuePair[] meta_list)* public String[] upload_file(byte[] file_buff, int offset, int length, String file_ext_name, NameValuePair[] meta_list)* public String[] upload_file(String group_name, long file_size, UploadCallback callback, String file_ext_name, NameValuePair[] meta_list)* public String[] upload_file(String group_name, byte[] file_buff, int offset, int length, String file_ext_name, NameValuePair[] meta_list)* public String[] upload_file(String group_name, String master_filename, String prefix_name, byte[] file_buff, String file_ext_name, NameValuePair[] meta_list)* public String[] upload_file(String group_name, String master_filename, String prefix_name, String local_filename, String file_ext_name, NameValuePair[] meta_list)* public String[] upload_file(String group_name, String master_filename, String prefix_name, long file_size, UploadCallback callback, String file_ext_name, NameValuePair[] meta_list)* public String[] upload_file(String group_name, String master_filename, String prefix_name, byte[] file_buff, int offset, int length, String file_ext_name, NameValuePair[] meta_list)**/ public class UploadFile {public static final String uploadPathPre = "./upload/";public static String originalFileName = "";public static void main(String[] args) throws FileNotFoundException, IOException, Exception {// 加載配置文件ClientGlobal.init("./fdfs_client.conf");TrackerClient trackerClient = new TrackerClient();TrackerServer trackerServer = trackerClient.getTrackerServer();StorageServer storageServer = null;StorageClient storageClient = new StorageClient(trackerServer, storageServer);upload_file10(storageClient);}public static void recordFileName(String[] upload_file) throws IOException {StringBuffer sb = new StringBuffer();for (String string : upload_file) {System.out.println(string);sb.append(string).append("#");}sb.append(originalFileName).append("\r\n");FileUtils.write(new File("./download/uploadfilename.txt"), sb.toString(), "utf-8", true);}/*** 通過字節(jié)數(shù)組上傳文件到儲存服務(wù)器, 適合上傳小文件*/public static void upload_file1(StorageClient storageClient) throws Exception {originalFileName = "01.png";byte[] buffer = FileUtils.readFileToByteArray(new File(uploadPathPre + originalFileName));String[] upload_file = storageClient.upload_file(buffer, "png", null);recordFileName(upload_file);}/*** 通過文件名上傳文件到儲存服務(wù)器, 適合上傳小文件*/public static void upload_file2(StorageClient storageClient) throws Exception {originalFileName = "02.png";String[] upload_file = storageClient.upload_file(uploadPathPre + originalFileName, "png", null);recordFileName(upload_file);}/*** 通過字節(jié)數(shù)組上傳文件到指定卷/組的儲存服務(wù)器, 適合上傳小文件*/public static void upload_file3(StorageClient storageClient) throws Exception {originalFileName = "03.png";byte[] buffer = FileUtils.readFileToByteArray(new File(uploadPathPre + originalFileName));String[] upload_file = storageClient.upload_file("group2", buffer, "png", null);recordFileName(upload_file);}/*** 通過字節(jié)數(shù)組上傳文件到儲存服務(wù)器(可以設(shè)置偏移量和長度), 適合上傳小文件*/public static void upload_file4(StorageClient storageClient) throws Exception {originalFileName = "04.png";byte[] buffer = FileUtils.readFileToByteArray(new File(uploadPathPre + originalFileName));String[] upload_file = storageClient.upload_file(buffer, 0, buffer.length, "png", null);recordFileName(upload_file);}/*** 設(shè)置上傳文件回調(diào)上傳文件到儲存服務(wù)器, 適合上傳大文件*/public static void upload_file5(StorageClient storageClient) throws Exception {originalFileName = "001.mp4";File file = new File(uploadPathPre + originalFileName);String[] upload_file = storageClient.upload_file("group2", file.length(), new UploadCallback() {@Overridepublic int send(OutputStream out) throws IOException {int readBytes = -1;byte[] buff = new byte[256 * 1024];FileInputStream fis = new FileInputStream(uploadPathPre + originalFileName);try {while ((readBytes = fis.read(buff)) != -1) {out.write(buff, 0, readBytes);}out.flush();} finally {fis.close();}return 0; }}, "mp4", null);recordFileName(upload_file);}/*** 通過字節(jié)數(shù)組上傳文件到指定卷/組的儲存服務(wù)器(可以設(shè)置偏移量和長度), 適合上傳小文件*/public static void upload_file6(StorageClient storageClient) throws Exception {originalFileName = "05.png";byte[] buffer = FileUtils.readFileToByteArray(new File(uploadPathPre + originalFileName));String[] upload_file = storageClient.upload_file("group2", buffer, 0, buffer.length, "png", null);recordFileName(upload_file);}/*** 通過字節(jié)數(shù)組上傳關(guān)聯(lián)文件到指定卷/組的儲存服務(wù)器, 適合上傳小文件* 如: thum180_01.png是01.png的縮略圖*/public static void upload_file7(StorageClient storageClient) throws Exception {originalFileName = "thum180_01.png";byte[] buffer = FileUtils.readFileToByteArray(new File(uploadPathPre + originalFileName));String[] upload_file = storageClient.upload_file("group1", "M00/00/00/wKgZh17ndBKAdW0AAARREaA3Y7E015.png", "-thum180", buffer, "png", null);recordFileName(upload_file);}/*** 通過文件名上傳關(guān)聯(lián)文件到指定卷/組的儲存服務(wù)器, 適合上傳小文件*/public static void upload_file8(StorageClient storageClient) throws Exception {originalFileName = "thum180_02.png";String[] upload_file = storageClient.upload_file("group1", "M00/00/00/wKgZiV7ndIGAMj1_AAKI8AhjjRQ986.png", "-thum180", uploadPathPre + originalFileName, "png", null);recordFileName(upload_file);}/*** 設(shè)置上傳文件回調(diào)上傳關(guān)聯(lián)文件到指定卷/組的文件到儲存服務(wù)器, 適合上傳大文件*/public static void upload_file9(StorageClient storageClient) throws Exception {originalFileName = "002.wmv";File file = new File(uploadPathPre + originalFileName);String[] upload_file = storageClient.upload_file("group2", "M00/00/00/wKgZil7neX6AfyzNHod5dWQOPIE467.mp4", "-part1", file.length(), new UploadCallback() {@Overridepublic int send(OutputStream out) throws IOException {int readBytes = -1;byte[] buff = new byte[256 * 1024];FileInputStream fis = new FileInputStream(uploadPathPre + originalFileName);try {while ((readBytes = fis.read(buff)) != -1) {out.write(buff, 0, readBytes);}out.flush();} finally {fis.close();}return 0; }}, "wmv", null);recordFileName(upload_file);}/*** 通過字節(jié)數(shù)組上傳關(guān)聯(lián)文件到指定卷/組的儲存服務(wù)器(可以設(shè)置偏移量和長度) 適合上傳小文件*/public static void upload_file10(StorageClient storageClient) throws Exception {originalFileName = "thum180_03.png";byte[] buffer = FileUtils.readFileToByteArray(new File(uploadPathPre + originalFileName));String[] upload_file = storageClient.upload_file("group2", "M00/00/00/wKgZil7neOaAaKSNAAKB0S2MPH0385.png", "-thum180", buffer, 0, buffer.length, "png", null);recordFileName(upload_file);}}4. 下載文件
package com.lywgames.fastdfs;import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.DownloadCallback; import org.csource.fastdfs.StorageClient; import org.csource.fastdfs.StorageServer; import org.csource.fastdfs.TrackerClient; import org.csource.fastdfs.TrackerServer;/*** public byte[] download_file(String group_name, String remote_filename)* public int download_file(String group_name, String remote_filename, DownloadCallback callback)* public int download_file(String group_name, String remote_filename, String local_filename)* public byte[] download_file(String group_name, String remote_filename, long file_offset, long download_bytes)* public int download_file(String group_name, String remote_filename, long file_offset, long download_bytes, DownloadCallback callback) * public int download_file(String group_name, String remote_filename, long file_offset, long download_bytes, String local_filename)*/ public class DownloadFile {public static void main(String[] args) throws FileNotFoundException, IOException, Exception {// 加載配置文件ClientGlobal.init("./fdfs_client.conf");TrackerClient trackerClient = new TrackerClient();TrackerServer trackerServer = trackerClient.getTrackerServer();StorageServer storageServer = null;StorageClient storageClient = new StorageClient(trackerServer, storageServer);download_file5(storageClient);}/*** 從存儲服務(wù)器上下載文件, 返回字節(jié)數(shù)組, 適合下載小文件*/public static void download_file1(StorageClient storageClient) throws IOException, Exception {byte[] buff = storageClient.download_file("group1", "M00/00/00/wKgZh17ndBKAdW0AAARREaA3Y7E015.png");FileUtils.copyInputStreamToFile(new ByteArrayInputStream(buff), new File("./download/wKgZh17ndBKAdW0AAARREaA3Y7E015.png"));}/*** 從存儲服務(wù)器上下載文件, 返回0下載成功, 可以設(shè)置下載回調(diào), 適合下載大文件*/public static void download_file2(StorageClient storageClient) throws IOException, Exception {int result = storageClient.download_file("group2", "M00/00/00/wKgZil7neX6AfyzNHod5dWQOPIE467.mp4", new DownloadCallback() {long current_bytes = 0;FileOutputStream fos = null;@Overridepublic int recv(long file_size, byte[] data, int bytes) {try {if(fos == null) {fos = new FileOutputStream("./download/wKgZil7neX6AfyzNHod5dWQOPIE467.mp4");}fos.write(data, 0, bytes);current_bytes += bytes;System.out.println("current_bytes = " + current_bytes);if (current_bytes == file_size) {fos.close();fos = null;current_bytes = 0;}} catch (IOException e) {e.printStackTrace();return -1; // 下載失敗}return 0; // 下載成功}});System.out.println("result = " + result + "[0成功,非0失敗]");}/*** 從存儲服務(wù)器上下載文件, 返回0下載成功, 適合下載小文件*/public static void download_file3(StorageClient storageClient) throws IOException, Exception {int result = storageClient.download_file("group1", "M00/00/00/wKgZiV7ndIGAMj1_AAKI8AhjjRQ986.png", "./download/wKgZiV7ndIGAMj1_AAKI8AhjjRQ986.png");System.out.println("result = " + result + "[0成功,非0失敗]");}/*** 從存儲服務(wù)器上下載文件, 返回字節(jié)數(shù)組, 可以設(shè)置偏移量和下載字節(jié)數(shù), 下載字節(jié)數(shù)設(shè)置為0, 下載從偏移量開始的所有剩余字節(jié), 適合下載小文件*/public static void download_file4(StorageClient storageClient) throws IOException, Exception {byte[] buff = storageClient.download_file("group2", "M00/00/00/wKgZil7neOaAaKSNAAKB0S2MPH0385.png", 0, 0);FileUtils.copyInputStreamToFile(new ByteArrayInputStream(buff), new File("./download/wKgZil7neOaAaKSNAAKB0S2MPH0385.png"));}/*** 從存儲服務(wù)器上下載文件, 返回0下載成功, 可以設(shè)置偏移量和下載字節(jié)數(shù), 下載字節(jié)數(shù)設(shè)置為0, 下載從偏移量開始的所有剩余字節(jié), 可以設(shè)置下載回調(diào), 適合下載大文件*/public static void download_file5(StorageClient storageClient) throws IOException, Exception {int result = storageClient.download_file("group2", "M00/00/00/wKgZil7neX6AfyzNHod5dWQOPIE467-part1.wmv", 0, 0, new DownloadCallback() {long current_bytes = 0;FileOutputStream fos = null;@Overridepublic int recv(long file_size, byte[] data, int bytes) {try {if(fos == null) {fos = new FileOutputStream("./download/1.wmv");}fos.write(data, 0, bytes);current_bytes += bytes;System.out.println("current_bytes = " + current_bytes);if (current_bytes == file_size) {fos.close();fos = null;current_bytes = 0;}} catch (IOException e) {e.printStackTrace();return -1; // 下載失敗}return 0; // 下載成功}});System.out.println("result = " + result + "[0成功,非0失敗]");}/*** 從存儲服務(wù)器上下載文件, 返回0下載成功, 可以設(shè)置偏移量和下載字節(jié)數(shù), 下載字節(jié)數(shù)設(shè)置為0, 下載從偏移量開始的所有剩余字節(jié), 適合下載小文件*/public static void download_file6(StorageClient storageClient) throws IOException, Exception {int result = storageClient.download_file("group1", "M00/00/00/wKgZh17neOaABcQnAAJwzdtgTas251.png", 0, 0, "./download/wKgZh17neOaABcQnAAJwzdtgTas251.png");System.out.println("result = " + result + "[0成功,非0失敗]");}}5. http下載文件
package com.lywgames.fastdfs;import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import org.apache.commons.io.FileUtils;/*** 使用FileUtils下載Url圖片*/ public class CopyURLToFile {public static void main(String[] args) throws MalformedURLException, IOException {String url = "http://192.168.25.135:8888/group2/M00/00/00/wKgZil7ngBaAGYoqAAKZkg_11ZI745.png";FileUtils.copyURLToFile(new URL(url), new File("./download/wKgZil7ngBaAGYoqAAKZkg_11ZI745.png"));} }6. 刪除文件
package com.lywgames.fastdfs;import java.io.IOException;import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.StorageClient; import org.csource.fastdfs.StorageServer; import org.csource.fastdfs.TrackerClient; import org.csource.fastdfs.TrackerServer;/*** public int delete_file(String group_name, String remote_filename)從儲存服務(wù)器上刪除文件, * 返回0刪除成功, 返回非0刪除失敗, 只有一個刪除方法。*/ public class DeleteFile {public static void main(String[] args) throws IOException, Exception {// 加載配置文件ClientGlobal.init("./fdfs_client.conf");TrackerClient trackerClient = new TrackerClient();TrackerServer trackerServer = trackerClient.getTrackerServer();StorageServer storageServer = null;StorageClient storageClient = new StorageClient(trackerServer, storageServer);int result = storageClient.delete_file("group1", "M00/00/00/wKgZh17ovJWECZFLAAAAAP4rmRY642.txt");System.out.println("result = " + result + "[0成功,非0失敗]");} }7. 追加文件
package com.lywgames.fastdfs;import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; import org.apache.commons.io.FileUtils; import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.StorageClient; import org.csource.fastdfs.StorageServer; import org.csource.fastdfs.TrackerClient; import org.csource.fastdfs.TrackerServer; import org.csource.fastdfs.UploadCallback;/*** public String[] upload_appender_file(byte[] file_buff, String file_ext_name, NameValuePair[] meta_list) * public String[] upload_appender_file(String local_filename, String file_ext_name, NameValuePair[] meta_list)* public String[] upload_appender_file(String group_name, byte[] file_buff, String file_ext_name, NameValuePair[] meta_list)* public String[] upload_appender_file(byte[] file_buff, int offset, int length, String file_ext_name, NameValuePair[] meta_list)* public String[] upload_appender_file(String group_name, long file_size, UploadCallback callback, String file_ext_name, NameValuePair[] meta_list)* public String[] upload_appender_file(String group_name, byte[] file_buff, int offset, int length, String file_ext_name, NameValuePair[] meta_list)* * public int append_file(String group_name, String appender_filename, byte[] file_buff)* public int append_file(String group_name, String appender_filename, String local_filename)* public int append_file(String group_name, String appender_filename, long file_size, UploadCallback callback)* public int append_file(String group_name, String appender_filename, byte[] file_buff, int offset, int length)*/ public class AppendFile {public static String originalFileName = "";public static void main(String[] args) throws FileNotFoundException, IOException, Exception {// 加載配置文件ClientGlobal.init("./fdfs_client.conf");TrackerClient trackerClient = new TrackerClient();TrackerServer trackerServer = trackerClient.getTrackerServer();StorageServer storageServer = null;StorageClient storageClient = new StorageClient(trackerServer, storageServer);uploadAppenderFile2(storageClient);uploadAppenderFile3(storageClient);uploadAppenderFile4(storageClient);uploadAppenderFile5(storageClient);uploadAppenderFile6(storageClient);appendFile1(storageClient);appendFile2(storageClient);appendFile3(storageClient);appendFile4(storageClient);}public static void recordFileName(String[] upload_file) throws IOException {StringBuffer sb = new StringBuffer();for (String string : upload_file) {System.out.println(string);sb.append(string).append("#");}sb.append(originalFileName).append("\r\n");FileUtils.write(new File("./download/uploadfilename.txt"), sb.toString(), "utf-8", true);}public static void uploadAppenderFile1(StorageClient storageClient) throws Exception {originalFileName = "";String uploadAppenderFileStr = "public String[] upload_appender_file(byte[] file_buff, String file_ext_name, NameValuePair[] meta_list)";byte[] buffer = uploadAppenderFileStr.getBytes(ClientGlobal.g_charset);String[] upload_file = storageClient.upload_appender_file(buffer, "txt", null);recordFileName(upload_file);}public static void uploadAppenderFile2(StorageClient storageClient) throws Exception {originalFileName = "0001.txt";String[] upload_file = storageClient.upload_appender_file("./upload/0001.txt", "txt", null);recordFileName(upload_file);}public static void uploadAppenderFile3(StorageClient storageClient) throws Exception {originalFileName = "";String uploadAppenderFileStr = "public String[] upload_appender_file(String local_filename, String file_ext_name, NameValuePair[] meta_list)";byte[] buffer = uploadAppenderFileStr.getBytes(ClientGlobal.g_charset);String[] upload_file = storageClient.upload_appender_file("group1", buffer, "txt", null);recordFileName(upload_file);}public static void uploadAppenderFile4(StorageClient storageClient) throws Exception {originalFileName = "";String uploadAppenderFileStr = "public String[] upload_appender_file(String group_name, byte[] file_buff, String file_ext_name, NameValuePair[] meta_list)";byte[] buffer = uploadAppenderFileStr.getBytes(ClientGlobal.g_charset);String[] upload_file = storageClient.upload_appender_file(buffer, 0, buffer.length, "txt", null);recordFileName(upload_file);}public static void uploadAppenderFile5(StorageClient storageClient) throws Exception {originalFileName = "0002.txt";File file = new File("./upload/0002.txt");String[] upload_file = storageClient.upload_appender_file("group1", file.length(), new UploadCallback() {@Overridepublic int send(OutputStream out) throws IOException {int readBytes = -1;byte[] buff = new byte[256 * 1024];FileInputStream fis = new FileInputStream("./upload/0002.txt");try {while ((readBytes = fis.read(buff)) != -1) {out.write(buff, 0, readBytes);}out.flush();} finally {fis.close();}return 0;}}, "txt", null);recordFileName(upload_file);}public static void uploadAppenderFile6(StorageClient storageClient) throws Exception {originalFileName = "";String uploadAppenderFileStr = "public String[] upload_appender_file(byte[] file_buff, int offset, int length, String file_ext_name, NameValuePair[] meta_list)";byte[] buffer = uploadAppenderFileStr.getBytes(ClientGlobal.g_charset);String[] upload_file = storageClient.upload_appender_file("group1", buffer, 0, buffer.length, "txt", null);recordFileName(upload_file);}public static void appendFile1(StorageClient storageClient) throws Exception {String uploadAppenderFileStr = "public int append_file(String group_name, String appender_filename, byte[] file_buff)";byte[] buffer = uploadAppenderFileStr.getBytes(ClientGlobal.g_charset);int result = storageClient.append_file("group1", "M00/00/00/wKgZh17ovJWECZFLAAAAAP4rmRY642.txt", buffer);System.out.println("result = " + result + "[0成功,非0失敗]");}public static void appendFile2(StorageClient storageClient) throws Exception {int result = storageClient.append_file("group1", "M00/00/00/wKgZh17ovJWECZFLAAAAAP4rmRY642.txt", "./upload/0003.txt");System.out.println("result = " + result + "[0成功,非0失敗]");}public static void appendFile3(StorageClient storageClient) throws Exception {File file = new File("./upload/0004.txt");int result = storageClient.append_file("group1", "M00/00/00/wKgZh17ovJWECZFLAAAAAP4rmRY642.txt", file.length(), new UploadCallback() {@Overridepublic int send(OutputStream out) throws IOException {int readBytes = -1;byte[] buff = new byte[256 * 1024];FileInputStream fis = new FileInputStream("./upload/0004.txt");try {while ((readBytes = fis.read(buff)) != -1) {out.write(buff, 0, readBytes);out.flush();}} finally {fis.close();}return 0;}});System.out.println("result = " + result + "[0成功,非0失敗]");}public static void appendFile4(StorageClient storageClient) throws Exception {String uploadAppenderFileStr = "public int append_file(String group_name, String appender_filename, String local_filename)";byte[] buffer = uploadAppenderFileStr.getBytes(ClientGlobal.g_charset);int result = storageClient.append_file("group1", "M00/00/00/wKgZh17ovJWECZFLAAAAAP4rmRY642.txt", buffer, 0, buffer.length);System.out.println("result = " + result + "[0成功,非0失敗]");}}8. 存儲服務(wù)器信息
package com.lywgames.fastdfs;import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.ServerInfo; import org.csource.fastdfs.StorageServer; import org.csource.fastdfs.StructGroupStat; import org.csource.fastdfs.StructStorageStat; import org.csource.fastdfs.TrackerClient; import org.csource.fastdfs.TrackerServer;/*** 獲取存儲服務(wù)器信息*/ public class Storages {public static void main(String[] args) throws Exception {// 加載配置文件ClientGlobal.init("./fdfs_client.conf");TrackerClient trackerClient = new TrackerClient();TrackerServer trackerServer = trackerClient.getTrackerServer();//StorageServer storageServer = null;// StorageClient storageClient = new StorageClient(trackerServer, storageServer);getFetchStorages2(trackerClient, trackerServer);}public static void print(StorageServer... storageServers) {if(storageServers == null) {return;}int i = 1;for (StorageServer storageServer : storageServers) {System.out.println(i++ + ". " + storageServer.getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServer.getInetSocketAddress().getPort());}}public static void getStoreStorage1(TrackerClient trackerClient, TrackerServer trackerServer) throws Exception {StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);print(storageServer);}public static void getStoreStorage2(TrackerClient trackerClient, TrackerServer trackerServer) throws Exception {StorageServer storageServer = trackerClient.getStoreStorage(trackerServer, "group2");print(storageServer);}public static void getStoreStorages(TrackerClient trackerClient, TrackerServer trackerServer) throws Exception {StorageServer[] storageServers = trackerClient.getStoreStorages(trackerServer, "group1");print(storageServers);}public static void getFetchStorage(TrackerClient trackerClient, TrackerServer trackerServer) throws Exception {StorageServer storageServer = trackerClient.getFetchStorage(trackerServer, "group2", "M00/00/00/wKgZil7neX6AfyzNHod5dWQOPIE467.mp4");print(storageServer);}public static void getUpdateStorage(TrackerClient trackerClient, TrackerServer trackerServer) throws Exception {StorageServer storageServer = trackerClient.getUpdateStorage(trackerServer, "group2", "M00/00/00/wKgZil7neX6AfyzNHod5dWQOPIE467.mp4");print(storageServer);}public static void getFetchStorage1(TrackerClient trackerClient, TrackerServer trackerServer) throws Exception {StorageServer storageServer = trackerClient.getFetchStorage1(trackerServer, "group2/M00/00/00/wKgZil7neX6AfyzNHod5dWQOPIE467.mp4");print(storageServer);}public static void print(StructGroupStat[] sgs) {int i = 1;for (StructGroupStat structGroupStat : sgs) {System.out.println("Group " + i++ + ":");System.out.println("\tgroup name = " + structGroupStat.getGroupName());System.out.println("\tdisk total space = " + structGroupStat.getTotalMB());System.out.println("\tdisk free space = " + structGroupStat.getFreeMB());System.out.println("\ttrunk free space = " + structGroupStat.getTrunkFreeMB());System.out.println("\tstorage server count = " + structGroupStat.getStorageCount());System.out.println("\tstorage server port = " + structGroupStat.getStoragePort());System.out.println("\tstorage HTTP port = " + structGroupStat.getStorageHttpPort());System.out.println("\tactive server count = " + structGroupStat.getActiveCount());System.out.println("\tcurrent write server index = " + structGroupStat.getCurrentWriteServer());System.out.println("\tstore path count = " + structGroupStat.getStorePathCount());System.out.println("\tsubdir count per path = " + structGroupStat.getSubdirCountPerPath());System.out.println("\tcurrent trunk file id = " + structGroupStat.getCurrentTrunkFileId());}}public static void listGroups(TrackerClient trackerClient, TrackerServer trackerServer) throws Exception {StructGroupStat[] sgs = trackerClient.listGroups(trackerServer);print(sgs);}public static void print(StructStorageStat[] sss) {int i = 1;for (StructStorageStat structStorageStat : sss) {System.out.println("Storage " + i++ + ":");System.out.println("\tid = " + structStorageStat.getId());System.out.println("\tip_addr = " + structStorageStat.getIpAddr());System.out.println("\thttp domain = " + structStorageStat.getDomainName());System.out.println("\tversion = " + structStorageStat.getVersion());System.out.println("\tjoin time = " + structStorageStat.getJoinTime());System.out.println("\tup time = " + structStorageStat.getUpTime());System.out.println("\ttotal storage = " + structStorageStat.getTotalMB());System.out.println("\tfree storage = " + structStorageStat.getFreeMB());System.out.println("\tupload priority = " + structStorageStat.getUploadPriority());System.out.println("\tstore_path_count = " + structStorageStat.getStorePathCount());System.out.println("\tsubdir_count_per_path = " + structStorageStat.getSubdirCountPerPath());System.out.println("\tstorage_port = " + structStorageStat.getStoragePort());System.out.println("\tstorage_http_port = " + structStorageStat.getStorageHttpPort());System.out.println("\tcurrent_write_path = " + structStorageStat.getCurrentWritePath());System.out.println("\tsource storage id = " + structStorageStat.getSrcIpAddr());System.out.println("\tif_trunk_server = " + structStorageStat.isTrunkServer());System.out.println("\tconnection.alloc_count = " + structStorageStat.getConnectionAllocCount());System.out.println("\tconnection.current_count = " + structStorageStat.getConnectionCurrentCount());System.out.println("\tconnection.max_count = " + structStorageStat.getConnectionMaxCount());System.out.println("\ttotal_upload_count = " + structStorageStat.getTotalUploadCount());System.out.println("\tsuccess_upload_count = " + structStorageStat.getSuccessUploadCount());System.out.println("\ttotal_append_count = " + structStorageStat.getTotalAppendCount());System.out.println("\tsuccess_append_count = " + structStorageStat.getSuccessAppendCount());System.out.println("\ttotal_modify_count = " + structStorageStat.getTotalModifyCount());System.out.println("\tsuccess_modify_count = " + structStorageStat.getSuccessModifyCount());System.out.println("\ttotal_truncate_count = " + structStorageStat.getTotalTruncateCount());System.out.println("\tsuccess_truncate_count = " + structStorageStat.getSuccessTruncateCount());System.out.println("\ttotal_set_meta_count = " + structStorageStat.getTotalSetMetaCount());System.out.println("\tsuccess_set_meta_count = " + structStorageStat.getSuccessSetMetaCount());System.out.println("\ttotal_delete_count = " + structStorageStat.getTotalDeleteCount());System.out.println("\tsuccess_delete_count = " + structStorageStat.getSuccessDeleteCount());System.out.println("\ttotal_download_count = " + structStorageStat.getTotalDownloadCount());System.out.println("\tsuccess_download_count = " + structStorageStat.getSuccessDownloadCount());System.out.println("\ttotal_get_meta_count = " + structStorageStat.getTotalGetMetaCount());System.out.println("\tsuccess_get_meta_count = " + structStorageStat.getSuccessGetMetaCount());System.out.println("\ttotal_create_link_count = " + structStorageStat.getTotalCreateLinkCount());System.out.println("\tsuccess_create_link_count = " + structStorageStat.getSuccessCreateLinkCount());System.out.println("\ttotal_delete_link_count = " + structStorageStat.getTotalDeleteLinkCount());System.out.println("\tsuccess_delete_link_count = " + structStorageStat.getSuccessDeleteLinkCount());System.out.println("\ttotal_upload_bytes = " + structStorageStat.getTotalUploadBytes());System.out.println("\tsuccess_upload_bytes = " + structStorageStat.getSuccessUploadBytes());System.out.println("\ttotal_append_bytes = " + structStorageStat.getTotalAppendBytes());System.out.println("\tsuccess_append_bytes = " + structStorageStat.getSuccessAppendBytes());System.out.println("\ttotal_modify_bytes = " + structStorageStat.getTotalModifyBytes());System.out.println("\tsuccess_modify_bytes = " + structStorageStat.getSuccessModifyBytes());System.out.println("\ttotal_download_bytes = " + structStorageStat.getTotalDownloadloadBytes());System.out.println("\tsuccess_download_bytes = " + structStorageStat.getSuccessDownloadloadBytes());System.out.println("\ttotal_sync_in_bytes = " + structStorageStat.getTotalSyncInBytes());System.out.println("\tsuccess_sync_in_bytes = " + structStorageStat.getSuccessSyncInBytes());System.out.println("\ttotal_sync_out_bytes = " + structStorageStat.getTotalSyncOutBytes());System.out.println("\tsuccess_sync_out_bytes = " + structStorageStat.getSuccessSyncOutBytes());System.out.println("\ttotal_file_open_count = " + structStorageStat.getTotalFileOpenCount());System.out.println("\tsuccess_file_open_count = " + structStorageStat.getSuccessFileOpenCount());System.out.println("\ttotal_file_read_count = " + structStorageStat.getTotalFileReadCount());System.out.println("\tsuccess_file_read_count = " + structStorageStat.getSuccessFileReadCount());System.out.println("\ttotal_file_write_count = " + structStorageStat.getTotalFileWriteCount());System.out.println("\tsuccess_file_write_count = " + structStorageStat.getSuccessFileWriteCount());System.out.println("\tlast_heart_beat_time = " + structStorageStat.getLastHeartBeatTime());System.out.println("\tlast_source_update = " + structStorageStat.getLastSourceUpdate());System.out.println("\tlast_sync_update = " + structStorageStat.getLastSyncUpdate());System.out.println("\tlast_synced_timestamp = " + structStorageStat.getLastSyncedTimestamp());}}public static void listStorages1(TrackerClient trackerClient, TrackerServer trackerServer) throws Exception {StructStorageStat[] sss = trackerClient.listStorages(trackerServer, "group1");print(sss);}public static void listStorages2(TrackerClient trackerClient, TrackerServer trackerServer) throws Exception {StructStorageStat[] sss = trackerClient.listStorages(trackerServer, "group2", "192.168.25.138");print(sss);}public static void print(ServerInfo[] si) {int i = 1;for (ServerInfo serverInfo : si) {System.err.println(i++ + ". " + serverInfo.getIpAddr() + ":" + serverInfo.getPort());}}public static void getFetchStorages1(TrackerClient trackerClient, TrackerServer trackerServer) throws Exception {ServerInfo[] si = trackerClient.getFetchStorages(trackerServer, "group2", "M00/00/00/wKgZil70YPqAG6voHod5dWQOPIE969.mp4");print(si);}public static void getFetchStorages2(TrackerClient trackerClient, TrackerServer trackerServer) throws Exception {ServerInfo[] si = trackerClient.getFetchStorages1(trackerServer, "group2/M00/00/00/wKgZil70YPqAG6voHod5dWQOPIE969.mp4");print(si);} }?
總結(jié)
以上是生活随笔為你收集整理的012_Java操作FastDFS的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 007_FastDFS文件下载流程
- 下一篇: 011_fastdfs-client-j