成都创新互联网站制作重庆分公司

javafastdfs客户端使用实例代码

本文研究的主要是java fastdfs客户端使用实例的相关内容,具体实现如下。

创新互联建站自2013年起,公司以网站设计、网站建设、系统开发、网络推广、文化传媒、企业宣传、平面广告设计等为主要业务,适用行业近百种。服务企业客户上千余家,涉及国内多个省份客户。拥有多年网站建设开发经验。为企业提供专业的网站建设、创意设计、宣传推广等服务。 通过专业的设计、独特的风格,为不同客户提供各种风格的特色服务。

什么是FastDFS?

FastDFS是用c语言编写的一款开源的分布式文件系统。FastDFS为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。

FastDFS架构

FastDFS架构包括 Tracker server和Storage server。客户端请求Tracker server进行文件上传、下载,通过Tracker server调度最终由Storage server完成文件上传和下载。Tracker server作用是负载均衡和调度,通过Tracker server在文件上传时可以根据一些策略找到Storage server提供文件上传服务。可以将tracker称为追踪服务器或调度服务器。Storage server作用是文件存储,客户端上传的文件最终存储在Storage服务器上,Storage server没有实现自己的文件系统而是利用操作系统 的文件系统来管理文件。可以将storage称为存储服务器。

java fastdfs客户端使用实例代码

实例

一、创建一个maven的webproject,叫

file-manager:mvnarchetype:create-DgroupId=platform.activity.filemanager-DartifactId=file-manager-DarchetypeArtifactId=maven-archetype-webapp

二、定义一个fastDFS的客户端文件fdfs_client.conf:

class="properties" name="code">connect_timeout = 2
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.156:22122
#tracker_server = 192.168.1.188:22122

#storage_server = 192.168.1.155:23000 #no need here

三、定义一个配置接口:

package com.chuanliu.platform.activity.fm.manager;
import java.io.Serializable;
public interface FileManagerConfig extends Serializable {
 public static final String FILE_DEFAULT_WIDTH   = "120";
 public static final String FILE_DEFAULT_HEIGHT   = "120";
 public static final String FILE_DEFAULT_AUTHOR   = "Diandi";
 public static final String PROTOCOL = "http://";
 public static final String SEPARATOR = "/";
 public static final String TRACKER_NGNIX_PORT   = "8080";
 public static final String CLIENT_CONFIG_FILE  = "fdfs_client.conf"; 
}

四、封装一个FastDFS文件Bean

package com.chuanliu.platform.activity.fm.manager;
public class FastDFSFile implements FileManagerConfig {
 private static final long serialVersionUID = -996760121932438618L;
 private String name;
 private byte[] content;
 private String ext;
 private String height = FILE_DEFAULT_HEIGHT;
 private String width = FILE_DEFAULT_WIDTH;
 private String author = FILE_DEFAULT_AUTHOR;
 public FastDFSFile(String name, byte[] content, String ext, String height,String width, String author) {
  super();
  this.name = name;
  this.content = content;
  this.ext = ext;
  this.height = height;
  this.width = width;
  this.author = author;
 }
 public FastDFSFile(String name, byte[] content, String ext) {
  super();
  this.name = name;
  this.content = content;
  this.ext = ext;
 }
 public byte[] getContent() {
  return content;
 }
 public void setContent(byte[] content) {
  this.content = content;
 }
 public String getExt() {
  return ext;
 }
 public void setExt(String ext) {
  this.ext = ext;
 }
 public String getHeight() {
  return height;
 }
 public void setHeight(String height) {
  this.height = height;
 }
 public String getWidth() {
  return width;
 }
 public void setWidth(String width) {
  this.width = width;
 }
 public String getAuthor() {
  return author;
 }
 public void setAuthor(String author) {
  this.author = author;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 } 
}

五、定义核心的FileManager类,里面包含有上传、删除、获取文件的方法:

