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  [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());
        }
  }
}
时间: 2024-10-19 12:52:32

fastdfs使用的相关文章

FastDFS+Nginx问题及修复

FastDFS+nginx问题及修复:     1.[error] 30000#0: *1 open() "/usr/local/nginx/html/group1/M00/00 /00/wKgAA1cLh12AI0kfAAAADzbdjmQ50_big.html           "failed (2: No such file or directory), client: 192.168.0.181, server:localhost, request:            &

V 11 FastDFS

一.概念: https://github.com/happyfish100/fastdfs FastDFS is an open source high performance distributed file system (DFS). It's major functions include: file storing, file syncing and file accessing, and design for high capacity and load balance. https:

FastDFS的php和nginx模块配置

一.FastDFS和php整合 1.安装php # 安装依赖包 yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel cu

[项目构建 五]babasport ajax图片上传及FastDFS入门案例.

今天来开始写图片上传的功能, 现在的图片上传都讲求 上传完成后立刻回显且页面不刷新, 这里到底是怎么做的呢? 当然是借助于ajax了, 但是ajax又不能提交表单, 这里我们还要借助一个插件: jquery.form.js剩下的一个是FastDFS, 那么什么是FastDFS呢? FastDFS是一个开源的轻量级分布式文件系统,由跟踪服务器(tracker server).存储服务器(storage server)和客户端(client)三个部分组成,主要解决了海量数据存储问题,特别适合以中小文

FastDFS之java客户端使用

为了方便应用程序的访问FastDFS,官网提供了fastdfs-client-java,以便更好的与应用程序结合使用. 下载fastdfs-client-java源码添加到项目工程里面,添加配置文件:fdfs_client.conf 这个jar包在中央仓库是没有的,我们可以将源码下载下来,使用maven install安装到本地仓库. 附上pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=

安装fastdfs文件系统

1.下载源码包 需要下载的源码包: fastDFS源代码:FastDFS_v5.01.tar.gz fastDFS的nginx模块源代码:fastdfs-nginx-module_v1.15.tar.gz nginx服务器源代码:nginx-1.4.7.tar.gz nginx依赖的pcre库源代码:pcre-8.34.tar.gz nginx依赖的zlib库源代码:zlib-1.2.8.tar.gz 可以我的微云中下载http://url.cn/4Byr7Ky,也可以网上自行下载: 2.安装,

FastDFS

FastDFS系统是一个类似于Google FS的轻量级分布式文件系统,用C语言实现,支持包括Linux.FreeBSD.AIX等UNIX系统.从诞生之初至今,FastDFS已经推出了31个版本.下面是FastDFS的架构示意图: 特别适合以中小文件(建议范围:4KB < file_size <500MB)为载体的在线服务. fastdfs做分布式缺点就是不支持挂载硬盘!性能确实不错

fastdfs集群

基础环境说明 6台服务器 两台tracker  4台storages 192.168.56.131 tracker 服务器 edu-dfs-tracker-1 192.168.56.132 tracker 服务器 edu-dfs-tracker-2 192.168.56.135 storage服务器 edu-dfs-storage-group1-1 192.168.56.136 storage服务器 edu-dfs-storage-group1-2 192.168.56.137 storage服

FastDFS 安装配置

本文以  FastDFS  5.03 源码包作为基础的介绍. 1.背景 公司购买了一套语音分析系统,每天有30G的电话录话需要转换成为文本,然后在语音系统自定义的格式中打上标签,以提供给WEB前端搜索与回放.打标签后的数据约是原始录音的10倍大小,也就是每个月大概有 30G x 10倍 x 30天 = 9T 的数据需要存放,计划最长保留一年的历史数据,总共约 9T x 12个月 = 108T . 在这里,需要考虑到的问题是 A.容量,108T是理想数值,还需要计算做Raid损耗 + 文件系统损耗

FastDFS安装部署

1.安装gcc编译器 yum install -y gcc 2.解压安装包 git clone https://github.com/happyfish100/libfastcommon.git cd libfastcommon ./make.sh && ./make.sh install 上传fastdfs源码包到服务器,这里版本用fastdfs_v5.0.8.tar.gz tar xf fastdfs_v5.0.8.tar.gz cd FastDFS ./make.sh &&a