【FastDFS】FastDFS上传图片

前言

    FastDFS是用c语言编写的一款开源的分布式文件系统。FastDFS为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。

引入jar包

    需要使用fastdfs_client_v1.20.jar,点击下载,并将此jar加入到工程中。

使用步骤

public void upload() throws Exception{
    //创建一个配置文件内容为tracker服务器的地址
    //使用全局对象来加载配置文件
    ClientGlobal.init("全局配置文件的全路径,该配置文件内容为<tracker_server=FastDFS服务器ip:端口号>");
    //创建一个trackerClient对象
    TrackerClient trackerClient = new TrackerClient();
    //通过trackerClient获得一个trackerServer对象
    TrackerServer trackerServer = trackerClient.getConnection();
    //创建一个strorageServer的引用,可以为null
    StorageServer storageServer = null;
    //创建一个storageClient,参数需要TrackerServer和StrorageServer
    StorageClient storageClient = new StorageClient(trackerServer,storageServer);
    //使用StorageClient上传文件
    String[] strings = storageClient.upload_file("待上传图片全路径", "图片格式,如jpg,不需要加.", null);
    for(String string : strings) {
        System.out.println(string);
    }
}

执行此方法后,控制台打印上传后图片的地址以及名称,如下:


文件索引信息包括

(1)组名:文件上传后所在的storage组名称,在文件上传成功后有storage服务器返回,需要客户端自行保存。

(2)虚拟磁盘路径:storage配置的虚拟路径,与磁盘选项store_path*对应。如果配置了store_path0则是M00,如果配置了store_path1则是M01,以此类推。

(3)数据两级目录:storage服务器在每个虚拟磁盘路径下创建的两级目录,用于存储数据文件。

(4)文件名:与文件上传时不同。是由存储服务器根据特定信息生成,文件名包含:源存储服务器IP地址、文件创建时间戳、文件大小、随机数和文件拓展名等信息。

上传成功后,访问http://FastDFS文件服务器ip/该全路径即可。

使用工具类

FastDFSClient.java

package cn.itcast.fastdfs.cliennt;

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);
    }
}

客户端使用

public void fastDfsClient() throws Exception{
    FastDFSClient fastDFSClient = new FastDFSClient("配置文件地址,可用classpath相对路径,工具类提供转换");
    String string = fastDFSClient.uploadFile("待上传图片全路径");
    System.out.println(string);
}

总结

    工具类的使用能极大的方便我们的编程,工具类的思想也就是面向对象的特性之——封装。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值