java连接FastDFS

本文详细介绍如何使用Java连接FastDFS,包括环境配置、文件上传、下载、删除等操作,为开发者提供全面的FastDFS文件管理解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.环境的搭建

里面有环境的搭建,还有tar包。很详细。

https://download.youkuaiyun.com/download/u012448904/11245305    

2.java连接FastDFS

1.resources下的fdfs_client.conf

connect_timeout = 2
network_timeout = 30
charSet = UTF-8
http.tracker_http_port = 8888
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 192.168.28.100:22122
import java.io.*;


import org.csource.common.MyException;
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;

/**
 *
 * @ClassName: FastDfsUtil
 * @Description: FastDfs文件管理简单工具类
 * @version V1.0
 */
public class FastDfsUtil {

    private static TrackerClient trackerClient = null;
    private static TrackerServer trackerServer = null;
    private static StorageServer storageServer = null;
    private static StorageClient storageClient = null;

    static {
        try {
            ClientGlobal.init("fdfs_client.conf");
            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageClient = new StorageClient(trackerServer, storageServer);
        } catch (IOException | MyException e) {
            throw new RuntimeException("FastDfs工具类初始化失败!");
        }
    }

    /**
     *
     * @Title: fdfsUpload
     * @Description: 通过文件流上传文件
     * @param @param inputStream 文件流
     * @param @param filename 文件名称
     * @param @return
     * @param @throws IOException
     * @param @throws MyException
     * @return String 返回文件在FastDfs的存储路径
     * @throws
     */
    public  String fdfsUpload(InputStream inputStream, String filename) throws IOException, MyException{
        String suffix = ""; //后缀名
        try{
            suffix = filename.substring(filename.lastIndexOf(".")+1);
        }catch (Exception e) {
            throw new RuntimeException("参数filename不正确!格式例如:a.png");
        }
        String savepath = ""; //FastDfs的存储路径
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buff)) != -1) {
            swapStream.write(buff, 0, len);
        }
        byte[] in2b = swapStream.toByteArray();
        String[] strings = storageClient.upload_file(in2b, suffix, null); //上传文件
        for (String str : strings) {
            savepath += "/" + str; //拼接路径
        }
        return savepath;
    }

    /**
     *
     * @Title: fdfsUpload
     * @Description: 本地文件上传
     * @param @param filepath 本地文件路径
     * @param @return
     * @param @throws IOException
     * @param @throws MyException
     * @return String 返回文件在FastDfs的存储路径
     * @throws
     */
    public static String fdfsUpload(String filepath) throws IOException, MyException{
        String suffix = ""; //后缀名
        try{
            suffix = filepath.substring(filepath.lastIndexOf(".")+1);
        }catch (Exception e) {
            throw new RuntimeException("上传的不是文件!");
        }
        String savepath = ""; //FastDfs的存储路径
        //String[] strings = storageClient.upload_file(filepath.getBytes(), suffix, null);
        String[] strings = storageClient.upload_file(filepath, suffix, null); //上传文件
        for (String str : strings) {
            savepath += "/" + str; //拼接路径
        }
        return savepath;
    }

    /**
     *
     * @Title: fdfsDownload
     * @Description: 下载文件到目录
     * @param @param savepath 文件存储路径
     * @param @param localPath 下载目录
     * @param @return
     * @param @throws IOException
     * @param @throws MyException
     * @return boolean 返回是否下载成功
     * @throws
     */
    public static boolean fdfsDownload(String savepath, String localPath) throws IOException, MyException{
        String group = ""; //存储组
        String path = ""; //存储路径
        try{
            int secondindex = savepath.indexOf("/", 2); //第二个"/"索引位置
            group = savepath.substring(1, secondindex); //类似:group1
            path = savepath.substring(secondindex + 1); //类似:M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png
        }catch (Exception e) {
            throw new RuntimeException("传入文件存储路径不正确!格式例如:/group1/M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png");
        }
        int result = storageClient.download_file(group, path, localPath);
        if(result != 0){
            throw new RuntimeException("下载文件失败:文件路径不对或者文件已删除!");
        }
        return true;
    }

    /**
     *
     * @Title: fdfsDownload
     * @Description: 返回文件字符数组
     * @param @param savepath 文件存储路径
     * @param @return
     * @param @throws IOException
     * @param @throws MyException
     * @return byte[] 字符数组
     * @throws
     */
    public static byte[] fdfsDownload(String savepath) throws IOException, MyException{
        byte[] bs = null;
        String group = ""; //存储组
        String path = ""; //存储路径
        try{
            int secondindex = savepath.indexOf("/", 2); //第二个"/"索引位置
            group = savepath.substring(1, secondindex); //类似:group1
            path = savepath.substring(secondindex + 1); //类似:M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png
        }catch (Exception e) {
            throw new RuntimeException("传入文件存储路径不正确!格式例如:/group1/M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png");
        }
        bs = storageClient.download_file(group, path); //返回byte数组
        return bs;
    }

    /**
     *
     * @Title: fdfsDeleteFile
     * @Description: 删除文件
     * @param @param savepath 文件存储路径
     * @param @return
     * @param @throws IOException
     * @param @throws MyException
     * @return boolean 返回true表示删除成功
     * @throws
     */
    public static boolean fdfsDeleteFile(String savepath) throws IOException, MyException{
        String group = ""; //存储组
        String path = ""; //存储路径
        try{
            int secondindex = savepath.indexOf("/", 2); //第二个"/"索引位置
            group = savepath.substring(1, secondindex); //类似:group1
            path = savepath.substring(secondindex + 1); //类似:M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png
        }catch (Exception e) {
            throw new RuntimeException("传入文件存储路径不正确!格式例如:/group1/M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png");
        }
        int result = storageClient.delete_file(group, path); //删除文件,0表示删除成功
        if(result != 0){
            throw new RuntimeException("删除文件失败:文件路径不对或者文件已删除!");
        }
        return true;
    }

    /**
     *
     * @Title: fdfdFileInfo
     * @Description: 返回文件信息
     * @param @param savepath 文件存储路径
     * @param @return
     * @param @throws IOException
     * @param @throws MyException
     * @return FileInfo 文件信息
     * @throws
     */
    public static FileInfo fdfdFileInfo(String savepath) throws IOException, MyException{
        String group = ""; //存储组
        String path = ""; //存储路径
        try{
            int secondindex = savepath.indexOf("/", 2); //第二个"/"索引位置
            group = savepath.substring(1, secondindex); //类似:group1
            path = savepath.substring(secondindex + 1); //类似:M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png
        }catch (Exception e) {
            throw new RuntimeException("传入文件存储路径不正确!格式例如:/group1/M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png");
        }
        FileInfo fileInfo = storageClient.get_file_info(group, path);
        return fileInfo;
    }


/*    public static void main(String[] args) {
        FastDfsUtil fast = new FastDfsUtil();
        try {
            String str="shenningwuid";
            InputStream stream= new ByteArrayInputStream(str.getBytes());
            String txt = fast.fdfsUpload(stream, "txt");
            System.out.println(txt);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }*/
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值