FastDFS应用代码

上传附件:

    @Test
    public void uploadFile() throws Exception {
        //1、向工程中添加jar包
        //2、创建一个配置文件。配置tracker服务器地址
        //3、加载配置文件,配置文件中tracker_server=192.168.66.129:22122
        ClientGlobal.init("D:/taotaoworkspace/taotao-manager-web/src/main/resources/resource/client.conf");
        //4、创建一个TrackerClient对象。
        TrackerClient trackerClient = new TrackerClient();
        //5、使用TrackerClient对象获得trackerserver对象。
        TrackerServer trackerServer = trackerClient.getConnection();
        //6、创建一个StorageServer的引用null就可以。
        StorageServer storageServer = null;
        //7、创建一个StorageClient对象。trackerserver、StorageServer两个参数。
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
        //8、使用StorageClient对象上传文件。
        String[] strings = storageClient.upload_file("D:/test.png", "png", null);
        for (String string : strings) {
            System.out.println(string);

        }
    }

执行结果为:

group1
M00/00/00/wKhCgVkoQjSAUIkfAALlhUPEEGo827.png

返回个数组,string[0]位group号,string[1]位路径

上传时一定要把fdfs服务器的22122端口和storage的端口23000打开,否则会报socket连不上

新建个上传工具类FastDFSClient如下:

    @Test
    public void testFastDfsClient() throws Exception {
        FastDFSClient fastDFSClient = new FastDFSClient("D:/taotaoworkspace/taotao-manager-web/src/main/resources/resource/client.conf");
        String string = fastDFSClient.uploadFile("D:/test.png");
        System.out.println(string);
    }

输出:

group1/M00/00/00/wKhCgVkoQsSANBGXAALlhUPEEGo756.png

FastDfsClient类:

package com.taotao.utils;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

public class FastDFSClient {

    private TrackerClient trackerClient = null;
    private TrackerServer trackerServer = null;
    private StorageServer storageServer = null;
    private StorageClient1 storageClient = null;

    public FastDFSClient(String conf) throws Exception {
        if (conf.contains("classpath:")) {
            conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
        }
        ClientGlobal.init(conf);
        trackerClient = new TrackerClient();
        trackerServer = trackerClient.getConnection();
        storageServer = null;
        storageClient = new StorageClient1(trackerServer, storageServer);
    }

    /**
     * 上传文件方法
     * <p>Title: uploadFile</p>
     * <p>Description: </p>
     * @param fileName 文件全路径
     * @param extName 文件扩展名,不包含(.)
     * @param metas 文件扩展信息
     * @return
     * @throws Exception
     */
    public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
        String result = storageClient.upload_file1(fileName, extName, metas);
        return result;
    }

    public String uploadFile(String fileName) throws Exception {
        return uploadFile(fileName, null, null);
    }

    public String uploadFile(String fileName, String extName) throws Exception {
        return uploadFile(fileName, extName, null);
    }

    /**
     * 上传文件方法
     * <p>Title: uploadFile</p>
     * <p>Description: </p>
     * @param fileContent 文件的内容,字节数组
     * @param extName 文件扩展名
     * @param metas 文件扩展信息
     * @return
     * @throws Exception
     */
    public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {

        String result = storageClient.upload_file1(fileContent, extName, metas);
        return result;
    }

    public String uploadFile(byte[] fileContent) throws Exception {
        return uploadFile(fileContent, null, null);
    }

    public String uploadFile(byte[] fileContent, String extName) throws Exception {
        return uploadFile(fileContent, extName, null);
    }
}

文件下载:

storage里配置了nginx,可以通过nginx直接访问,nginx配置文件nginx.conf

    server {
        listen       8080;
        listen       localhost;      

        location ~/group([0-9])/M00 {
            ngx_fastdfs_module;
        }
    }

可以url直接通过如下访问

192.168.66.129:8080/group1/M00/00/00/wKhCgVkoQsSANBGXAALlhUPEEGo756.png

如果tracker中设置了nginx,负载均衡,nginx.conf配置

    upstream fdfs_group1 {
         server 192.168.224.25:8080 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.224.26:8080 weight=1 max_fails=2 fail_timeout=30s;
    }

    upstream fdfs_group2 {
         server 192.168.224.28:8080 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.224.29:8080 weight=1 max_fails=2 fail_timeout=30s;
    }

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /group1/M00 {
            proxy_next_upstream http_502 http_504 error timeout invalid_header;
            proxy_cache http-cache;
            proxy_cache_valid  200 304 12h;
            proxy_cache_key $uri$is_args$args;
            proxy_pass http://fdfs_group1;
            expires 30d;
        }

        location /group2/M00 {
            proxy_next_upstream http_502 http_504 error timeout invalid_header;
            proxy_cache http-cache;
            proxy_cache_valid  200 304 12h;
            proxy_cache_key $uri$is_args$args;
            proxy_pass http://fdfs_group2;
            expires 30d;
        }

        location ~/purge(/.*) {
            allow 127.0.0.1;
            allow 192.168.224.0/24;
            deny all;
            proxy_cache_purge http-cache  $1$is_args$args;
        }     
