java中使用FastDFS处理文件

1.pom文件直接引入依赖是下载不下来的

    <dependency>
      <groupId>org.csource</groupId>
      <artifactId>fastdfs-client-java</artifactId>
      <version>1.27-SNAPSHOT</version>
    </dependency>

2.下载相关包,链接:https://github.com/happyfish100/fastdfs-client-java

将下载zip包解压到一个目录,然后在进到fastdfs-client-master目录中,按住shift然后右键选在此处打开Powershrll窗口

会下载到自己maven的配置文件setting.xml配置的路径库中

3.FastDFS配置,可以看FastDFS的API,我项目配置文件是application.properties文件

 

4.配置文件中只加了两个值,key不要写错,对应API的ClientGlobal.class中都有,copy过来就可以了

fastdfs.tracker_servers的值对应tracker的地址和ip,

fastdfs.http_tracker_http_port的值默认是80,试过对应的值写哪个端口都是可以的,但是必须要有这个key,要不会报错

#application.properties
fastdfs.tracker_servers=20.200.54.230:22122,20.200.54.231:22122
fastdfs.http_tracker_http_port=8888

5.FastDFS上传,下载,删除的工具类,ClientGlobal.configInfo()方法会输出配置文件你加的内容,有的是默认的


import org.apache.log4j.Logger;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class FastDFSUtil {

    private static final String GROUP_NAME = "group1";
    private static final Integer STORAGE_SERVER_PORT = 23000;
    private static final Logger log = Logger.getLogger(FastDFSUtil.class);
    private static TrackerClient trackerClient;
    private static TrackerServer trackerServer;

    static {
        try {
            ClientGlobal.initByProperties("application.properties");
            log.info("FastDFS配置加载info:" + ClientGlobal.configInfo());
            if (trackerServer == null) {
                trackerClient = new TrackerClient();
                trackerServer = trackerClient.getConnection();                
            }
        } catch (IOException | MyException e) {
            e.printStackTrace();
        }
    }

    /**
     * 向FastDFS上传文件
     * @param file 本地文件
     * @param type 传到哪个目录路径 M00 M01     
     */
    public static String uploadFile (File file, Integer type) {
        if (trackerServer == null) {
            return null;
        }
        String result = null;
        try {
            if (type == null) {
                type = 0;
            }
            //上传指定路径了,0对应的是M00,1对应的是M01
            StorageServer storageServer = new StorageServer(getStorageServerIp(), STORAGE_SERVER_PORT, type);
            StorageClient1 storageClient = new StorageClient1(trackerServer, storageServer);
            result = storageClient.upload_file(file.getPath(), null, null);
            if (result != null && "".equals(result)) {
                log.info("向FastDFS上传文件失成功,路径: " + result);
            } else {
                log.info("向FastDFS上传文件失败");
            }
        } catch (IOException | MyException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * FastDFS下载文件
     * @param localFileName 本地文件名
     * @param remoteFileName 文件在FastDFS的名称--路径
     */
    public static void downLoadFile(String localFileName, String remoteFileName) {
        if (trackerServer == null) {
            return;
        }
        File file = new File(localFileName);
        try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
            StorageClient storageClient = new StorageClient(trackerServer, null);
            byte[] content = storageClient.download_file(GROUP_NAME, remoteFileName);
            if (content == null || content.length == 0) {
                log.info("文件不存在");
                return;
            }
            os.write(content);
            log.info("成功下载文件:" + localFileName);
        } catch (IOException | MyException e) {
            e.printStackTrace();
        }
    }


    /**
     * 从FastDFS删除文件
     * @param remoteFileName 文件在FastDFS的名称
     */
    public static void deleteFile (String remoteFileName) {
        if (trackerServer == null) {
            return;
        }
        try {
            StorageClient storageClient = new StorageClient(trackerServer, null);
            int i = storageClient.delete_file(GROUP_NAME, remoteFileName);
            if (i == 0) {
                log.info("FastDFS删除文件成功");
            } else {
                log.info("FastDFS删除文件失败");
            }
        } catch (IOException | MyException e) {
            e.printStackTrace();
        }
    }


    private static String getStorageServerIp() {
        String ip = null;
        if (trackerServer != null && trackerClient != null) {
            try {
                StorageServer storageServer = trackerClient.getStoreStorage(trackerServer, GROUP_NAME);
                ip = storageServer.getSocket().getInetAddress().getHostAddress();
            } catch (IOException e) {
                log.error("获取StorageServer的ip出错",e);
            }
        }
        return ip;
    }

    /**
     * 关闭trackerServer连接
     */
    private static void closeTrackerServer(TrackerServer trackerServer) {
        try {
            if (trackerServer != null) {
                trackerServer.close();
                log.info("关闭trackerServer连接");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

6.测试接口

public void test () {

    //删除
    FastDFSUtil.deleteFile("M01/00/00/xxxxxxxxxxxxxxx.jpg");
      
    //上传
    File file = new File("D:\\000.jpg);
    //第二个参数可以可以传null,默认为0,上传到M00,也可以传1,传1上传到M01
    FastDFSUtil.uploadFile(file, null);
    
    //下载
    FastDFSUtil.downLoadFile("D:\\test.jpg", "M00/00/00/xxxxxxxxxxxxx.jpg");
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值