FastDFS蛋疼的集群和负载均衡(十)之编写FastDFSUtil

Interesting things

因为元旦,公司放3天假。但是这3天里面也不敢松懈,继续撸FastDFS了。

What did you do today

  • 我们查看fastdfs-client-java的源码,根据自己的需求封装FastDFSUtil,核心方法为upload()、download()、delete()、getFileInfo()、getFileMetaData()。代码没什么好说的,用到的设计模式是单例模式,是用静态内部类实现的单例。
public class FastDFSUtil {

    private static String classPath = FastDFSUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath();

    private static String FASTDFS_CLIENT_CONF = classPath + "fdfs_client.conf";

    private static TrackerClient trackerClient = null;

    private static TrackerServer trackerServer = null;

    private static StorageClient storageClient = null;

    private static StorageServer storageServer = null;

    private FastDFSUtil () {
        FastDFSUtil.init();
    }

    private static void init() {
        try {
            ClientGlobal.init(FASTDFS_CLIENT_CONF);
            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageServer = trackerClient.getStoreStorage(trackerServer);
            storageClient = new StorageClient(trackerServer, storageServer);

        } catch (Exception e) {
            throw new RuntimeException("init exception");
        }
    }

    public static FastDFSUtil getInstance() {
        return FastDFSUtilHolder.INSTANCE;
    }

    private static class FastDFSUtilHolder {
        private static final FastDFSUtil INSTANCE = new FastDFSUtil();
    }

    public static String upload(String maydayImg, String fileExtName, NameValuePair[] metaDataList) {
        try {
            String fileIds[] = storageClient.upload_file(maydayImg, fileExtName, metaDataList);

            System.out.println(fileIds.length);
            System.out.println("group:" + fileIds[0]);
            System.out.println("path:" + fileIds[1]);

            return fileIds[0] + fileIds[1];
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("upload exception");
        }
    }

