当前位置:首页 > 谈天说地

Flutter实现资源下载断点续传的示例代码

34资源网2022-07-29428

协议梳理

一般情况下,下载的功能模块,至少需要提供如下基础功能:资源下载、取消当前下载、资源是否下载成功、资源文件的大小、清除缓存文件。而断点续传主要体现在取消当前下载后,再次下载时能在之前已下载的基础上继续下载。这个能极大程度的减少我们服务器的带宽损耗,而且还能为用户减少流量,避免重复下载,提高用户体验。

前置条件:资源必须支持断点续传。如何确定可否支持?看看你的服务器是否支持range请求即可

实现步骤

1.定好协议。我们用的http库是dio;通过校验md5检测文件缓存完整性;关于代码中的subdir,设计上认为资源会有多种:音频、视频、安装包等,每种资源分开目录进行存储。

import 'package:dio/dio.dart';

typedef progresscallback = void function(int count, int total);

typedef canceltokenprovider = void function(canceltoken canceltoken);

abstract class assetrepositoryprotocol {
  /// 下载单一资源
  future<string> downloadasset(string url,
      {string? subdir,
      progresscallback? onreceiveprogress,
      canceltokenprovider? canceltokenprovider,
      function(string)? done,
      function(exception)? failed});

  /// 取消下载,dio中通过canceltoken可控制
  void canceldownload(canceltoken canceltoken);

  /// 获取文件的缓存地址
  future<string?> filepathforasset(string url, {string? subdir});

  /// 检查文件是否缓存成功,简单对比md5
  future<string?> checkcachedsuccess(string url, {string? md5str});
  
  /// 查看缓存文件的大小
  future<int> cachedfilesize({string? subdir});

  /// 清除缓存
  future<void> clearcache({string? subdir});
}

2.实现抽象协议,其中httpmanagerprotocol内部封装了dio的相关请求。

class assetrepository implements assetrepositoryprotocol {
  assetrepository(this.httpmanager);

  final httpmanagerprotocol httpmanager;

  @override
  future<string> downloadasset(string url,
      {string? subdir,
      progresscallback? onreceiveprogress,
      canceltokenprovider? canceltokenprovider,
      function(string)? done,
      function(exception)? failed}) async {
    canceltoken canceltoken = canceltoken();
    if (canceltokenprovider != null) {
      canceltokenprovider(canceltoken);
    }

    final savepath = await _getsavepath(url, subdir: subdir);
    try {
      httpmanager.downloadfile(
          url: url,
          savepath: savepath + '.temp',
          onreceiveprogress: onreceiveprogress,
          canceltoken: canceltoken,
          done: () {
            done?.call(savepath);
          },
          failed: (e) {
            print(e);
            failed?.call(e);
          });
      return savepath;
    } catch (e) {
      print(e);
      rethrow;
    }
  }

  @override
  void canceldownload(canceltoken canceltoken) {
    try {
      if (!canceltoken.iscancelled) {
        canceltoken.cancel();
      }
    } catch (e) {
      print(e);
    }
  }

  @override
  future<string?> filepathforasset(string url, {string? subdir}) async {
    final path = await _getsavepath(url, subdir: subdir);
    final file = file(path);
    if (!(await file.exists())) {
      return null;
    }
    return path;
  }

  @override
  future<string?> checkcachedsuccess(string url, {string? md5str}) async {
    string? path = await _getsavepath(url, subdir: filetype.video.dirname);
    bool iscached = await file(path).exists();
    if (iscached && (md5str != null && md5str.isnotempty)) {
      // 存在但是md5验证不通过
      file(path).readasbytes().then((uint8list str) {
        if (md5.convert(str).tostring() != md5str) {
          path = null;
        }
      });
    } else if (iscached) {
      return path;
    } else {
      path = null;
    }
    return path;
  }
  
  @override
  future<int> cachedfilesize({string? subdir}) async {
    final dir = await _getdir(subdir: subdir);
    if (!(await dir.exists())) {
      return 0;
    }

    int totalsize = 0;
    await for (var entity in dir.list(recursive: true)) {
      if (entity is file) {
        try {
          totalsize += await entity.length();
        } catch (e) {
          print('get size of $entity failed with exception: $e');
        }
      }
    }

    return totalsize;
  }

  @override
  future<void> clearcache({string? subdir}) async {
    final dir = await _getdir(subdir: subdir);
    if (!(await dir.exists())) {
      return;
    }
    dir.deletesync(recursive: true);
  }

  future<string> _getsavepath(string url, {string? subdir}) async {
    final savedir = await _getdir(subdir: subdir);

    if (!savedir.existssync()) {
      savedir.createsync(recursive: true);
    }

    final uri = uri.parse(url);
    final filename = uri.pathsegments.last;
    return savedir.path + filename;
  }

  future<directory> _getdir({string? subdir}) async {
    final cachedir = await gettemporarydirectory();
    late final directory savedir;
    if (subdir == null) {
      savedir = cachedir;
    } else {
      savedir = directory(cachedir.path + '/$subdir/');
    }
    return savedir;
  }
}

3.封装dio下载,实现资源断点续传。

这里的逻辑比较重点,首先未缓存100%的文件,我们以.temp后缀进行命名,在每次下载时检测下是否有.temp的文件,拿到其文件字节大小;传入在header中的range字段,服务器就会去解析需要从哪个位置继续下载;下载全部完成后,再把文件名改回正确的后缀即可。

