java fastdfs客户端使用实例代码
本文研究的主要是java fastdfs客户端使用实例的相关内容,具体实现如下。
什么是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称为存储服务器。
实例
一、创建一个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客户端使用实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!