java客户端操作fastdfs集群

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

你可能感兴趣的:(fdfs)