final downloaddio = dio();

future<void> downloadfile({
  required string url,
  required string savepath,
  required canceltoken canceltoken,
  progresscallback? onreceiveprogress,
  void function()? done,
  void function(exception)? failed,
}) async {
  int downloadstart = 0;
  file f = file(savepath);
  if (await f.exists()) {
    // 文件存在时拿到已下载的字节数
    downloadstart = f.lengthsync();
  }
  print("start: $downloadstart");
  try {
    var response = await downloaddio.get<responsebody>(
      url,
      options: options(
        /// receive response data as a stream
        responsetype: responsetype.stream,
        followredirects: false,
        headers: {
          /// 加入range请求头,实现断点续传
          "range": "bytes=$downloadstart-",
        },
      ),
    );
    file file = file(savepath);
    randomaccessfile raf = file.opensync(mode: filemode.append);
    int received = downloadstart;
    int total = await _getcontentlength(response);
    stream<uint8list> stream = response.data!.stream;
    streamsubscription<uint8list>? subscription;
    subscription = stream.listen(
      (data) {
        /// write files must be synchronized
        raf.writefromsync(data);
        received += data.length;
        onreceiveprogress?.call(received, total);
      },
      ondone: () async {
        file.rename(savepath.replaceall('.temp', ''));
        await raf.close();
        done?.call();
      },
      onerror: (e) async {
        await raf.close();
        failed?.call(e);
      },
      cancelonerror: true,
    );
    canceltoken.whencancel.then((_) async {
      await subscription?.cancel();
      await raf.close();
    });
  } on dioerror catch (error) {
    if (canceltoken.iscancel(error)) {
      print("download cancelled");
    } else {
      failed?.call(error);
    }
  }
}

写在最后

这篇文章确实没有技术含量,水一篇,但其实是实用的。这个断点续传的实现有几个注意的点:

  • 使用文件操作的方式,区分后缀名来管理缓存的资源;
  • 安全性使用md5校验,这点非常重要,断点续传下载的文件,在完整性上可能会因为各种突发情况而得不到保障;
  • 在资源管理协议上,我们将下载、检测、获取大小等方法都抽象出去,在业务调用时比较灵活。

以上就是flutter实现资源下载断点续传的示例代码的详细内容,更多关于flutter资源下载断点续传的资料请关注萬仟网其它相关文章!

看完文章,还可以扫描下面的二维码下载快手极速版领4元红包

快手极速版二维码

快手极速版新人见面礼

除了扫码领红包之外,大家还可以在快手极速版做签到,看视频,做任务,参与抽奖,邀请好友赚钱)。

邀请两个好友奖最高196元,如下图所示:

快手极速版邀请好友奖励

扫描二维码推送至手机访问。

版权声明:本文由34楼发布,如需转载请注明出处。

本文链接:https://www.34l.com/post/20000.html

分享给朋友:

相关文章

打败你的不一定是你的对手,有可能是一个过路人

打败你的不一定是你的对手,有可能是一个过路人

有句话叫做“羊毛出在狗身上,由猪买单”…

用不言谢造句17句分享

用不言谢造句17句分享

1、大恩不言谢,以后有什么用得上我的,尽管说!2、人们都说大恩不言谢,又说滴水之恩当涌泉相报,谢还是不谢?好为难!那今儿就不谢天不谢地,只谢朋友,谢谢你风雨一路的陪伴。感恩节快乐!3、大恩不言谢,我就是结草衔环,也不足为报。…

​50句名言警句摘抄大全

​50句名言警句摘抄大全

网上的名言警句有很多,相信大家想找的话,一下子就能找到几千上万句这类名言名句,这类名言名句只有短短的几个字,却蕴藏着巨大的人生哲理,有些可能对你的人生有很大帮助。喜欢名言警句的朋友,赶紧看下面小编摘录的50句名言警句吧,一定会对你有所帮助的…

koko卡卡拖地机器人家用全自动擦地机推荐

koko卡卡拖地机器人家用全自动擦地机推荐

koko卡卡智能拖地机是东莞市宝联电子科技有限公司旗下产品,隶属于香港概念数码科技有限公司,其主要研发机器人吸尘器等高科技领域家居产品,想知道卡卡智能拖地机好用吗,看看下面是网友使用koko卡卡智能拖地机的相关介绍,希望对大家有所帮助。1、…

视频号入口在哪里(视频号直播入口新手手册)

视频号入口在哪里(视频号直播入口新手手册)

视频号助手在哪里?视频号助手什么时候上线?微信视频号助手正式上线目前微信视频号助手已经开始内测使用了,大家可以直接在PC端扫码登录,管理自己的视频号,可以看到自己的各项动态数据,非常方便管理。 视频号助手在哪里 视频号助手在哪里? 手机微…

带货主播成正式工种;AITO汽车来了;小米、英特尔入股张艺谋等创办的VR公司 ;特斯拉推出儿童电动车丨邦早报

带货主播成正式工种;AITO汽车来了;小米、英特尔入股张艺谋等创办的VR公司 ;特斯拉推出儿童电动车丨邦早报

【电子驾驶证12月10日起全国全面推行】据公安部消息,12月10日起,电子驾驶证在全国全面推行。此前,驾驶证电子化已覆盖北京、上海、广州、西安等200个城市,5000多万名驾驶人领取了电子驾驶证,实现手机在线“亮证”。电子驾驶证通过全国“交…