【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
  • 1

这里写图片描述

      测试用例:

    @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项目中也有很好的操作。加油!

猜你喜欢

转载自blog.csdn.net/weixin_41761540/article/details/80105185