012_Java操作FastDFS

1. 创建一个名为FastDFS的Java工程, 并导入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 #网络建立连接超时时间
network_timeout = 30 #网络发送和接收数据超时时间
charset = UTF-8 #编码方式
http.tracker_http_port = 8888 #http的端口号
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
# tracker服务器
tracker_server = 192.168.25.135:22122
tracker_server = 192.168.25.137:22122
tracker_server = 192.168.25.138:22122

3. 上传文件

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);
	}
	
	/**
	 * 通过字节数组上传文件到储存服务器, 适合上传小文件
	 */
	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);
	}
	
	/**
	 * 通过文件名上传文件到储存服务器, 适合上传小文件
	 */
	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);
	}
	
	/**
	 * 通过字节数组上传文件到指定卷/组的储存服务器, 适合上传小文件
	 */
	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);
	}

	/**
	 * 通过字节数组上传文件到储存服务器(可以设置偏移量和长度), 适合上传小文件
	 */
	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);
	}
	
	/**
	 * 设置上传文件回调上传文件到储存服务器, 适合上传大文件
	 */
	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() {
			@Override
			public 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);
	}
	
	/**
	 * 通过字节数组上传文件到指定卷/组的储存服务器(可以设置偏移量和长度), 适合上传小文件
	 */
	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);
	}
	
	/**
	 * 通过字节数组上传关联文件到指定卷/组的储存服务器, 适合上传小文件
	 * 如: 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);
	}
	
	/**
	 * 通过文件名上传关联文件到指定卷/组的储存服务器, 适合上传小文件
	 */
	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);
	}
	
	/**
	 * 设置上传文件回调上传关联文件到指定卷/组的文件到储存服务器, 适合上传大文件
	 */
	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() {
			@Override
			public 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);
	}

	/**
	 * 通过字节数组上传关联文件到指定卷/组的储存服务器(可以设置偏移量和长度) 适合上传小文件
	 */
	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);
	}
	
	/**
	 * 从存储服务器上下载文件, 返回字节数组, 适合下载小文件
	 */
	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"));
	}
	
	/**
	 * 从存储服务器上下载文件, 返回0下载成功, 可以设置下载回调, 适合下载大文件
	 */
	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;
			
			@Override
			public 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失败]");
	}
	
	/**
	 * 从存储服务器上下载文件, 返回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失败]");
	}

	/**
	 * 从存储服务器上下载文件, 返回字节数组, 可以设置偏移量和下载字节数, 下载字节数设置为0, 下载从偏移量开始的所有剩余字节, 适合下载小文件
	 */
	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"));
	}
	
	/**
	 * 从存储服务器上下载文件, 返回0下载成功, 可以设置偏移量和下载字节数, 下载字节数设置为0, 下载从偏移量开始的所有剩余字节, 可以设置下载回调, 适合下载大文件
	 */
	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;
			
			@Override
			public 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失败]");
	}

	/**
	 * 从存储服务器上下载文件, 返回0下载成功, 可以设置偏移量和下载字节数, 下载字节数设置为0, 下载从偏移量开始的所有剩余字节, 适合下载小文件
	 */
	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)从储存服务器上删除文件, 
 * 返回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() {
			@Override
			public 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() {
			@Override
			public 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. 存储服务器信息

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;

/**
 * 获取存储服务器信息
 */
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);
	}
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值