首页 > 其他分享 >Future 下载

Future 下载

时间:2022-12-28 14:22:20浏览次数:30  
标签:category String format fileName Future static path 下载

Future<void> downloadFile(String? fileName, String urlPath) async {
    final CancelToken cancelToken = CancelToken();
    try {
      final String path = DirectoryUtil.getStoragePath(
        fileName: fileName,
        category: 'project',
      ).nullSafe;
      final File file = File(path);
      await Dio().download(
        urlPath,
        file.path,
        cancelToken: cancelToken,
        onReceiveProgress: (int count, int total) {
          if (total != -1) {
            EasyLoading.showProgress(
              count / total,
              status: '下载中...',
            );
            if (count == total) {
              EasyLoading.dismiss();
            }
          }
        },
      );
    } catch (e) {
      EasyLoading.showError('下载失败');
    }
  }

  

import 'dart:async';
import 'dart:io';

import 'package:path_provider/path_provider.dart';
import 'package:synchronized/synchronized.dart';

/// getTemporaryDirectory
/// 设备上未备份的临时目录的路径,适用于存储下载文件的缓存。
/// 此目录中的文件可以随时清除。 *不会*返回新的临时目录。 相反,调用者负责在此目录中创建(和清理)文件或目录。 此目录的作用域是调用应用程序。
/// 在iOS上,它使用“NSCachesDirectory”API。
/// 在Android上,它在上下文中使用“getCacheDir”API。

/// getApplicationSupportDirectory
/// 应用程序可以在其中放置应用程序支持文件的目录的路径。
/// 将此文件用于您不想向用户公开的文件。 您的应用不应将此目录用于用户数据文件。
/// 在iOS上,它使用`NSApplicationSupportDirectory` API。 如果此目录不存在,则会自动创建。
/// 在Android上,此函数在上下文中使用`getFilesDir` API。

/// getApplicationDocumentsDirectory
/// 应用程序可能在其中放置用户生成的数据或应用程序无法重新创建的数据的目录路径。
/// 在iOS上,它使用`NSDocumentDirectory` API。 如果不是用户生成的数据,请考虑使用[getApplicationSupportDirectory]。
/// 在Android上,它在上下文上使用`getDataDirectory` API。 如果要让用户看到数据,请考虑改用[getExternalStorageDirectory]。

/// getExternalStorageDirectory
/// 应用程序可以访问顶级存储的目录的路径。在发出此函数调用之前,应确定当前操作系统,因为此功能仅在Android上可用。
/// 在iOS上,这个函数抛出一个[UnsupportedError],因为它不可能访问应用程序的沙箱之外。
/// 在Android上,它使用`getExternalFilesDir(null)`。

bool _initTempDir = false;
bool _initAppDocDir = false;
bool _initAppSupportDir = false;
bool _initStorageDir = false;

/// 配置初始化Directory。
void setInitDir({
  bool? initTempDir,
  bool? initAppDocDir,
  bool? initAppSupportDir,
  bool? initStorageDir,
}) {
  _initTempDir = initTempDir ?? _initTempDir;
  _initAppDocDir = initAppDocDir ?? _initAppDocDir;
  _initAppSupportDir = initAppSupportDir ?? _initAppSupportDir;
  _initStorageDir = initStorageDir ?? _initStorageDir;
}

/**
 * @Author: Sky24n
 * @GitHub: https://github.com/Sky24n
 * @Email: [email protected]
 * @Description: Directory Util.
 * @Date: 2019/05/09
 */

/// DirectoryUtil。
class DirectoryUtil {
  static DirectoryUtil? _singleton;

  static Lock _lock = Lock();

  static Directory? _tempDir;
  static Directory? _appDocDir;
  static Directory? _appSupportDir;
  static Directory? _storageDir;

  static Future<DirectoryUtil?> getInstance() async {
    if (_singleton == null) {
      await _lock.synchronized(() async {
        if (_singleton == null) {
          // keep local instance till it is fully initialized.
          // 保持本地实例直到完全初始化。
          var singleton = DirectoryUtil._();
          await singleton._init();
          _singleton = singleton;
        }
      });
    }
    return _singleton;
  }

  DirectoryUtil._();

  Future _init() async {
    int old = DateTime.now().millisecondsSinceEpoch;
    if (_initTempDir) {
      await initTempDir();
    }
    if (_initAppDocDir) {
      await initAppDocDir();
    }
    if (_initAppSupportDir) {
      await initAppSupportDir();
    }
    if (_initStorageDir) {
      await initStorageDir();
    }
    print(
        "thll DirectoryUtil init : ${DateTime.now().millisecondsSinceEpoch - old}");
  }

