FastdDFS入门二 简单使用

java代码操作

2.1、启动服务器

先启动Tracker服务器与Storage服务器,然后启动linux服务器。

/usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf
/usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf
/usr/local/nginx/sbin/nginx

2.2、初始化编程环境

要用java操作 FastDSF 需要引入作者提供的源码,因为作者提供的源码并未编译发布到maven的中央仓库,因此需要我们自己编译,并安装到本地的mavn仓库。

1、下载源码

https://github.com/happyfish100/fastdfs-client-java

2、编译安装

对下载的源码进行解压,并进入到解压目录,执行mvn clean install命令,此时就会生成一个jar包,并安装到本地的maven仓库。

3、项目中引入jar包

在执行完第二步之后,就可以在项目中通过maven坐标引入jar包了。

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

2.3、测试代码

初始化完成编程环境之后,我们就可以编写测试代码了

1、配置一个.properties代码。

fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key = FastDFS1234567890
fastdfs.http_tracker_http_port = 80
​
fastdfs.tracker_servers = 192.168.2.136:22122
​
fastdfs.connection_pool.enabled = true
fastdfs.connection_pool.max_count_per_entry = 500
fastdfs.connection_pool.max_idle_time = 3600
fastdfs.connection_pool.max_wait_time_in_ms = 1000

除了fastdfs.tracker_servers之外,其他的配置都不是必须的。参考文档:https://github.com/happyfish100/fastdfs-client-java

2、编写测试代码

package com.jwt.test;
​
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;
​
import java.io.IOException;
​
public class FastDFS {
​
    public static void main(String[] args) throws Exception {
        String uploadFilePath="D:/电子书籍/makedown_picture/bean的生命周期1.JPG";
​
        String filePath = new ClassPathResource("application.properties").getFile().getAbsolutePath();
        System.out.println(filePath);
        // 1、加载配置文件,配置文件中的内容就是 tracker 服务的地址。
        ClientGlobal.initByProperties(filePath);
        // 2、创建一个 TrackerClient 对象。直接 new 一个。
        TrackerClient trackerClient = new TrackerClient();
        // 3、使用 TrackerClient 对象创建连接,获得一个 TrackerServer 对象。
        TrackerServer trackerServer = trackerClient.getTrackerServer();
        // 4、创建一个 StorageServer 的引用,值为 null
        StorageServer storageServer = null;
        // 5、创建一个 StorageClient 对象,需要两个参数 TrackerServer 对象、StorageServer 的引用
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
        // 6、使用 StorageClient 对象上传图片。
        //扩展名不带“.”
        String[] strings = storageClient.upload_file(uploadFilePath, "jpg",
                null);
        // 7、返回数组。包含组名和图片的路径。
        for (String string : strings) {
            System.out.println(string);
        }
        System.out.println("上传完成");
    }
}
​
​

注意:这里采取的是.properties文件,并非.conf文件,所以初始化时的代码是ClientGlobal.initByProperties(filePath);

2.4、工具类

可以封装工具类,来操作FastDFS

package com.maxrocky.tools;
 
        import java.io.File;
        import java.util.HashMap;
        import java.util.Map;
 
        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 int delete_file(String group_name, String remote_filename) throws IOException, MyException ;
public byte[] download_file(String group_name, String remote_filename) throws IOException, MyException
posted @ 2021-11-28 10:25  阿瞒123  阅读(95)  评论(0编辑  收藏  举报