FastDFS工具类

当你在使用fastDFS时,将所有的东西配置好之后,使用这个工具类能让你事半功倍

package com.xxx.file.utils;

import com.xxx.file.pojo.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 文件上传的工具类
 */
public class FastDFSClient {

    private static Logger logger = LoggerFactory.getLogger(FastDFSClient.class);

    private static TrackerClient trackerClient = null;

    private static StorageClient1 storageClient1 = null;

    private static TrackerServer trackerServer = null;

    private static StorageServer storageServer = null;

    //静态代码块   为了上传文件  初始化加载文件的上传配置文件  (文件中包含  上传文件的地址 连接时间 读取时间 编码集 等
    static {
        try {
            String absolutePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();
            ClientGlobal.init(absolutePath);

            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageClient1 = new StorageClient1(trackerServer, storageServer);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("ClientGlobal is fail:{}",e.getMessage());
        }

    }

    /**
     * 上传文件
     *
     * @param file
     * @return 文件的路径
     */
    public static String uploadFile(FastDFSFile file){
        System.out.println("上传文件");
        NameValuePair[] nameValuePairs = new NameValuePair[1];
        nameValuePairs[0] = new NameValuePair(file.getAuthor());
        try {
            return storageClient1.upload_file1(file.getContent(),file.getExt(),nameValuePairs);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("upload file is fail: {} ",e.getMessage());
        }
        return null;

    }

    /**
     * 下载文件
     * @param path
     * @return  文件对象
     */
    public static InputStream downFile(String path){

        try {
            byte[] bytes = storageClient1.download_file1(path);
            return new ByteArrayInputStream(bytes);
        } catch (Exception e){
            e.getStackTrace();
            logger.error("download file is fail : {}" ,e.getMessage());
        }


        return null;
    }


    /**
     *  删除文件
     * @param path
     *
     */
    public static void deleteFile(String path){

        try {
            storageClient1.delete_file1(path);
        } catch (Exception e)  {
            e.printStackTrace();
            logger.error("delete file is fail : {}",e.getMessage());
        }
    }
    /***
     * 获取Tracker服务地址
     * @return
     * @throws IOException
     */
    public static String getTrackerUrl() throws IOException {
        return "http://"+trackerServer.getInetSocketAddress().getHostString()+":"+ClientGlobal.getG_tracker_http_port()+"/";
    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值