JAvaクライアント操作fastdfsクラスタ

8754 ワード

pomファイル構成

		
			junit
			junit
			4.11
		
		
			commons-fileupload
			commons-fileupload
			1.3.1
		
		
			commons-io
			commons-io
			2.0.1
		
		
			org.apache.commons
			commons-lang3
			3.1
		
		
			commons-logging
			commons-logging
			1.1.3
		
		
			log4j
			log4j
			1.2.17
		
		
			org.slf4j
			slf4j-api
			1.7.5
		
		
			org.slf4j
			slf4j-log4j12
			1.7.5
		
		
		
			org.csource
			fastdfs-client-java
			v1.24
		
	
fdfs_client.conf  ファイル:
connect_timeout = 10
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890


tracker_server = 192.168.1.251:22122
tracker_server = 192.168.1.252:22122

Clientツールクラス:
package com.sun.dfs.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

/**
 * 
 * @ClassName: FastDFSClient 
 * @Description: fdfs   
 * @author: sunqz
 * @date: 2017-6-3   3:17:46
 */
public class FastDFSClient {


	private static final String CONF_FILENAME = "src/main/resources/fdfs/fdfs_client.conf";
	private static StorageClient1 storageClient1 = null;

	private static Logger logger = Logger.getLogger(FastDFSClient.class);

	/**
	 *      .
	 */
	static {
		try {
			logger.info("=== CONF_FILENAME:" + CONF_FILENAME);
			ClientGlobal.init(CONF_FILENAME);
			TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
			TrackerServer trackerServer = trackerClient.getConnection();
			if (trackerServer == null) {
				logger.error("getConnection return null");
			}
			StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
			if (storageServer == null) {
				logger.error("getStoreStorage return null");
			}
			storageClient1 = new StorageClient1(trackerServer, storageServer);
		} catch (Exception e) {
			logger.error(e);
		}
	}
	
	/**
	 * 
	 * @Title: uploadFile 
	 * @Description:     
	 * @param fullpath       
	 * @return
	 * @return: String    id
	 */
	public static String uploadFile( String fullpath) {
		File file = new File(fullpath);
		FileInputStream fis = null;
		try {
			NameValuePair[] meta_list = null; // new NameValuePair[0];
			fis = new FileInputStream(file);
			byte[] file_buff = null;
			if (fis != null) {
				int len = fis.available();
				file_buff = new byte[len];
				fis.read(file_buff);
			}

			String fileid = storageClient1.upload_file1(file_buff, getFileExt(fullpath), meta_list);
			
		
			return fileid;
		} catch (Exception ex) {
			logger.error(ex);
			return null;
		}finally{
			if (fis != null){
				try {
					fis.close();
				} catch (IOException e) {
					logger.error(e);
				}
			}
		}
	}
	
	
	/**
	 * 
	 * @Title: uploadSlaveFile 
	 * @Description:       
	 * @param masterFileId     id
	 * @param fullpath        
	 * @param prefixName                     product_100010.png              product_100010_120x120.png
	 * @return
	 * @return: String   id
	 */
	public static String uploadSlaveFile(String masterFileId, String fullpath,String prefixName) {
		File file = new File(fullpath);
		FileInputStream fis = null;
		try {
			NameValuePair[] meta_list = null; // new NameValuePair[0];
			fis = new FileInputStream(file);
			byte[] file_buff = null;
			if (fis != null) {
				int len = fis.available();
				file_buff = new byte[len];
				fis.read(file_buff);
			}

			 String fileid =storageClient1.upload_file1(masterFileId,prefixName , fullpath, getFileExt(fullpath), null); 
	  
			return fileid;
		} catch (Exception ex) {
			logger.error(ex);
			return null;
		}finally{
			if (fis != null){
				try {
					fis.close();
				} catch (IOException e) {
					logger.error(e);
				}
			}
		}
	}

	

