成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

javafastdfs客戶端使用實(shí)例代碼

本文研究的主要是java fastdfs客戶端使用實(shí)例的相關(guān)內(nèi)容,具體實(shí)現(xiàn)如下。

創(chuàng)新互聯(lián)建站自2013年起,公司以網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、系統(tǒng)開發(fā)、網(wǎng)絡(luò)推廣、文化傳媒、企業(yè)宣傳、平面廣告設(shè)計等為主要業(yè)務(wù),適用行業(yè)近百種。服務(wù)企業(yè)客戶上千余家,涉及國內(nèi)多個省份客戶。擁有多年網(wǎng)站建設(shè)開發(fā)經(jīng)驗。為企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、創(chuàng)意設(shè)計、宣傳推廣等服務(wù)。 通過專業(yè)的設(shè)計、獨(dú)特的風(fēng)格,為不同客戶提供各種風(fēng)格的特色服務(wù)。

什么是FastDFS?

FastDFS是用c語言編寫的一款開源的分布式文件系統(tǒng)。FastDFS為互聯(lián)網(wǎng)量身定制,充分考慮了冗余備份、負(fù)載均衡、線性擴(kuò)容等機(jī)制,并注重高可用、高性能等指標(biāo),使用FastDFS很容易搭建一套高性能的文件服務(wù)器集群提供文件上傳、下載等服務(wù)。

FastDFS架構(gòu)

FastDFS架構(gòu)包括 Tracker server和Storage server。客戶端請求Tracker server進(jìn)行文件上傳、下載,通過Tracker server調(diào)度最終由Storage server完成文件上傳和下載。Tracker server作用是負(fù)載均衡和調(diào)度,通過Tracker server在文件上傳時可以根據(jù)一些策略找到Storage server提供文件上傳服務(wù)??梢詫racker稱為追蹤服務(wù)器或調(diào)度服務(wù)器。Storage server作用是文件存儲,客戶端上傳的文件最終存儲在Storage服務(wù)器上,Storage server沒有實(shí)現(xiàn)自己的文件系統(tǒng)而是利用操作系統(tǒng) 的文件系統(tǒng)來管理文件??梢詫torage稱為存儲服務(wù)器。

java fastdfs客戶端使用實(shí)例代碼

實(shí)例

一、創(chuàng)建一個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());
		}
	}
}

總結(jié)

以上就是本文關(guān)于java fastdfs客戶端使用實(shí)例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

網(wǎng)頁題目:javafastdfs客戶端使用實(shí)例代碼
URL分享:http://jinyejixie.com/article48/pppgep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計公司網(wǎng)站制作、電子商務(wù)網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化
砀山县| 枣庄市| 顺平县| 潮安县| 长沙县| 长丰县| 河曲县| 建水县| 昆明市| 克什克腾旗| 尤溪县| 固阳县| 乐山市| 内黄县| 扬中市| 太白县| 土默特左旗| 葵青区| 林州市| 武汉市| 小金县| 元朗区| 葫芦岛市| 宁都县| 陆丰市| 泾川县| 石林| 通辽市| 准格尔旗| 定南县| 太湖县| 江阴市| 长海县| 武强县| 镇坪县| 和政县| 平武县| 永平县| 陆河县| 江源县| 建始县|