  static Future<Directory?> initTempDir() async {
    if (_tempDir == null) {
      _tempDir = await getTemporaryDirectory();
    }
    return _tempDir;
  }

  static Future<Directory?> initAppDocDir() async {
    if (_appDocDir == null) {
      _appDocDir = await getApplicationDocumentsDirectory();
    }
    return _appDocDir;
  }

  static Future<Directory?> initAppSupportDir() async {
    if (_appSupportDir == null) {
      _appSupportDir = await getApplicationSupportDirectory();
    }
    return _appSupportDir;
  }

  static Future<Directory?> initStorageDir() async {
    if (_storageDir == null) {
      if (Platform.isAndroid) {
        _storageDir = await getExternalStorageDirectory();
      }
    }
    return _storageDir;
  }

  /// 同步创建文件夹
  static Directory? createDirSync(String? path) {
    if (path == null) return null;
    Directory dir = Directory(path);
    if (!dir.existsSync()) {
      dir.createSync(recursive: true);
    }
    return dir;
  }

  /// 异步创建文件夹
  static Future<Directory?> createDir(String? path) async {
    if (path == null) return null;
    Directory dir = Directory(path);
    bool exist = await dir.exists();
    if (!exist) {
      dir = await dir.create(recursive: true);
    }
    return dir;
  }

  /// get path.
  /// dir
  /// category 分类,例如:Download,Pictures, Music等等
  /// fileName 文件名
  /// format 文件格式,如果文件名包含格式,则不需要
  static String? getPath(
    Directory? dir, {
    String? category,
    String? fileName,
    String? format,
  }) {
    if (dir == null) return null;
    StringBuffer sb = StringBuffer("${dir.path}");
    if (category != null) sb.write("/$category");
    if (fileName != null) sb.write("/$fileName");
    if (format != null) sb.write(".$format");
    return sb.toString();
  }

  /// get Temporary Directory file path.
  /// category 分类,例如:Download,Pictures, Music等等
  /// fileName 文件名
  /// format 文件格式,如果文件名包含格式,则不需要
  /// String path = DirectoryUtil.getTempPath(category: 'Pictures',fileName: 'demo.png');
  /// String path = DirectoryUtil.getTempPath(category: 'Pictures', fileName: 'demo', format: 'png');
  /// Android: /data/user/0/com.thl.flustars_example/cache/Pictures/demo.png
  /// iOS: xxx;
  static String? getTempPath({
    String? category,
    String? fileName,
    String? format,
  }) {
    return getPath(_tempDir,
        category: category, fileName: fileName, format: format);
  }

  /// get Application Documents Directory file path.
  /// fileName 文件名
  /// format 文件格式,如果文件名包含格式,则不需要
  /// category 分类,例如:Download,Pictures, Music等等
  /// String path = DirectoryUtil.getAppDocPath(category: 'Pictures', fileName: 'demo.png');
  /// String path = DirectoryUtil.getAppDocPath(category: 'Pictures', fileName: 'demo', format: 'png');
  /// Android: /data/user/0/com.thl.flustars_example/app_flutter/Pictures/demo.png
  /// iOS: xxx;
  static String? getAppDocPath({
    String? category,
    String? fileName,
    String? format,
  }) {
    return getPath(_appDocDir,
        category: category, fileName: fileName, format: format);
  }

  /// get Application Support Directory file path.
  /// fileName 文件名
  /// format 文件格式,如果文件名包含格式,则不需要
  /// category 分类,例如:video,image等等
  /// String path = DirectoryUtil.getAppSupportPath(category: 'Pictures', fileName: 'demo.png');
  /// String path = DirectoryUtil.getAppSupportPath(category: 'Pictures', fileName: 'demo', format: 'png');
  /// Android: /data/user/0/com.thl.flustars_example/files/Pictures/demo.png
  /// iOS: xxx;
  static String? getAppSupportPath({
    String? category,
    String? fileName,
    String? format,
  }) {
    return getPath(_appSupportDir,
        category: category, fileName: fileName, format: format);
  }