时间: 2024-08-28 05:04:47

FastDFS应用代码的相关文章

fastdfs 集群配置

fastdfs 简介FastDFS是一个国产开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载体的在线服务,如相册网站.视频网站等等.FastDFS服务端有两个角色:跟踪器(tracker)和存储节点(storage).跟踪器主要做调度工作,在访问上起负载均衡的作用.官方论坛: http://www.csource.org下载地址: http://sourceforge.net/pro

分布式文件系统fastDFS

分布式文件系统fastDFS 1.什么是FastDFS FastDFS是一个开源的轻量级分布式文件系统.它解决了大数据量存储和负载均衡等问题.特别适合以中小文件(建议范围:4KB < file_size <500MB)为载体的在线服务,如相册网站.视频网站等等.在UC基于FastDFS开发向用户提供了:网盘,社区,广告和应用下载等业务的存储服务. 2.FastDFS架构和原理 FastDFS服务端有三个角色:跟踪服务器(tracker server).存储服务器(storage server)

开源分布式文件系统比较

要使用分布式文件系统来降低成本,于是搜寻了开源的分布式文件系统. 经过安装部署和测试,把我使用中碰到的一些问题总结一下, 希望对大家有所帮助, 我也有一些问题没有搞懂,希望和大家一起交流,共同进步. 第一:CEPH 网上搜索了一些资料, 说 ceph 性能最高,C++编写的代码,支持Fuse,并且没有单点故障依赖, 于是下载安装, 由于 ceph 使用 btrfs 文件系统, 而btrfs 文件系统需要 Linux 2.6.34 以上的内核才支持, 显然我使用的 RHEL5 的内核还不支持 bt

各种分布式文件系统的比较

1.MooseFS 支持FUSE,相对比较轻量级,对master服务器有单点依赖,用perl编写,性能相对较差,国内用的人比较多,易用,稳定,对小文件很高效. + 支持文件元信息 + mfsmount 很好用 + 编译依赖少,文档全,默认配置很好 + mfshdd.cfg 加 * 的条目会被转移到其它 chunk server,以便此 chunk server 安全退出 + 不要求 chunk server 使用的文件系统格式以及容量一致 + 开发很活跃 + 可以以非 root 用户身份运行 +

错误和问题解决的成本

问题描写叙述 错误 数据收集 根本原因 版本号   组件:数据修复           在一个实际成本组织中,(平均,先进先出,后进先出) 一个或更 多的下面情况可能发生: 1.导航到物料成本历史表单上的数量信息,与现有量表单的数量不匹配的记录 2. 一些物料前期已计成本的数量与前面的事务处理历史表单的数量不匹配 3. 全部的库存值报表与事务处理值报表不匹配 4. 存货层次成本更新表单的总数量与现有量数量表单不匹配(只在先进先出/后进先出) 5.这些症状的不论什么一个意味着 MMT-CQL不匹配

20.fastDFS集群java代码测试

1.工程结构 2.代码内容 FastdfsClientTest.java代码 package cn.itcast.fastdfs.cliennt; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.UUID; import org.csource.common.NameVal

FastDFS的配置、部署与API使用解读(2)以字节方式上传文件的客户端代码

1.下载FastDFS的API FastDFS提供Java和PHP等语言的客户端API.可以到FastDFS在Google Code的项目主页 http://code.google.com/p/fastdfs/downloads/list 下载.本文以Java API为例. 2.调用API的上传接口 通过Servlet得到InputStream.文件名称和文件长度,然后通过调用FastDFS提供的Java API把文件上传到FastDFS服务器.下段代码中的getFileBuffer可参考本博客

django中使用FastDFS分布式文件系统接口代码实现文件上传、下载、更新、删除

运维使用docker部署好之后FastDFS分布式文件系统之后,提供给我接口如下: fastdfs tracker 192.168.1.216 192.168.1.217 storage 192.168.1.216 192.168.1.217 我们只需要在配置文件中进行配置即可,然后利用客户端提供的接口通过简单的代码就可以将文件上传到分布式文件系统中 至于内部实现机制,可以参考我的另外一篇博客:分布式文件系统Fastdfs原理及部署 再次提醒在安装客户端可能会遇到各种不可控的因素,导致你上传失败

FastDFS的配置、部署与API使用解读(3)以流的方式上传文件的客户端代码(转)

调用的API为: String[] upload_file( String group_name,//组名,不指定则可设为null long file_size,//文件大小,必须制定 UploadCallback callback,//回调 String file_ext_name, NameValuePair[] meta_list ) 1 /** 2 * Upload File to DFS, directly transferring java.io.InputStream to jav