	/**
	 * 
	 * @Title: deleteFile 
	 * @Description:                           group1
	 * @param groupName
	 * @param fileName    :/M00/00/00/wKgB-lkdxUmAPb-QAAIbD3CxJDw317.jpg
	 * @return
	 * @return: int
	 */
	public static int deleteFile(String groupName, String fileName) {
		try {
			int result = storageClient1.delete_file(groupName == null ? "group1" : groupName, fileName);
			return result;
		} catch (Exception ex) {
			logger.error(ex);
			return 0;
		}
	}

	/**
	 * 
	 * @Title: deleteFile 
	 * @Description:   fileId       
	 * @param fileId
	 * @return
	 * @return: int
	 */
	public static int deleteFile(String fileId) {
		try {
			int result = storageClient1.delete_file1(fileId);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return 0;
		}
	}

	/**
	 * 
	 * @Title: modifyFile 
	 * @Description:                        
	 * @param oldFileId    id
	 * @param fullPath        
	 * @return
	 * @return: String
	 */
	public static String modifyFile(String oldFileId, String fullPath) {
		String fileid = null;
		try {
			//    
			fileid = uploadFile(fullPath);
			if (fileid == null) {
				return null;
			}
			//    
			int delResult = deleteFile(oldFileId);
			if (delResult != 0) {
				return null;
			}
		} catch (Exception ex) {
			logger.error(ex);
			return null;
		}
		return fileid;
	}

	/**
	 * 
	 * @Title: downloadFile 
	 * @Description:     
	 * @param fileId
	 * @return
	 * @return: InputStream
	 */
	public static InputStream downloadFile(String fileId) {
		try {
			byte[] bytes = storageClient1.download_file1(fileId);
			InputStream inputStream = new ByteArrayInputStream(bytes);
			return inputStream;
		} catch (Exception ex) {
			logger.error(ex);
			return null;
		}
	}

	/**
	 * 
	 * @Title: getFileExt 
	 * @Description:        (   ).
	 * @param fileName
	 * @return
	 * @return: String
	 */
	private static String getFileExt(String fileName) {
		if (StringUtils.isBlank(fileName) || !fileName.contains(".")) {
			return "";
		} else {
			return fileName.substring(fileName.lastIndexOf(".") + 1); //       
		}
	}
}

テストコード:
package com.sun.dfs.test;

import java.io.File;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;

import com.sun.dfs.utils.FastDFSClient;

/**
 * 
 * @ClassName: FastDFSTest 
 * @Description: TODO
 * @author: sunqz
 * @date: 2017-6-3   4:03:13
 */
public class FastDFSTest {
	
	/**
	 *     .
	 * @throws Exception
	 */
	public static void upload() throws Exception {
		String fullPath = "C:/Users/Dell/Desktop/1.jpg";
		File file = new File(fullPath);
		String fileId = FastDFSClient.uploadFile(fullPath);
		System.out.println("Upload local file " + fullPath + " ok, fileid=" + fileId);
		
	}
	
	/**
	 *     .
	 * @throws Exception
	 */
	public static void download() throws Exception {
		String fileId = "group2/M00/00/00/wKgB-lkdxUmAPb-QAAIbD3CxJDw317.jpg";
		InputStream inputStream = FastDFSClient.downloadFile(fileId);
		File destFile = new File("C:/Users/Dell/Desktop/2.jpg");
		FileUtils.copyInputStreamToFile(inputStream, destFile);
	}

	/**
	 *     
	 * @throws Exception
	 */
	public static void delete() throws Exception {
		String fileId = "group2/M00/00/00/wKgB-lkdxUmAPb-QAAIbD3CxJDw317.jpg";
		int result = FastDFSClient.deleteFile(fileId);
		System.out.println(result);
		System.out.println(result == 0 ? "    " : "    :" + result);
	}


	
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		//upload();
		//download();
		delete();

	}

}

ソースのダウンロードアドレス:http://download.csdn.net/detail/sunqingzhong44/9859649