我是靠谱客的博主 飘逸飞鸟,最近开发中收集的这篇文章主要介绍Java实现FastDFS文件上传、查询、下载和删除,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1、配置内容:

## fastdfs-client.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 = 8080

fastdfs.tracker_servers = 192.168.18.130:22122

2、Java内容:

package com.jd;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.junit.jupiter.api.Test;

class Tests {

    //上传文件
    @Test
    public void upload() {
        try {
            //加载FastDFS客户端配置文件
            ClientGlobal.initByProperties("fastdfs-client.properties");
            //创建tracker客户端
            TrackerClient trackerClient = new TrackerClient();
            //建立与Tracker服务器的连接
            TrackerServer trackerServer = trackerClient.getConnection();
            //建立与Storage服务器的连接
            StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
            //获取storage客户端
            StorageClient storageClient = new StorageClient(trackerServer, storageServer);

            //定义文件元数据
            NameValuePair[] metaList = new NameValuePair[2];
            metaList[0] = new NameValuePair("id", "bbc3dc68-4f25-450c-8022-63d2c0dfd5e0");
            metaList[1] = new NameValuePair("描述", "这是一张适用于Windows操作系统的桌面背景图片。");
            String path = "C:/Windows/Web/Screen/img100.jpg";
            String [] fileInfoes = storageClient.upload_file(path, "jpg", metaList);
            System.out.println("groupName:"+fileInfoes[0]);//group1
            System.out.println("remoteFileName:"+fileInfoes[1]);//M00/00/00/wKgSjGBpOU-AKLKOAAsZd_e-IiM892.jpg
            storageServer.close();
            trackerServer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询文件
    @Test
    public void query() {
        try {
            //加载FastDFS客户端配置文件
            ClientGlobal.initByProperties("fastdfs-client.properties");
            //创建tracker客户端
            TrackerClient trackerClient = new TrackerClient();
            //建立与Tracker服务器的连接
            TrackerServer trackerServer = trackerClient.getConnection();
            //建立与Storage服务器的连接
            StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
            //获取storage客户端
            StorageClient storageClient = new StorageClient(trackerServer, storageServer);

            NameValuePair[] nvps = storageClient.get_metadata("group1", "M00/00/00/wKgSjGBpOU-AKLKOAAsZd_e-IiM892.jpg");
            for (NameValuePair nvp : nvps) {
                System.out.println(nvp.getName() + ":" + nvp.getValue());
            }
            FileInfo fileInfo = storageClient.query_file_info("group1", "M00/00/00/wKgSjGBpOU-AKLKOAAsZd_e-IiM892.jpg");
            System.out.println(fileInfo);
            storageServer.close();
            trackerServer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //下载文件
    @Test
    public void download() {
        try {
            //加载FastDFS客户端配置文件
            ClientGlobal.initByProperties("fastdfs-client.properties");
            //创建tracker客户端
            TrackerClient trackerClient = new TrackerClient();
            //建立与Tracker服务器的连接
            TrackerServer trackerServer = trackerClient.getConnection();
            //建立与Storage服务器的连接
            StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
            //获取storage客户端
            StorageClient storageClient = new StorageClient(trackerServer, storageServer);

            int result = storageClient.download_file("group1","M00/00/00/wKgSjGBpOU-AKLKOAAsZd_e-IiM892.jpg","D:/1.png");
            if (result == 0) {
                System.out.println("下载成功");
            } else {
                System.out.println("下载失败");
            }
            storageServer.close();
            trackerServer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除文件
    @Test
    public void delete() {
        try {
            //加载FastDFS客户端配置文件
            ClientGlobal.initByProperties("fastdfs-client.properties");
            //创建tracker客户端
            TrackerClient trackerClient = new TrackerClient();
            //建立与Tracker服务器的连接
            TrackerServer trackerServer = trackerClient.getConnection();
            //建立与Storage服务器的连接
            StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
            //获取storage客户端
            StorageClient storageClient = new StorageClient(trackerServer, storageServer);

            Integer result = storageClient.delete_file("group1", "M00/00/00/wKgSjGBpOU-AKLKOAAsZd_e-IiM892.jpg");
            if (result == 0) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
            storageServer.close();
            trackerServer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

最后

以上就是飘逸飞鸟为你收集整理的Java实现FastDFS文件上传、查询、下载和删除的全部内容,希望文章能够帮你解决Java实现FastDFS文件上传、查询、下载和删除所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部