一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java fastdfs客戶端使用實例代碼

java fastdfs客戶端使用實例代碼

2021-03-28 16:10胡一生 Java教程

這篇文章主要介紹了java fastdfs客戶端使用實例代碼,簡單介紹了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稱為存儲服務器。

java fastdfs客戶端使用實例代碼

實例

一、創建一個maven的webproject,叫

file-manager:mvnarchetype:create-dgroupid=platform.activity.filemanager-dartifactid=file-manager-darchetypeartifactid=maven-archetype-webapp

二、定義一個fastdfs的客戶端文件fdfs_client.conf:

?
1
2
3
4
5
6
7
8
9
10
11
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

三、定義一個配置接口:

?
1
2
3
4
5
6
7
8
9
10
11
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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類,里面包含有上傳、刪除、獲取文件的方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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測試類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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 [email protected]
 */
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客戶端使用實例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

原文鏈接:http://www.cnblogs.com/go4mi/p/5809541.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费免费啪视频在线观播放 | 亚洲天堂导航 | 欧美一级专区免费大片 | 成人网18免费网站 | voyeur 中国女厕 亚洲女厕 | bl放荡受np双性 | 视频免费观看在线播放高清 | 男人jj视频 | 无遮挡激情| 亚洲黄色片免费看 | 四虎网站在线 | 叛佛 作者满栀小说免费阅读 | 日本午夜大片免费观看视频 | 久久精麻豆亚洲AV国产品 | 国产欧美va欧美va香蕉在线观看 | a∨79成人网 | 四虎影院在线免费观看 | 特级淫片大乳女子高清视频 | 亚洲国产成人久久综合区 | 亚洲视频在线一区二区三区 | 激情视频在线播放 | 国产亚洲精品第一综合linode | 国产精品制服丝袜白丝www | 欧美老女人b | 91制片厂制作传媒网站 | 成人小视频在线观看免费 | 深夜免费看 | 国产白白视频在线观看2 | 狠狠色婷婷| 精品美女国产互换人妻 | 欧美美女被爆操 | www.av网站| 精品视频一区二区三区 | 天天色踪合合 | 精品综合久久久久久8888 | 91色视 | 9久re在线观看视频精品 | 欧美日韩在线成人看片a | 99久久精品99999久久 | 风间由美一区二区av101 | 动漫女性扒开尿口羞羞漫画 |