  /// get External Storage Directory file path.
  /// category 分类,例如:video,image等等
  /// fileName 文件名
  /// format 文件格式,如果文件名包含格式,则不需要
  /// String path = DirectoryUtil.getStoragePath(category: 'Download', fileName: 'demo.apk';
  /// String path = DirectoryUtil.getStoragePath(category: 'Download', fileName: 'demo', format: 'apk');
  /// Android: /storage/emulated/0/Android/data/com.thl.flustars_example/files/Download/demo.apk
  /// iOS: xxx;
  static String? getStoragePath(
      {String? category, String? fileName, String? format}) {
    return getPath(
      _storageDir,
      category: category,
      fileName: fileName,
      format: format,
    );
  }

  static Directory? createTempDirSync({String? category}) {
    String? path = getTempPath(category: category);
    return createDirSync(path);
  }

  static Directory? createAppDocDirSync({String? category}) {
    String? path = getAppDocPath(category: category);
    return createDirSync(path);
  }

  static Directory? createAppSupportDirSync({String? category}) {
    String? path = getAppSupportPath(category: category);
    return createDirSync(path);
  }

  static Directory? createStorageDirSync({String? category}) {
    String? path = getStoragePath(category: category);
    return createDirSync(path);
  }

  static Future<Directory?> createTempDir({String? category}) async {
    await initTempDir();
    String? path = getTempPath(category: category);
    return createDir(path);
  }

  static Future<Directory?> createAppDocDir({String? category}) async {
    await initAppDocDir();
    String? path = getAppDocPath(category: category);
    return createDir(path);
  }

  static Future<Directory?> createAppSupportDir({String? category}) async {
    await initAppSupportDir();
    String? path = getAppSupportPath(category: category);
    return createDir(path);
  }

  static Future<Directory?> createStorageDir({String? category}) async {
    await initStorageDir();
    String? path = getStoragePath(category: category);
    return createDir(path);
  }
}

  

标签:category,String,format,fileName,Future,static,path,下载
From: https://www.cnblogs.com/shangdishijiao/p/17010040.html

相关文章

  • 下载量过百万的吴恩达机器学习和深度学习笔记更新了!(附PDF下载)
    今天,我把吴恩达机器学习和深度学习课程笔记都更新了,并提供下载,这两本笔记非常适合机器学习和深度学习入门。(作者:黄海广)0.导语我和同学将吴恩达老师机器学习和深度学习课程笔......
  • asp.net 批量大文件上传下载
    ​ 以ASP.NETCoreWebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API,包括文件的上传和下载。 准备文件上传的API #region 文件上传......
  • php 批量大文件上传下载
    ​PHP用超级全局变量数组$_FILES来记录文件上传相关信息的。1.file_uploads=on/off 是否允许通过http方式上传文件2.max_execution_time=30 允许脚本最大执行时间......
  • CompletableFuture 使用总结
    转载请注明出处:1.Future使用对比Future表示一个异步计算的结果。它提供了isDone()来检测计算是否已经完成,并且在计算结束后,可以通过get()方法来获取计算结果。在异步......
  • PBC教程 | 超详细图解—如何在VS2015中配置PBC库(附密码学常用库的下载)
    导读很少有人用VS2015做基于PBC的开发,一些书籍的配置还是基于VC6.0。但是在使用过程中,VC6.0有各种不方便,例如没有代码提示功能,没有行号,没法实现代码块的折叠等各种问题!为此,......
  • LaTeX简易教程 | 02 LaTeX模板下载与结构详解
    导读LaTeX是我们科研过程中,非常重要的编写论文的工具。掌握LaTeX的基本用法,是科研人员必备的技能。学LaTeX的过程中,遇到很多坑,总结一下,写一份简易教程,方便大家入门LaTeX并能......
  • Eclipse插件下载地址汇总
    easyexploreimplementors反编译插件EclipseClassDecompiler​​http://marketplace.eclipse.org/content/eclipse-class-decompiler​​ tomcatPropertiesEditor ​​h......
  • CountDownLatch和FutureTask类使用方法解析
    摘要:使用CountDownLatch和FutureTask解决主线程需要拿到多个子线程任务的执行结果之后再进行执行的问题。综述  我们在工作中,经常遇到有些业务场景需要使用多线程异步......
  • Docker下RabbitMQ下载插件并且安装
    1.先去github下载到对应插件:​​延迟信息插件​​如需其它请访问:​​rabbitmq其它插件下载​​2.自己启动的rabbitmq容器号是多少dockerps或者dockerps-aqf"name=rabb......
  • 体素建模神器——MagicaVoxel使用教程简易教学(附软件下载)
    体素是体积元素的简称,是三维空间中分割的最小单位,大家可以把三维中的体素看做是二维空间的像素。今天我们来介绍一款特别轻便的体素编辑程序——MagicaVoxel。MagicaVoxel......