fastDFS 入门

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;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值