FastDFS分布文件系统

FastDFS是为互联网应用量身定做的一套分布式文件存储系统,非常适合用来存储用户图片、视频、文档等文件。对于互联网应用,和其他分布式文件系统相比,优势非常明显。
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">
import java.io.File;
import java.io.FileInputStream;

import org.apache.log4j.Logger;
import org.csource.common.IniFileReader;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.ServerInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;




public class FastDFSUtils
{
	
	private static Logger log = Logger.getLogger(FastDFSUtils.class);
	

	public static String getFileName(String filePath)
	{
		String fileName = null;
		if (filePath != null)
		{
			int index = filePath.replace("\\", "/").lastIndexOf("/");
			fileName = filePath.substring(index);
		}
		return fileName;
	}
	

	public static String getFileFormat(String fileName)
	{
		String fileFormat = null;
		if (fileName != null && fileName.contains("."))
		{
			String[] arry = fileName.split("[.]");
			fileFormat = arry[1];
		}
		return fileFormat;
	}
	

	public static FileVO upload(String filePath, String configRootPath,
			String oldFileName, String fileFormat)
		throws Exception
	{
		File file = new File(filePath);
		FileVO fileVo = upload(file, configRootPath, oldFileName, fileFormat);
		return fileVo;
	}
	
	
	public static FileVO upload(File file, String configRootPath,
			String oldFileName, String fileFormat)
		throws Exception
	{
		boolean isOK = false;
		long uploadTime = 0;
		String groupName = null;
		String newFileName = null;
		long fileSize = 0;
		
		String classPath = configRootPath == null ? new File(FastDFSUtils.class
				.getResource("/").getFile()).getCanonicalPath()
				: configRootPath;
		String configFilePath = classPath + File.separator + "fdfs_client.conf";
		if (new File(configFilePath.replace("%20", " ")).exists())
		{
			System.out.println("配置文件:" + configFilePath);
			
			ClientGlobal.init(configFilePath.replace("%20", " "));
			IniFileReader iniReader = new IniFileReader(configFilePath.replace(
					"%20", " "));
			groupName = iniReader.getStrValue("groupName");
			TrackerClient trackerClient = new TrackerClient();
			TrackerServer trackerServer = trackerClient.getConnection();
			
			StorageServer storageServer = null;
			StorageClient storageClient = new StorageClient(trackerServer,
					storageServer);
			
			NameValuePair[] meta_list = new NameValuePair[3];
			meta_list[0] = new NameValuePair("width", "120");
			meta_list[1] = new NameValuePair("heigth", "120");
			meta_list[2] = new NameValuePair("author", "gary");
			FileInputStream fis = new FileInputStream(file);
			byte[] file_buff = null;
			if (fis != null)
			{
				int len = fis.available();
				file_buff = new byte[len];
				fis.read(file_buff);
			}
			log.info("file length: " + file_buff.length);
			
			StorageServer[] storageServers = trackerClient.getStoreStorages(
					trackerServer, groupName);
			if (storageServers == null)
			{
				log.error("get store storage servers fail, error code: "
						+ storageClient.getErrorCode());
			}
			else
			{
				log.info("store storage servers count: "
						+ storageServers.length);
				for (int k = 0; k < storageServers.length; k++)
				{
					log.info(k
							+ 1
							+ ". "
							+ storageServers[k].getInetSocketAddress()
									.getAddress().getHostAddress()
							+ ":"
							+ storageServers[k].getInetSocketAddress()
									.getPort());
				}
				
			}
			
			long startTime = System.currentTimeMillis();
			/*
			 * String[] results = storageClient.upload_file(file_buff,
			 * fileFormat, meta_list);
			 */
			String[] results = storageClient.upload_file(groupName, file_buff,
					fileFormat, meta_list);
			uploadTime = System.currentTimeMillis() - startTime;
			log.info("upload_file time used: " + uploadTime + " ms");
			
			fis.close();
			
			if (results == null)
			{
				log.error("upload file fail, error code: "
						+ storageClient.getErrorCode());
				
				throw new Exception("upload file fail, error code: "
						+ storageClient.getErrorCode());
			}
			
			isOK = true;
			// groupName = results[0];
			newFileName = results[1];
			log.info("group_name: " + groupName + ", remote_filename: "
					+ newFileName);
			log.info(storageClient.get_file_info(groupName, newFileName));
			
			ServerInfo[] servers = trackerClient.getFetchStorages(
					trackerServer, groupName, newFileName);
			if (servers == null)
			{
				log.error("get storage servers fail, error code: "
						+ trackerClient.getErrorCode());
			}
			else
			{
				log.info("storage servers count: " + servers.length);
				for (int k = 0; k < servers.length; k++)
				{
					log.info(k + 1 + ". " + servers[k].getIpAddr() + ":"
							+ servers[k].getPort());
				}
				
			}
		}
		else
		{
			log.error("配置文件不存在!" + configFilePath);
			throw new Exception("配置文件不存在!");
		}
		
		FileVO fileVo = new FileVO(isOK, groupName, oldFileName, newFileName,
				fileSize, uploadTime, fileFormat);
		return fileVo;
	}
	
	
	public static byte[] download(String groupName, String fileName,
			String configRootPath)
		throws Exception
	{
		String classPath = configRootPath == null ? new File(FastDFSUtils.class
				.getResource("/").getFile()).getCanonicalPath()
				: configRootPath;
		String configFilePath = classPath + File.separator + "fdfs_client.conf";
		ClientGlobal
				.init(configFilePath.replace("%20", " ").replace("\\", "/"));
		TrackerClient trackerClient = new TrackerClient();
		TrackerServer trackerServer = trackerClient.getConnection();
		StorageServer storageServer = null;
		StorageClient storageClient = new StorageClient(trackerServer,
				storageServer);
		byte[] bytes = null;
		bytes = storageClient.download_file(groupName, fileName);
		return bytes;
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值