码迷,mamicode.com
首页 > 其他好文 > 详细

fastDFS 入门

时间:2016-05-13 01:52:21      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

fdfs_client.conf

connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8090
http.anti_steal_token = yes
http.secret_key = FastDFS1234567890

tracker_server = xx.xx.xx.xx:22122
tracker_server = xx.xx.xx.xx:22122

FastDFSUtil

package com.mongo.tools;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.IOUtils;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

public class FastDFSUtil {

    private static String configFile_path = (FastDFSUtil.class.getClassLoader()
            .getResource("").getPath() + "fdfs_client.conf").replace("/", "//");
    private static TrackerClient trackerClient = null;
    private static TrackerServer trackerServer = null;
    private static StorageClient storageClient = null;
    private static StorageServer storageServer = null;

    static {
        try {
            ClientGlobal.init(configFile_path);
            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageServer = null;
            storageClient = new StorageClient(trackerServer, storageServer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }

    /**
     * keep the file info
     * 
     * @author whf
     * 
     */
    public static class MyFileInfo {
        private String groupName = null;
        private String filePath = null;

        public MyFileInfo(String groupName, String filePath) {
            super();
            this.groupName = groupName;
            this.filePath = filePath;
        }

        public String getGroupName() {
            return groupName;
        }

        public void setGroupName(String groupName) {
            this.groupName = groupName;
        }

        public String getFilePath() {
            return filePath;
        }

        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }
    }

    /**
     * upload file to fastDFS
     * 
     * @whf
     * @param localFilePath
     * @param ext
     * @return
     */
    public static List<MyFileInfo> uploadFile(List<File> files) {
        List<MyFileInfo> res = new ArrayList<MyFileInfo>();
        for (File file : files) {
            byte buffer[] = CommonUtil.getBytes(file);
            String fileName = file.getName();
            Integer start = fileName.lastIndexOf(".");
            String fileExtName = start != -1 ? fileName.substring(start + 1)
                    : "";
            Long fileLength = file.length();
            NameValuePair[] metaList = new NameValuePair[] {
                    new NameValuePair("fileName", fileName),
                    new NameValuePair("fileExtName", fileExtName),
                    new NameValuePair("fileLength", String.valueOf(fileLength)) };
            try {
                String[] fileIds = storageClient.upload_file(buffer,
                        fileExtName, metaList);
                res.add(new MyFileInfo(fileIds[0], fileIds[1]));
            } catch (IOException e) {
                e.printStackTrace();
            } catch (MyException e) {
                e.printStackTrace();
            }
        }
        return res;
    }

    /**
     * download file from fastdfs
     * 
     * @author whf
     * @param localFilePath
     * @param fileName
     * @param ext
     * @param groupName
     * @param filePath
     * @return
     */
    public static Boolean downLoadFile(String localFilePath, String fileName,
            String ext, String groupName, String filePath) {
        try {
            byte[] b = storageClient.download_file(groupName, filePath);
            FileOutputStream fo = new FileOutputStream(new File(localFilePath
                    + "\\"
                    + (fileName == null ? UUID.randomUUID().toString()
                            : fileName) + (ext == null ? "" : "." + ext)));
            IOUtils.write(b, fo);
            fo.flush();
            fo.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } catch (MyException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * delete file from fastdfs
     * 
     * @author whf
     * @param groupName
     * @param filePath
     * @return
     */
    public static Boolean deleteFile(String groupName, String filePath) {
        Integer flag = null;
        try {
            flag = storageClient.delete_file(groupName, filePath);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return flag == null || flag == 0 ? true : false;
    }

    /**
     * get file info from fastdfs
     * 
     * @author whf
     * @param groupName
     * @param filePath
     * @return
     */
    public static Map<String, Object> getFileInfo(String groupName,
            String filePath) {
        Map<String, Object> res = new HashMap<String, Object>();
        FileInfo fileInfo = null;
        try {
            fileInfo = storageClient.get_file_info(groupName, filePath);
            res.put("ipAddr", fileInfo.getSourceIpAddr());
            res.put("fileSize", fileInfo.getFileSize());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * get file meta from fastdfs
     * 
     * @author whf
     * @param groupName
     * @param filePath
     * @return
     */
    public static Map<String, Object> getFileMeta(String groupName,
            String filePath) {
        Map<String, Object> res = new HashMap<String, Object>();
        try {
            NameValuePair nvps[] = storageClient.get_metadata(groupName,
                    filePath);
            for (NameValuePair nvp : nvps) {
                res.put(nvp.getName(), nvp.getValue());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return res;
    }

}

fastDFS 入门

标签:

原文地址:http://blog.csdn.net/qq_17612199/article/details/51344016

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!