【FastDFS】maven项目使用FastDFS上传和读取图片

一、前言

      在前一篇博客中,小编简单的向大家介绍了一下FastDFS的安装,把安装中会有的问题也简答说了一下。不过这些都是运维部门要做到,对于开发人员,我们只需要知道FastDFS的服务器地址就可以了,然后利用FastDFS提供的jar工具,来上传和读取操作。这篇博客,小编就向大家介绍一下这个。

二、环境准备

三、具体操作

3.1 fastdfs-client安装到本地仓库

      为了方便应用程序的访问FastDFS,官网提供了fastdfs-client-java,以便更好的与应用程序结合使用。

      下载fastdfs-client-java源码添加到项目工程里面

这里写图片描述

      这个jar包在中央仓库是没有的,我们可以将源码下载下来,使用maven install安装到本地仓库。

这里写图片描述

3.2 添加标签

      在我们需要的项目的pom.xml文件中,添加标签。

     <dependency> 
     <groupId>org.csource</groupId>
                 <artifactId>fastdfs-client-java</artifactId>
                 <version>1.27-SNAPSHOT</version>
    </dependency>

      添加的标签要借鉴导入的fastdfs-client-java项目中的pom文件中的内容:

这里写图片描述

      自己项目整理后的效果:

这里写图片描述

      保存后,就会在项目中,添加引用:

这里写图片描述

四、FastDFS服务器的使用

使用方法:
1、 把FastDFS提供的jar包添加到工程中
2、 初始化全局配置。加载一个配置文件。
3、 创建一个TrackerClient对象。
4、 创建一个TrackerServer对象。
5、 声明一个StorageServer对象,null。
6、 获得StorageClient对象。
7、直接调用StorageClient对象方法上传文件即可。

      在使用之前,我们需要在配置文件中表明tracker的地址是什么,所以我们建立一个名为client.conf的配置文件:

tracker_server=192.168.137.11:22122

这里写图片描述

      测试用例:

    @Test
    public void testUpload() throws Exception {
        // 1、把FastDFS提供的jar包添加到工程中
        // 2、初始化全局配置。加载一个配置文件。
        ClientGlobal.init("D:\\workspaces-itcast\\JaveEE18\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\client.conf");
        // 3、创建一个TrackerClient对象。
        TrackerClient trackerClient = new TrackerClient();
        // 4、创建一个TrackerServer对象。
        TrackerServer trackerServer = trackerClient.getConnection();
        // 5、声明一个StorageServer对象,null。
        StorageServer storageServer = null;
        // 6、获得StorageClient对象。
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
        // 7、直接调用StorageClient对象方法上传文件即可。
        String[] strings = storageClient.upload_file("D:\\Documents\\Pictures\\images\\2f2eb938943d.jpg", "jpg", null);
        for (String string : strings) {
            System.out.println(string);
        }
    }

      在正式的项目中我们会封装一个专门操作FastDFS的使用类:

package com.taotao.fastdfs;

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:")) {
                String url = this.getClass().getResource("/").getPath();
                url = url.substring(1);
                conf = conf.replace("classpath:", url);
            }
            ClientGlobal.init(conf);
            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageServer = null;
            storageClient = new StorageClient1(trackerServer, storageServer);
        }

        public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
            return storageClient.upload_file1(fileName, extName, metas);
        }
        public String uploadFile(String fileName, String extName) throws Exception {
            return storageClient.upload_file1(fileName, extName, null);
        }

        public String uploadFile(String fileName) throws Exception {
            return storageClient.upload_file1(fileName, null, null);
        }
        public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
            return storageClient.upload_file1(fileContent, extName, metas);
        }
        public String uploadFile(byte[] fileContent, String extName) throws Exception {
            return storageClient.upload_file1(fileContent, extName, null);
        }
        public String uploadFile(byte[] fileContent) throws Exception {
            return storageClient.upload_file1(fileContent, null, null);
        }
}

四、小结

      通过对FastDFS的了解,如何导入相应的jar包,这个操作也是小编第一次使用,在maven项目中也有很好的操作。加油!

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 27
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你个佬六

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值