fastdfs 下载文件操作

下载操作实际通过url就可以直接下载:一般通过在storage服务器上安装nginx来访问

搭建过程:https://blog.csdn.net/qq_41813208/article/details/103286576

其中

配置文件信息的部分截图:安装文章前面找一下

和前面类似分四步:

  1. 初始化配置文件
  2. 通过配置文件 和 trackerClient 连接tracker获取trackerServer
  3. 通过trackerServer来获取StorageClient对象
  4. 文件操作(查询,上传,下载)

记得要导入对应的包

    @Test
    void testDownload() {
        try {
            //1.初始化fastdfs客户端配置文件
            ClientGlobal.initByProperties("fastdfs/fdfs.properties");// 加载properties配置文件
            System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");//输出properties中配置的参数,检测properties文件是否生效
            System.out.println("charset=" + ClientGlobal.g_charset);

            // 2.获取trackerServer
            TrackerClient tracker = new TrackerClient();// 创建tracker客户端对象
            TrackerServer trackerServer = tracker.getConnection();//获得tracker连接对象

            //3.获取storageClient(通过trackerServer 和 storageServer:null)
            StorageServer storageServer = null;
            StorageClient1 client = new StorageClient1(trackerServer, storageServer);//通过tracker服务器返回storage服务器对象(客户端)

            //4.下载操作
            //方式一 文件id下载
            byte[] bytes = client.download_file1("group1/M00/00/00/wKgByl3hXDiAR662AAFLbNXkMmY992.jpg");//记得要传文件id返回字节流
            File file=new File("/home/angel/test.jpg");// 给定文件路径 和 名称
            FileOutputStream fileOutputStream=new FileOutputStream(file);//窗机输出流
            fileOutputStream.write(bytes);//写入数据
            fileOutputStream.close();//关闭输出流

            //方式二 组名+文件路径
            byte[] bytes2 = client.download_file("group1","M00/00/00/wKgByl3hXDiAR662AAFLbNXkMmY992.jpg");//记得修改
            File file2=new File("/home/angel/test2.jpg");// 给定文件路径 和 名称
            FileOutputStream fileOutputStream2=new FileOutputStream(file2);//窗机输出流
            fileOutputStream2.write(bytes);//写入数据
            fileOutputStream2.close();//关闭输出流
            

            trackerServer.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

其它操作在我的fastdfs专栏找一下https://blog.csdn.net/qq_41813208/category_9535310.html

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

诗水人间

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

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

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

打赏作者

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

抵扣说明:

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

余额充值