FastDFS:实现文件的上传、下载、删除、修改

 

1、maven项目中,在pom中添加 fastdfs_client 依赖

  1.  
    #添加依赖
  2.  
    <dependency>
  3.  
    <groupId>org.csource</groupId>
  4.  
    <artifactId>fastdfs-client-java</artifactId>
  5.  
    <version>5.0.4</version>
  6.  
    </dependency>
  7.  
     
  8.  
    #说明:
  9.  
    #由于fastdfs-client-java jar包没有上传到中央仓库,所以需要下载源码进行maven编译再上传到私服,然
  10.  
    #后通过私服下载或者通过jar包的形式进行下载

2、在src/main/resource 下创建 fdfs_client.conf 配置文件

  1.  
    #fdfs_client.conf 配置
  2.  
    connect_timeout = 10
  3.  
    network_timeout = 30
  4.  
    charset = UTF-8
  5.  
    http.tracker_http_port = 8888
  6.  
    http.anti_steal_token = no
  7.  
    http.secret_key = FastDFS1234567890
  8.  
     
  9.  
    tracker_server = 100.98.22.253:22122
  10.  
    #如果有多台服务,指定集群的IP
  11.  
    #tracker_server = 192.168.10.250:22122

3、客户端代码实例

  1.  
    package util;
  2.  
     
  3.  
    import java.io.ByteArrayInputStream;
  4.  
    import java.io.File;
  5.  
    import java.io.FileInputStream;
  6.  
    import java.io.IOException;
  7.  
    import java.io.InputStream;
  8.  
     
  9.  
    import org.apache.commons.lang3.StringUtils;
  10.  
    import org.apache.log4j.Logger;
  11.  
    import org.csource.common.NameValuePair;
  12.  
    import org.csource.fastdfs.ClientGlobal;
  13.  
    import org.csource.fastdfs.StorageClient1;
  14.  
    import org.csource.fastdfs.StorageServer;
  15.  
    import org.csource.fastdfs.TrackerClient;
  16.  
    import org.csource.fastdfs.TrackerGroup;
  17.  
    import org.csource.fastdfs.TrackerServer;
  18.  
     
  19.  
    /**
  20.  
    *
  21.  
    * <B>系统名称:</B><BR>
  22.  
    * <B>模块名称:</B><BR>
  23.  
    * <B>中文类名:</B>FastDFS分布式文件系统操作客户端<BR>
  24.  
    * <B>概要说明:</B>FastDFS分布式文件系统操作客户端<BR>
  25.  
    * @author bhz
  26.  
    * @since 2013年11月10日
  27.  
    */
  28.  
    public class FastDFSClientUtils {
  29.  
     
  30.  
    private static final String CONF_FILENAME = Thread.currentThread().getContextClassLoader().getResource("fastdfs_client.conf").getPath();
  31.  
     
  32.  
    private static Logger logger = Logger.getLogger(FastDFSClientUtils.class);
  33.  
     
  34.  
     
  35.  
    private static TrackerClient trackerClient;
  36.  
     
  37.  
     
  38.  
    //加载文件
  39.  
    static {
  40.  
    try {
  41.  
    ClientGlobal.init(CONF_FILENAME);
  42.  
    TrackerGroup trackerGroup = ClientGlobal.g_tracker_group;
  43.  
    trackerClient = new TrackerClient(trackerGroup);
  44.  
    } catch (Exception e) {
  45.  
    logger.error(e);
  46.  
    }
  47.  
    }
  48.  
     
  49.  
    /**
  50.  
    * <B>方法名称:</B>上传方法<BR>
  51.  
    * <B>概要说明:</B><BR>
  52.  
    * @param file 文件
  53.  
    * @param path 路径
  54.  
    * @return 上传成功返回id,失败返回null
  55.  
    */
  56.  
    public static String upload(File file, String path) {
  57.  
    TrackerServer trackerServer = null;
  58.  
    StorageServer storageServer = null;
  59.  
    StorageClient1 storageClient1 = null;
  60.  
    FileInputStream fis = null;
  61.  
    try {
  62.  
    NameValuePair[] meta_list = null; // new NameValuePair[0];
  63.  
    fis = new FileInputStream(file);
  64.  
    byte[] file_buff = null;
  65.  
    if (fis != null) {
  66.  
    int len = fis.available();
  67.  
    file_buff = new byte[len];
  68.  
    fis.read(file_buff);
  69.  
    }
  70.  
     
  71.  
    trackerServer = trackerClient.getConnection();
  72.  
    if (trackerServer == null) {
  73.  
    logger.error("getConnection return null");
  74.  
    }
  75.  
    storageServer = trackerClient.getStoreStorage(trackerServer);
  76.  
    storageClient1 = new StorageClient1(trackerServer, storageServer);
  77.  
    String fileid = storageClient1.upload_file1(file_buff, getFileExt(path), meta_list);
  78.  
     
  79.  
    return fileid;
  80.  
    } catch (Exception ex) {
  81.  
    logger.error(ex);
  82.  
    return null;
  83.  
    }finally{
  84.  
    if (fis != null){
  85.  
    try {
  86.  
    fis.close();
  87.  
    } catch (IOException e) {
  88.  
    logger.error(e);
  89.  
    }
  90.  
    }
  91.  
    if (storageServer != null){
  92.  
    try {
  93.  
    storageServer.close();
  94.  
    } catch (IOException e) {
  95.  
    e.printStackTrace();
  96.  
    }
  97.  
    }
  98.  
    if (trackerServer != null){
  99.  
    try {
  100.  
    trackerServer.close();
  101.  
    } catch (IOException e) {
  102.  
    e.printStackTrace();
  103.  
    }
  104.  
    }
  105.  
    storageClient1 = null;
  106.  
    }
  107.  
    }
  108.  
     
  109.  
    /**
  110.  
    * <B>方法名称:</B>上传方法<BR>
  111.  
    * <B>概要说明:</B><BR>
  112.  
    * @param data 数据
  113.  
    * @param path 路径
  114.  
    * @return 上传成功返回id,失败返回null
  115.  
    */
  116.  
    public static String upload(byte[] data, String extName) {
  117.  
    TrackerServer trackerServer = null;
  118.  
    StorageServer storageServer = null;
  119.  
    StorageClient1 storageClient1 = null;
  120.  
    try {
  121.  
    NameValuePair[] meta_list = null; // new NameValuePair[0];
  122.  
     
  123.  
    trackerServer = trackerClient.getConnection();
  124.  
    if (trackerServer == null) {
  125.  
    logger.error("getConnection return null");
  126.  
    }
  127.  
    storageServer = trackerClient.getStoreStorage(trackerServer);
  128.  
    storageClient1 = new StorageClient1(trackerServer, storageServer);
  129.  
    String fileid = storageClient1.upload_file1(data, extName, meta_list);
  130.  
    return fileid;
  131.  
    } catch (Exception ex) {
  132.  
    logger.error(ex);
  133.  
    return null;
  134.  
    }finally{
  135.  
    if (storageServer != null){
  136.  
    try {
  137.  
    storageServer.close();
  138.  
    } catch (IOException e) {
  139.  
    e.printStackTrace();
  140.  
    }
  141.  
    }
  142.  
    if (trackerServer != null){
  143.  
    try {
  144.  
    trackerServer.close();
  145.  
    } catch (IOException e) {
  146.  
    e.printStackTrace();
  147.  
    }
  148.  
    }
  149.  
    storageClient1 = null;
  150.  
    }
  151.  
    }
  152.  
     
  153.  
    /**
  154.  
    * <B>方法名称:</B>下载方法<BR>
  155.  
    * <B>概要说明:</B>通过文件id进行下载<BR>
  156.  
    * @param fileId 文件id
  157.  
    * @return 返回InputStream
  158.  
    */
  159.  
    public static InputStream download(String groupName, String fileId) {
  160.  
    TrackerServer trackerServer = null;
  161.  
    StorageServer storageServer = null;
  162.  
    StorageClient1 storageClient1 = null;
  163.  
    try {
  164.  
    trackerServer = trackerClient.getConnection();
  165.  
    if (trackerServer == null) {
  166.  
    logger.error("getConnection return null");
  167.  
    }
  168.  
    storageServer = trackerClient.getStoreStorage(trackerServer, groupName);
  169.  
    storageClient1 = new StorageClient1(trackerServer, storageServer);
  170.  
    byte[] bytes = storageClient1.download_file1(fileId);
  171.  
    InputStream inputStream = new ByteArrayInputStream(bytes);
  172.  
    return inputStream;
  173.  
    } catch (Exception ex) {
  174.  
    logger.error(ex);
  175.  
    return null;
  176.  
    } finally {
  177.  
    if (storageServer != null){
  178.  
    try {
  179.  
    storageServer.close();
  180.  
    } catch (IOException e) {
  181.  
    e.printStackTrace();
  182.  
    }
  183.  
    }
  184.  
    if (trackerServer != null){
  185.  
    try {
  186.  
    trackerServer.close();
  187.  
    } catch (IOException e) {
  188.  
    e.printStackTrace();
  189.  
    }
  190.  
    }
  191.  
    storageClient1 = null;
  192.  
    }
  193.  
    }
  194.  
     
  195.  
    /**
  196.  
    * <B>方法名称:</B>删除方法<BR>
  197.  
    * <B>概要说明:</B>根据id来删除一个文件<BR>
  198.  
    * @param fileId 文件id
  199.  
    * @return 删除成功返回0,非0则操作失败,返回错误代码
  200.  
    */
  201.  
    public static int delete(String groupName, String fileId) {
  202.  
    TrackerServer trackerServer = null;
  203.  
    StorageServer storageServer = null;
  204.  
    StorageClient1 storageClient1 = null;
  205.  
    try {
  206.  
    trackerServer = trackerClient.getConnection();
  207.  
    if (trackerServer == null) {
  208.  
    logger.error("getConnection return null");
  209.  
    }
  210.  
    storageServer = trackerClient.getStoreStorage(trackerServer, groupName);
  211.  
    storageClient1 = new StorageClient1(trackerServer, storageServer);
  212.  
    int result = storageClient1.delete_file1(fileId);
  213.  
    return result;
  214.  
    } catch (Exception ex) {
  215.  
    logger.error(ex);
  216.  
    return 0;
  217.  
    } finally {
  218.  
    if (storageServer != null){
  219.  
    try {
  220.  
    storageServer.close();
  221.  
    } catch (IOException e) {
  222.  
    e.printStackTrace();
  223.  
    }
  224.  
    }
  225.  
    if (trackerServer != null){
  226.  
    try {
  227.  
    trackerServer.close();
  228.  
    } catch (IOException e) {
  229.  
    e.printStackTrace();
  230.  
    }
  231.  
    }
  232.  
    storageClient1 = null;
  233.  
    }
  234.  
    }
  235.  
     
  236.  
    /**
  237.  
    * <B>方法名称:</B><BR>
  238.  
    * <B>概要说明:</B><BR>
  239.  
    * @param oldFileId 旧文件id
  240.  
    * @param file 新文件
  241.  
    * @param path 新文件路径
  242.  
    * @return 上传成功返回id,失败返回null
  243.  
    */
  244.  
    public static String modify(String oldGroupName, String oldFileId, File file, String path) {
  245.  
    String fileid = null;
  246.  
    try {
  247.  
    // 先上传
  248.  
    fileid = upload(file, path);
  249.  
    if (fileid == null) {
  250.  
    return null;
  251.  
    }
  252.  
    // 再删除
  253.  
    int delResult = delete(oldGroupName, oldFileId);
  254.  
    if (delResult != 0) {
  255.  
    return null;
  256.  
    }
  257.  
    } catch (Exception ex) {
  258.  
    logger.error(ex);
  259.  
    return null;
  260.  
    }
  261.  
    return fileid;
  262.  
    }
  263.  
     
  264.  
    /**
  265.  
    * <B>方法名称:</B>获取文件后缀名<BR>
  266.  
    * <B>概要说明:</B>获取文件后缀名<BR>
  267.  
    * @param fileName
  268.  
    * @return 如:"jpg"、"txt"、"zip" 等
  269.  
    */
  270.  
    private static String getFileExt(String fileName) {
  271.  
    if (StringUtils.isBlank(fileName) || !fileName.contains(".")) {
  272.  
    return "";
  273.  
    } else {
  274.  
    return fileName.substring(fileName.lastIndexOf(".") + 1);
  275.  
    }
  276.  
    }
  277.  
    }

