fastdfs工具类

/**
 * Bestpay.com.cn Inc.
 * Copyright (c) 2011-2016 All Rights Reserved.
 */
package com.bestpay.messagecenter.product.common.constant;

import java.util.List;

/**
 * fastdfs 常量类
 * @author lxn
 * @version Id: FastDfsConstant.java, v 0.1 2016/11/24 19:20 lxn Exp $$
 */
public class FastDfsConstant {

    private int trackerHttpPort;
    private List trackerServers;
    private int connectTimeout ;
    private int networkTimeout;
    private String charset;
    private boolean antiStealToken;
    private String secretKey;


    /**
     * 获取 trackerHttpPort
     *
     * @return trackerHttpPort
     */
    public int getTrackerHttpPort() {
        return trackerHttpPort;
    }

    /**
     * 设置trackerHttpPort
     *
     * @return trackerHttpPort
     */
    public void setTrackerHttpPort(int trackerHttpPort) {
        this.trackerHttpPort = trackerHttpPort;
    }

    /**
     * 获取 trackerServers
     *
     * @return trackerServers
     */
    public List getTrackerServers() {
        return trackerServers;
    }

    /**
     * 设置trackerServers
     *
     * @return trackerServers
     */
    public void setTrackerServers(List trackerServers) {
        this.trackerServers = trackerServers;
    }

    /**
     * 获取 connectTimeout
     *
     * @return connectTimeout
     */
    public int getConnectTimeout() {
        return connectTimeout;
    }

    /**
     * 设置connectTimeout
     *
     * @return connectTimeout
     */
    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    /**
     * 获取 networkTimeout
     *
     * @return networkTimeout
     */
    public int getNetworkTimeout() {
        return networkTimeout;
    }

    /**
     * 设置networkTimeout
     *
     * @return networkTimeout
     */
    public void setNetworkTimeout(int networkTimeout) {
        this.networkTimeout = networkTimeout;
    }

    /**
     * 获取 charset
     *
     * @return charset
     */
    public String getCharset() {
        return charset;
    }

    /**
     * 设置charset
     *
     * @return charset
     */
    public void setCharset(String charset) {
        this.charset = charset;
    }

    /**
     * 获取 antiStealToken
     *
     * @return antiStealToken
     */
    public boolean isAntiStealToken() {
        return antiStealToken;
    }

    /**
     * 设置antiStealToken
     *
     * @return antiStealToken
     */
    public void setAntiStealToken(boolean antiStealToken) {
        this.antiStealToken = antiStealToken;
    }

    /**
     * 获取 secretKey
     *
     * @return secretKey
     */
    public String getSecretKey() {
        return secretKey;
    }

    /**
     * 设置secretKey
     *
     * @return secretKey
     */
    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    @Override
    public String toString() {
        return "FastDfsConstant{" +
                "trackerHttpPort=" + trackerHttpPort +
                ", trackerServers=" + trackerServers +
                ", connectTimeout=" + connectTimeout +
                ", networkTimeout=" + networkTimeout +
                ", charset='" + charset + '\'' +
                ", antiStealToken=" + antiStealToken +
                ", secretKey='" + secretKey + '\'' +
                '}';
    }
}
/**
 * Bestpay.com.cn Inc.
 * Copyright (c) 2011-2016 All Rights Reserved.
 */
package com.bestpay.messagecenter.product.manager;

import java.net.InetSocketAddress;

import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bestpay.messagecenter.product.common.constant.FastDfsConstant;
import com.bestpay.messagecenter.product.common.model.FastRemoteAddress;

/**
 * fastdfs操作manager
 * @author lxn
 * @version Id: FastDfsManager.java, v 0.1 2016/11/24 19:25 lxn Exp $$
 */
@Component
public class FastDfsManager {

    /**
     * fastdfs 常量类
     */
    @Autowired
    private FastDfsConstant fastDfsConstant;

    /**
     * 获取fastdfs client
     * @return
     * @throws Exception
     */
    public StorageClient getClient()throws Exception {
        ClientGlobal.setG_anti_steal_token(fastDfsConstant.isAntiStealToken());
        ClientGlobal.setG_charset(fastDfsConstant.getCharset());
        ClientGlobal.setG_tracker_http_port(fastDfsConstant.getTrackerHttpPort());
        ClientGlobal.setG_network_timeout(fastDfsConstant.getNetworkTimeout());
        ClientGlobal.setG_connect_timeout(fastDfsConstant.getConnectTimeout());
        ClientGlobal.setG_secret_key(fastDfsConstant.getSecretKey());

        InetSocketAddress[] tracker_servers = new InetSocketAddress[fastDfsConstant.getTrackerServers().size()];

        for(int i = 0; i < fastDfsConstant.getTrackerServers().size(); ++i) {
            String[] parts = fastDfsConstant.getTrackerServers().get(i).split("\\:", 2);
            if(parts.length != 2) {
                throw new MyException("the value of item \"tracker_server\" is invalid, the correct format is host:port");
            }

            tracker_servers[i] = new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
        }

        TrackerGroup g_tracker_group = new TrackerGroup(tracker_servers);
        ClientGlobal.setG_tracker_group(g_tracker_group);

        TrackerClient tracker = new TrackerClient();
        TrackerServer trackerServer = tracker.getConnection();
        StorageServer storageServer = null;
        StorageClient client = new StorageClient(trackerServer, storageServer);
        return  client;
    }

    /**
     * 上传文件到fastdfs
     * @param input
     * @param ext
     * @return
     * @throws Exception
     */
    public FastRemoteAddress upload(byte[] input,String ext) throws Exception {
        StorageClient client = getClient();
        String[] down =  client.upload_file(input,ext,null);
        if(down==null){
            return null;
        }
        return new FastRemoteAddress(down[0],down[1]);
    }

    /**
     * 从fastdfs下载文件
     * @param fastRemoteAddress
     * @return
     * @throws Exception
     */
    public byte[] download(FastRemoteAddress fastRemoteAddress)throws Exception {
        StorageClient client = getClient();
        return client.download_file(fastRemoteAddress.getGroup(),fastRemoteAddress.getRemoateFileName());
    }

    /**
     *  从fastdfs删除文件
     * @param fastRemoteAddress
     * @return
     * @throws Exception
     */
    public int delete(FastRemoteAddress fastRemoteAddress) throws Exception {
        StorageClient client = getClient();
        return client.delete_file(fastRemoteAddress.getGroup(), fastRemoteAddress.getRemoateFileName());
    }

}


你可能感兴趣的:(工具类)