鸿蒙Next文件下载RCP单线程和多线程使用对比
2025-06-27 22:50:34
104次阅读
0个评论
本文介绍: 1.基于RCP中提供封装好的Session.downloadToFile()方法进行文件下载 2.基于TaskPool和RCP的三方库SFFT实现多线程下载,原理是将文件分割为多个小块,由多个线程同时下载这些部分,并发写入到本地文件中,从而实现高效、稳定的下载
看一下实现效果: 当前网络情况下,同时下同一个文件,使用SFFT多线程下载大概快10s 看一下两种方式单独下载的对比:发现相差不大。
看一下两种方式同时下载,不同线程数下载对比:
简单对比,STTF多线程下载还是相对要快一些。 接下来介绍一下两种方式的实现方式: ####RCP 1.创建HTTP会话rcp.createSession() 2.downloadToFile(url: URLOrString, downloadTo: DownloadToFile): url:请求资源的URL downloadTo:保存到本地文件系统中的指定位置
const uiContext: UIContext | undefined = AppStorage.get('uiContext');
let context = uiContext!.getHostContext()!;
function genSessionConfig(httpEventsHandler?: rcp.HttpEventsHandler) {
const config: rcp.SessionConfiguration = {
baseAddress: ApiConstants.SERVER,
requestConfiguration: {
tracing: { httpEventsHandler },
transfer: { //自动重定向和超时设置
timeout: { //接和传输数据所允许的最长时间
connectMs: 1000 * 60 * 20, //允许建立连接的最长时间
transferMs: 1000 * 60 * 20 //允许传输数据的最长时间
}
}
}
};
return config;
}
export function download(downloadUrl: string, httpEventsHandler: rcp.HttpEventsHandler) {
const destPath =`${context.filesDir}/${downloadUrl.split('/').pop() || ''}`;
const rcpSession = rcp.createSession(genSessionConfig(httpEventsHandler));
const downloadTo: rcp.DownloadToFile = {
kind: 'file',
file: destPath
};
return rcpSession.downloadToFile(downloadUrl, downloadTo)
.then(() => destPath)
.finally(() => {
rcpSession.close();
});
}
####SFFT 1.引入第三方库,gitcode搜索super_fast_file_trans,或将源码下载下来,作为一个module引入 在entry中oh-package.json5中增加配置"@hadss/super_fast_file_trans": "file:../library_ssf" 2.配置DownloadConfig 3.根据配置创建下载任务 DownloadManager.getInstance().createDownloadTask 4.start()开始下载
const uiContext: UIContext | undefined = AppStorage.get('uiContext');
let context = uiContext!.getHostContext()!;
let downloadInstance: DownloadTask | undefined;
function getDownloadConfig(url:string,fileName:string,concurrency?:number): DownloadConfig{
return {
url: url, // 远端文件url地址
fileName: fileName+'ssf', // 本地文件名
concurrency:concurrency!=0?concurrency:1, // 启用的线程数,concurrency为1~8的正整数
isBreakpointResume: false, // 是否启用断点续下,isBreakpointResume为true时启用
maxRetries: 3, // 重试次数为3次
retryInterval: 2000, // 重试间隔为2000ms
}
}
export async function sfftDownload(downloadUrl:string,downloadListener: DownloadListener,concurrency?:number) {
await DownloadManager.getInstance().init(context as common.UIAbilityContext);
// 根据配置创建下载任务
downloadInstance = DownloadManager.getInstance().createDownloadTask(getDownloadConfig(downloadUrl,downloadUrl.split('/').pop() || '',concurrency), downloadListener);
await downloadInstance?.start()
}
Page中下载回调配置
this.downloadListener = {
onStart: (trialResponseHeaders: Record<string, string | string[] | undefined>) => {
this.ssfstarttime = new Date().getTime()
},
onSuccess: (filePath: string) => {
this.ssfdownloadtime = (new Date().getTime() - this.ssfstarttime) / 1000
this.ssfProgress = 0;
},
onProgressUpdate: (downloadProgress: DownloadProgressInfo) => {
let transferredSize = downloadProgress.transferredSize;
this.totalSize = downloadProgress.totalSize;
this.speed = downloadProgress.speed/1024/1024;
this.ssfProgress = transferredSize / this.totalSize * 100;
}
}
00
- 0回答
- 0粉丝
- 0关注
相关话题
- 鸿蒙Next网络请求HTTP和RCP的使用和对比
- 鸿蒙Next并发线程TaskPool使用
- 第二四课:HarmonyOS Next多线程与并发开发实战指南
- 第二四课:HarmonyOS Next多线程与并发开发实战指南
- HarmonyNext技术深度解析:ArkTS在鸿蒙系统中的多线程与并发编程实践
- (六三)ArkCompiler 的多线程编译:并行编译实现机制与编译速度提升
- 鸿蒙Next使用AVRecorder录制和播放音频
- Flutter 鸿蒙化 使用 Flutter Channel实现和Flutter和HarmonyOS交互
- 鸿蒙Next使用AudioCapturer实现音频录制和AI语言转文字
- 华为仓颉语言初识:并发编程之线程的基本使用
- 使用 HarmonyOS NEXT和Mass快速开发NutPITalk
- 《HarmonyOSNext性能暴增秘籍:Node-API多线程通信从阻塞到丝滑的4大方案实战》
- 鸿蒙next RCP网络请求工具类进阶版来了
- 鸿蒙next RCP网络请求工具类基础封装来了
- 鸿蒙-flutter-使用FlutterEntry的路由管理和参数传递_上