4、测试代码

  1.  
    package util;
  2.  
     
  3.  
    import java.io.File;
  4.  
    import java.io.InputStream;
  5.  
     
  6.  
    import org.apache.commons.io.FileUtils;
  7.  
     
  8.  
     
  9.  
     
  10.  
    public class FastDFSTest {
  11.  
     
  12.  
    /**
  13.  
    * 上传
  14.  
    */
  15.  
    public static void upload() throws Exception {
  16.  
    // id: group1/M00/00/00/wKgBr1crVnSAbI4sAAAeV2aU4jU126.jpg
  17.  
    // fastdfsBasePath = http://192.168.1.170/fastdfs
  18.  
    // url: http://192.168.1.170/fastdfs/group1/M00/00/00/wKgBr1crVnSAbI4sAAAeV2aU4jU126.jpg
  19.  
    String path = System.getProperty("user.dir") + File.separatorChar + "source" + File.separatorChar + "001.jpg";
  20.  
    File file = new File(path);
  21.  
    String fileId = FastDFSClientUtils.upload(file, path);
  22.  
    System.out.println("本地文件:" + path + ",上传成功! 文件ID为:" + fileId);
  23.  
    }
  24.  
     
  25.  
    /**
  26.  
    * 下载
  27.  
    */
  28.  
    public static void download() throws Exception {
  29.  
    // id: group1/M00/00/00/wKgBr1crVnSAbI4sAAAeV2aU4jU126.jpg
  30.  
    // url: http://192.168.1.170/fastdfs/group1/M00/00/00/wKgBr1crVnSAbI4sAAAeV2aU4jU126.jpg
  31.  
    String fileId = "group1/M00/00/00/wKgBr1crUIuAJ22iAADHTIxNXeI872.jpg";
  32.  
    InputStream inputStream = FastDFSClientUtils.download("group1", fileId);
  33.  
    System.out.println(inputStream.available());
  34.  
    String path = System.getProperty("user.dir") + File.separatorChar + "receive" + File.separatorChar + "001.jpg";
  35.  
    System.out.println(path);
  36.  
    //FileUtils.copyInputStreamToFile(inputStream, new File(path));
  37.  
    }
  38.  
     
  39.  
    /**
  40.  
    * 删除
  41.  
    */
  42.  
    public static void delete() throws Exception {
  43.  
    String fileId = "group1/M00/00/00/wKgBr1crVnSAbI4sAAAeV2aU4jU126.jpg";
  44.  
    int result = FastDFSClientUtils.delete("group1", fileId);
  45.  
    System.out.println(result == 0 ? "删除成功" : "删除失败:" + result);
  46.  
    }
  47.  
     
  48.  
     
  49.  
     
  50.  
    /**
  51.  
    * @param args
  52.  
    * @throws Exception
  53.  
    */
  54.  
    public static void main(String[] args) throws Exception {
  55.  
     
  56.  
     
  57.  
    //upload();
  58.  
    download();
  59.  
    Thread.sleep(10000);
  60.  
    download();
  61.  
    Thread.sleep(10000);
  62.  
    download();
  63.  
    //delete();
  64.  
     
  65.  
    }
  66.  
     
  67.  
    }
posted @ 2019-11-20 23:57  迎风飞舞de蒲公英  阅读(3011)  评论(0编辑  收藏  举报