欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

fastDFS连接池和客户端实现

程序员文章站 2022-10-03 13:40:25
fastdfs连接池实现:import org.csource.common.MyException;import org.csource.fastdfs.*;import java.io.IOException;import java.util.Properties;import java.util.concurrent.LinkedBlockingQueue;import java.util.concurrent.TimeUnit;/** * @Description: ....
fastdfs连接池实现: 
import org.csource.common.MyException;
import org.csource.fastdfs.*;

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;


/**
 * @Description:
 * @author:lgr
 * @CreateDate:2020.08.09 14:15
 */
public class FastdfsConnetionPool {


    /**
     * 网络请求超时时长
     */
    private static String networkTimeOut="1500";

    /**
     * 连接超时时长
     */
    private static String connectTimeout="600";

    /**
     * fastdfs服务地址
     */
    private static String fastDfsIp="127.0.0.1";

    /**
     * tracker端口号
     */
    private static String fastDfsPort="22122";


    /**
     * 默认连接池大小
     */
    public static String  connection_size = "10";

    /**
     * storageClient队列
     */
    private static LinkedBlockingQueue<StorageClient> storageClientQueue = new LinkedBlockingQueue<>(
            Integer.parseInt(connection_size));

    /**
     * 当前索引
     */
    private static int current_index;


    private static TrackerClient trackerClient;



    private static Properties properties=new Properties();

    /**
     * 初始化
     */
    static {
        //LoggerUtil.info("begin init FastDfs");
        properties.put("fastdfs.network_timeout_in_seconds",networkTimeOut);
        properties.put("fastdfs.connect_timeout_in_seconds",connectTimeout);
        properties.put("fastdfs.tracker_servers",fastDfsIp+":"+fastDfsPort);
        try {
            ClientGlobal.initByProperties(properties);
            trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
            //LoggerUtil.info("end initFastDfs");
        } catch (IOException e) {
            //LoggerUtil.info("init fastDFSConnectPool catch exception:{}",e.getMessage());
        } catch (MyException e) {
            //LoggerUtil.info("init fastDFSConnectPool catch exception:{}",e.getMessage());
        }
    }

    /**
     * 创建StorageClient
     */
    public static void createStorageClient() {
        synchronized (trackerClient) {
            if (current_index < Integer.parseInt(connection_size)) {
                try {
                    TrackerServer trackerServer = trackerClient.getConnection();
                    StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
                    StorageClient storageClient = new StorageClient(trackerServer, storageServer);
                    if (storageClientQueue.offer(storageClient)) {
                        current_index++;
                    }
                } catch (IOException e) {
                    //LoggerUtil.info("createStorageClient catch exception:{}",e.getMessage());
                }
            }
        }
    }

    /**
     * 获取StorageClient
     * @return
     */
    public static StorageClient findStorageClient() {
        // 尝试获取一个有用的客户端连接信息
        StorageClient clientInfo = storageClientQueue.poll();
        if (clientInfo == null) {
            if (current_index < Integer.parseInt(connection_size)) {
                createStorageClient();
            }
            try {
                clientInfo= storageClientQueue.poll(10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                //LoggerUtil.info("findStorageClient catch exception:{}",e.getMessage());
            }
        }
        return clientInfo;
    }

    /**
     * 回收资源
     * @param storageClient
     */
    public static void recycleStorageClient(StorageClient storageClient){
        try {
            if(storageClient != null){
                storageClientQueue.offer(storageClient);
            }
        } catch (Exception e) {
            //LoggerUtil.info("recycleStorageClient catch exception:{}",e.getMessage());
        }
    }






} 

fastdfs客户端实现:

 import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @author:lgr
 * @CreateDate:2020.06.28 19:16
 */


public class FastDfsClient {

    /**
     * 上传文件
     * @param content
     * @param fileExtName
     * @param valuePairs
     * @return
     */
    public static String[] upload(byte[] content, String fileExtName, NameValuePair[] valuePairs) {
        StorageClient storageClient = FastdfsConnetionPool.findStorageClient();
        String[] uploadResults = null;
        try {
            uploadResults = storageClient.upload_file(content, fileExtName, valuePairs);
        } catch (Exception e) {
            //LoggerUtil.info("upload catch exception:{}", e.getMessage());
        } finally {
            FastdfsConnetionPool.recycleStorageClient(storageClient);
        }
        return uploadResults;
    }

    /**
     * 删除文件
     * @param fileUrl
     */
    public static void deleteFile(String groupName,String fileUrl){
        try {
            StorageClient storageClient = FastdfsConnetionPool.findStorageClient();
            final int group = storageClient.delete_file(groupName, fileUrl);
        } catch (IOException e) {
            //LoggerUtil.info("deleteFile catch exception:{}", e.getMessage());
        } catch (Exception e) {
            //LoggerUtil.info("deleteFile catch exception:{}", e.getMessage());
        }
    }

    /**
     * 文件下载
     * @param fileUrl
     * @return
     */
    public static byte[] download(String groupName,String fileUrl){
        byte[] group1s = null;
        try {
            StorageClient storageClient = FastdfsConnetionPool.findStorageClient();
            group1s = storageClient.download_file(groupName, fileUrl);
        } catch (IOException e) {
            //LoggerUtil.info("download catch exception:{}", e.getMessage());
        } catch (Exception e) {
            //LoggerUtil.info("download catch exception:{}", e.getMessage());
        }
        return group1s;
    }

    /**
     * 获取文件元数据
     * @param fileId 文件ID
     * @return
     */
    public static Map<String,String> getFileMetadata(String groupName, String fileId) {
        try {
            StorageClient storageClient = FastdfsConnetionPool.findStorageClient();
            NameValuePair[] metaList = storageClient.get_metadata(groupName,fileId);
            if (metaList != null) {
                HashMap<String,String> map = new HashMap<String, String>();
                for (NameValuePair metaItem : metaList) {
                    map.put(metaItem.getName(),metaItem.getValue());
                }
                return map;
            }
        } catch (Exception e) {
            //LoggerUtil.info("getFileMetadata catch exception:{}", e.getMessage());
        }
        return null;
    }
} 

本文地址:https://blog.csdn.net/qq_39413364/article/details/107895878

相关标签: java fastdfs