package com.chuanliu.platform.activity.fm.manager;
import java.io.File;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
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;
import com.chuanliu.platform.activity.basic.util.LoggerUtils;
public class FileManager implements FileManagerConfig {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(FileManager.class);
	private static TrackerClient trackerClient;
	private static TrackerServer trackerServer;
	private static StorageServer storageServer;
	private static StorageClient storageClient;
	static {
		// Initialize Fast DFS Client configurations
		try {
			String classPath = new File(FileManager.class.getResource("/").getFile()).getCanonicalPath();
			String fdfsClientConfigFilePath = classPath + File.separator + CLIENT_CONFIG_FILE;
			logger.info("Fast DFS configuration file path:" + fdfsClientConfigFilePath);
			ClientGlobal.init(fdfsClientConfigFilePath);
			trackerClient = new TrackerClient();
			trackerServer = trackerClient.getConnection();
			storageClient = new StorageClient(trackerServer, storageServer);
		}
		catch (Exception e) {
			LoggerUtils.error(logger, e);
		}
	}
	public static String upload(FastDFSFile file) {
		LoggerUtils.info(logger, "File Name: " + file.getName() + "File Length: " + file.getContent().length);
		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", "Diandi");
		long startTime = System.currentTimeMillis();
		String[] uploadResults = null;
		try {
			uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);
		}
		catch (IOException e) {
			logger.error("IO Exception when uploadind the file: " + file.getName(), e);
		}
		catch (Exception e) {
			logger.error("Non IO Exception when uploadind the file: " + file.getName(), e);
		}
		logger.info("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
		if (uploadResults == null) {
			LoggerUtils.error(logger, "upload file fail, error code: " + storageClient.getErrorCode());
		}
		String groupName     = uploadResults[0];
		String remoteFileName  = uploadResults[1];
		String fileAbsolutePath = PROTOCOL + trackerServer.getInetSocketAddress().getHostName() 
		    + SEPARATOR
		    + TRACKER_NGNIX_PORT
		    + SEPARATOR 
		    + groupName 
		    + SEPARATOR 
		    + remoteFileName;
		LoggerUtils.info(logger, "upload file successfully!!! " +"group_name: " + groupName + ", remoteFileName:"
		    + " " + remoteFileName);
		return fileAbsolutePath;
	}
	public static FileInfo getFile(String groupName, String remoteFileName) {
		try {
			return storageClient.get_file_info(groupName, remoteFileName);
		}
		catch (IOException e) {
			logger.error("IO Exception: Get File from Fast DFS failed", e);
		}
		catch (Exception e) {
			logger.error("Non IO Exception: Get File from Fast DFS failed", e);
		}
		return null;
	}
	public static void deleteFile(String groupName, String remoteFileName) throws Exception {
		storageClient.delete_file(groupName, remoteFileName);
	}
	public static StorageServer[] getStoreStorages(String groupName) throws IOException {
		return trackerClient.getStoreStorages(trackerServer, groupName);
	}
	public static ServerInfo[] getFetchStorages(String groupName, String remoteFileName) throws IOException {
		return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
	}
}

六、Unit Test测试类

package manager;
import java.io.File;
import java.io.FileInputStream;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.ServerInfo;
import org.csource.fastdfs.StorageServer;
import org.junit.Test;
import org.springframework.util.Assert;
import com.chuanliu.platform.activity.fm.manager.FastDFSFile;
import com.chuanliu.platform.activity.fm.manager.FileManager;
/**
 * @author Josh Wang(Sheng)
 *
 * @email josh_wang23@hotmail.com
 */
public class TestFileManager {
	@Test
	 public void upload() throws Exception {
		File content = new File("C:\\520.jpg");
		FileInputStream fis = new FileInputStream(content);
		byte[] file_buff = null;
		if (fis != null) {
			int len = fis.available();
			file_buff = new byte[len];
			fis.read(file_buff);
		}
		FastDFSFile file = new FastDFSFile("520", file_buff, "jpg");
		String fileAbsolutePath = FileManager.upload(file);
		System.out.println(fileAbsolutePath);
		fis.close();
	}
	@Test
	 public void getFile() throws Exception {
		FileInfo file = FileManager.getFile("group1", "M00/00/00/wKgBm1N1-CiANRLmAABygPyzdlw073.jpg");
		Assert.notNull(file);
		String sourceIpAddr = file.getSourceIpAddr();
		long size = file.getFileSize();
		System.out.println("ip:" + sourceIpAddr + ",size:" + size);
	}
	@Test
	 public void getStorageServer() throws Exception {
		StorageServer[] ss = FileManager.getStoreStorages("group1");
		Assert.notNull(ss);
		for (int k = 0; k < ss.length; k++){
			System.err.println(k + 1 + ". " + ss[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + ss[k].getInetSocketAddress().getPort());
		}
	}
	@Test
	 public void getFetchStorages() throws Exception {
		ServerInfo[] servers = FileManager.getFetchStorages("group1", "M00/00/00/wKgBm1N1-CiANRLmAABygPyzdlw073.jpg");
		Assert.notNull(servers);
		for (int k = 0; k < servers.length; k++) {
			System.err.println(k + 1 + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
		}
	}
}

总结

以上就是本文关于java fastdfs客户端使用实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!


网页题目:javafastdfs客户端使用实例代码
本文路径:http://cxhlcq.com/article/pppgep.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部