    public static void download(String group, String file, String storePath) {
        try {
            byte[] data = storageClient.download_file(group, file);
            IOUtils.write(data, new FileOutputStream(new File(storePath
                    + "/" + file.substring(file.lastIndexOf("/") + 1))));
            System.out.println("download success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("download exception");
        }
    }

    public static FileInfo getFileInfo(String group, String file) {
        try {
            FileInfo fileInfo = storageClient.get_file_info(group, file);
            System.out.println("source ip=" + fileInfo.getSourceIpAddr());
            System.out.println("file sizes=" + fileInfo.getFileSize());
            System.out.println("timestamp=" + fileInfo.getCreateTimestamp());
            System.out.println("crc32=" + fileInfo.getCrc32());

            return fileInfo;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("gets file info exception");
        }
    }

    public static NameValuePair[] getFileMataData(String group, String file) {
        try {
            NameValuePair[] metas = storageClient.get_metadata(group, file);
            return metas;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("gets file metadata exception");
        }
    }

    public static void delete(String group, String file) {
        try {
            storageClient.delete_file(group, file);
            System.out.println("detele file success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("deletes file exception");
        }
    }
}
  • 整个项目结构是这样子的。

image.png

  • resources目录下,还需要添加fdfs_client.conf。具体如下:
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8000
http.anti_steal_token = no
http.secret_key = FastDFS1234567890

tracker_server = 192.168.12.11:22122
tracker_server = 192.168.12.22:22122
  • 测试upload()方法,我们上传resources/upload/目录下的mayday_life.png。
    public static String BASE_UPLOAD_PATH = TestFastDFS.class.getProtectionDomain().getCodeSource()
            .getLocation().getPath();

    public static String MAYDAY_IMG = BASE_UPLOAD_PATH + "upload/mayday_life.jpg";

        NameValuePair[] metaDataList = new NameValuePair[] {
                new NameValuePair("bound_name", "mayday"),
                new NameValuePair("masterpiece", "juejiang")
        };

        //tests upload mayday_life.jpg
        FastDFSUtil.getInstance().upload(MAYDAY_IMG, "jpg", metaDataList);

image.png

image.png

  • 测试download()方法
        public static String BASE_DOWNLOAD_PATH =
            "D:/myeclipse_workspace/fastdfs_demo/src/main/resources/download";

        //download group1/M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg
        FastDFSUtil.getInstance().download("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg"
                , BASE_DOWNLOAD_PATH);
  • 下载成功!我们可以看到download文件夹下多了wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg。

image.png

image.png

  • 测试getFileInfo()方法
        //gets "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file info
        FastDFSUtil.getInstance().getFileInfo("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");
  • 获取文件信息成功!

image.png

  • 测试getFileMetaData()方法
        //gets "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file metadata
        NameValuePair[] metaLists = FastDFSUtil.getInstance().getFileMataData("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");

        for (NameValuePair nvp : metaDataList) {
            System.out.println(nvp.getName() + ":" + nvp.getValue());
        }
  • 获取文件元信息成功!

image.png

  • 测试detele()方法
        //detele "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file
        FastDFSUtil.getInstance().delete("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");

image.png

  • 删除成功!我们进入192.168.12.33和192.168.12.44的cd /fastdfs/storage/data/M00/00/00/目录下面已经找不到wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg了。

image.png

image.png

  • 最后附上完整测试用例。
public class TestFastDFS {

    public static String BASE_UPLOAD_PATH = TestFastDFS.class.getProtectionDomain().getCodeSource()
            .getLocation().getPath();

    public static String MAYDAY_IMG = BASE_UPLOAD_PATH + "upload/mayday_life.jpg";

    public static String BASE_DOWNLOAD_PATH =
            "D:/myeclipse_workspace/fastdfs_demo/src/main/resources/download";

    public static void main(String[] args) {
        NameValuePair[] metaDataList = new NameValuePair[] {
                new NameValuePair("bound_name", "mayday"),
                new NameValuePair("masterpiece", "juejiang")
        };

        //tests upload mayday_life.jpg
        //FastDFSUtil.getInstance().upload(MAYDAY_IMG, "jpg", metaDataList);

        //upload success
        //group:group1
        //path:M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg

        //download group1/M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg
        //FastDFSUtil.getInstance().download("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg"
                //, BASE_DOWNLOAD_PATH);

        //gets "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file info
        //FastDFSUtil.getInstance().getFileInfo("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");

        //gets "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file metadata
        //NameValuePair[] metaLists = FastDFSUtil.getInstance().getFileMataData("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");

        //for (NameValuePair nvp : metaDataList) {
            //System.out.println(nvp.getName() + ":" + nvp.getValue());
        //}

        //detele "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file
        FastDFSUtil.getInstance().delete("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");

    }
}

Summary

写完了,就要陪女朋友吃螺蛳粉,逛万达广场,看电影咯。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏battcn

Spring解密 - 默认标签的解析

紧跟上篇 Spring解密 - XML解析 与 Bean注册 ,我们接着往下分析源码

11610
来自专栏Java与Android技术栈

基于Kotlin的委托机制实现一个对Extra、SharedPreferences操作的库

本文介绍的库,github地址:https://github.com/fengzhizi715/SAF-Object-Delegate

16530
来自专栏Android干货园

Retrofit+OKHttp 教你怎么持久化管理Cookie

版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/lyhhj/article/details/51...

14630
来自专栏lzj_learn_note

阿里ARouter使用及源码解析(一)

在app的开发中,页面之间的相互跳转是最基本常用的功能。在Android中的跳转一般通过显式intent和隐式intent两种方式实现的,而Android的原生...

20520
来自专栏不会写文章的程序员不是好厨师

Spring源码初探-IOC(5)-ApplicationContext功能扩展及其扩展点

前面几篇关于Spring的文章简单阐述了使用BeanFactory作为容器时bean的初始化过程。然而在实际使用中,我们并不会直接接触和编码BeanFactor...

13020
来自专栏Java成神之路

Java微信公众平台开发_07_JSSDK图片上传

(2)对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,使用URL键值对的格式 (即 key1=value1&key2=value2…)拼接成...

43920
来自专栏Android开发指南

6.后台任务封装

29470
来自专栏青青天空树

安卓OKhttp请求封装

  目前安卓开发中使用的网络工具为OKhttp,但是okhttp的使用还不是很方便,在okhttp的基础上再对请求进行封装会极大的方便网络调用。

25230
来自专栏ppjun专栏

android面试题

比如设置android:layout_gravity="right"的button会显示在父view的最右边。所以layout_gravity是设置当前view...

18220
来自专栏java技术学习之道

Spring AOP原理分析一次看懂

18240

扫码关注云+社区

领取腾讯云代金券