关闭

fastdfs文件服务器-客户端实现

462人阅读 评论(0) 收藏 举报
分类:

需要的依赖:

<!-- fastdfs jar依赖 -->
		<dependency>
			<groupId>org.csource</groupId>
			<artifactId>fastdfs</artifactId>
			<version>1.0.0</version>
		</dependency>

dfs的配置文件:

connect_timeout = 2000
network_timeout = 30000
charset = ISO8859-1
http.tracker_http_port = 8080
http.anti_steal_token = false
http.secret_key = FastDFS1234567890


tracker_server.length = 1
tracker_server1 = 182.92.105.142:22122


客户端client java:

package com.citic.gatz.service.impl;

import java.io.InputStream;
import java.net.InetSocketAddress;
import java.util.Properties;

import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerGroup;
import org.csource.fastdfs.TrackerServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class FdfsUpload {
    private final static Logger logger = LoggerFactory.getLogger(FdfsUpload.class);

    public FdfsUpload() {
        try {
        	//URL fileURL= this.getClass().getResource("/fdfs_client.conf");     
            //ClientGlobal.init(fileURL.getFile());
        	//返回读取指定资源的输入流    
            InputStream is=this.getClass().getResourceAsStream("/fdfs_client.conf");
            Properties p = new Properties();
            p.load(is);
            ClientGlobal.setG_connect_timeout(Integer.valueOf(p.getProperty("connect_timeout")));
            ClientGlobal.setG_network_timeout(Integer.valueOf(p.getProperty("network_timeout")));
            ClientGlobal.setG_charset(p.getProperty("charset"));
            ClientGlobal.setG_tracker_http_port(Integer.valueOf(p.getProperty("http.tracker_http_port")));
            ClientGlobal.setG_anti_steal_token(Boolean.valueOf(p.getProperty("http.anti_steal_token")));
            ClientGlobal.setG_secret_key(p.getProperty("http.secret_key"));
            
            //tracker_server
            String ip = p.getProperty("tracker_server1").split(":")[0];
            Integer port = Integer.valueOf(p.getProperty("tracker_server1").split(":")[1]);
            InetSocketAddress[] tracker_servers = new InetSocketAddress[Integer.valueOf(p.getProperty("tracker_server.length"))];
            tracker_servers[0] = new InetSocketAddress(ip, port);
			ClientGlobal.setG_tracker_group(new TrackerGroup(tracker_servers));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("fastdfs init failed e={}", e);
        }
        logger.info("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
        logger.info("connect_timeout=" + ClientGlobal.g_connect_timeout + "ms");
        logger.info("charset=" + ClientGlobal.g_charset);
        
    }

    public String upload(byte[] filebuf, String filename) {
        try {

            TrackerClient tc = new TrackerClient();

            TrackerServer ts = tc.getConnection();
            if (ts == null) {
                logger.info("getConnection return null");
                return null;
            }

            StorageServer ss = tc.getStoreStorage(ts);
            if (ss == null) {
                logger.info("getStoreStorage return null");
            }

            StorageClient1 sc1 = new StorageClient1(ts, ss);

            // NameValuePair[] metaList = new NameValuePair[1];
            // metaList[0] = new NameValuePair("fileName",
            // filename);//ChAL8VXSinWAHYvqAAC6gGlp1Zs535.pcm-m中记录文件名
            String fileid = sc1.upload_file1(filebuf, filename.substring(filename.lastIndexOf(".") + 1), null);
            
            // sc1.upload_file(file_buff, file_ext_name, meta_list)

            logger.info("Upload local file " + filename + " ok, fileid=" + fileid);
            return fileid;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }
    
    public byte[] download(String fileId) {
        try {

            TrackerClient tc = new TrackerClient();

            TrackerServer ts = tc.getConnection();
            if (ts == null) {
                logger.info("getConnection return null");
                return null;
            }

            StorageServer ss = tc.getStoreStorage(ts);
            if (ss == null) {
                logger.info("getStoreStorage return null");
            }

            StorageClient1 sc1 = new StorageClient1(ts, ss);

            // NameValuePair[] metaList = new NameValuePair[1];
            // metaList[0] = new NameValuePair("fileName",
            // filename);//ChAL8VXSinWAHYvqAAC6gGlp1Zs535.pcm-m中记录文件名
           // String fileid = sc1.upload_file1(filebuf, filename.substring(filename.lastIndexOf(".") + 1), null);
            logger.info("download file ok, fileid =" + fileId);
            return sc1.download_file1(fileId);
            // sc1.upload_file(file_buff, file_ext_name, meta_list)

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
    
    public int delete(String fileId) {
    	try {
            TrackerClient tc = new TrackerClient();
            TrackerServer ts = tc.getConnection();
            if (ts == null) {
                logger.info("getConnection return null");
                return -1;
            }
            StorageServer ss = tc.getStoreStorage(ts);
            if (ss == null) {
                logger.info("getStoreStorage return null");
                return -1;
            }
            StorageClient1 sc1 = new StorageClient1(ts, ss);
            
            int i = sc1.delete_file1(fileId);
            logger.info("delete file ok, fileid =" + fileId);
            return i;

        } catch (Exception ex) {
            return -1;
        }
    }
}

初始化配置文件的方式有两种:

1.直接使用ClientGlobal.init(file);初始化配置文件的方式

2.ClientGloabl.setXX();使用set参数注入的方式

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:217580次
    • 积分:3850
    • 等级:
    • 排名:第8269名
    • 原创:172篇
    • 转载:13篇
    • 译文:0篇
    • 评论:23条
    最新评论