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

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

34资源网2022-07-29476

协议梳理

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

前置条件:资源必须支持断点续传。如何确定可否支持?看看你的服务器是否支持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

分享给朋友:

相关文章

用侠肝义胆造句30句

用侠肝义胆造句30句

1、李大哥侠肝义胆,受到四邻的交口称赞。…

疫情还是很严峻,最近去超市都要检测了

疫情还是很严峻,最近去超市都要检测了

疫情还是很严峻,最近去超市也要各种检测了,记得前不久我去超市还是比较宽松的,基本上没有专门的检测员,口罩也不用戴。最近可能是外省一些地区疫情比较严重了,所以温州这边现在检测也更加严格了,我最近去超市买东西都要各种检测,买个菜呀,买些零食吃的…

谈2022年平衡发展策略,我们要平衡的发展

谈2022年平衡发展策略,我们要平衡的发展

2022年解决方案的第二个关键词叫平衡。我们要平衡的发展,长期的发展。你的个人生活,你的公司事业,还有国家的形势,我觉得一定要平衡的去发展,不能说我只赚钱,身体就搞垮,或者说我只赚快钱,但是不关心国家的形势。你看,密室逃脱这个行业,上周国家…

融资丨「奕斯伟计算」完成25亿元C轮融资,目标物联网芯片领域全球领导者

融资丨「奕斯伟计算」完成25亿元C轮融资,目标物联网芯片领域全球领导者

创业邦获悉,12月1日,AIoT芯片与解决方案提供商北京奕斯伟计算技术有限公司(以下简称:奕斯伟计算)宣布完成25亿元人民币C轮融资,由金石投资和中国互联网投资基金联合领投,尚颀投资、国开科创、华新投资等跟投,老股东 IDG、君联资本、刘益…

为了让米粉买到好看的手机壳,雷军投了一家“潮玩工厂”

为了让米粉买到好看的手机壳,雷军投了一家“潮玩工厂”

小米团队花了几个月时间,想要找到一个能DIY打印手机壳的设备却没有找到。直到2020年底,他们在西单大悦城看到玩壳工厂。 “用户需求一直存在,早前也有不少团队尝试开发这样的设备,但都失败了。”玩壳工厂创始人CEO韩冰告诉创业邦。 如今,玩壳…

B站、爱奇艺:这对难兄难弟

11月17日,哔哩哔哩(下称“B站”)与爱奇艺发布截至2021年9月30日的第三季度财报。B站Q3营收52.066亿元,同比增长61%,净亏损26.863亿元;爱奇艺Q3营收75.891亿元,同比增长6%,净亏损16.984亿元。 两家先后…