多线程下载ts文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const fs = require('fs')
const request = require('request')

var tsList = 'ts地址'//ts地址
core = 4//几线程运行

//下载文件
function download(url,filename,count){
request.head(url,(err,res,body)=>{
console.log(filename,res.statusCode)
if(res.statusCode == 200){
request(url).pipe(fs.createWriteStream(__dirname + `/out/${filename}.ts`)).on('close',()=>{
console.log(filename,'文件下载完成')
count += core
download(tsList+`${addZero(count,4)}.ts`,addZero(count,4),count)
})
}
})
}
//补零
function addZero(num,n){
return (Array(n).join(0)+num).slice(-n)
}
//异步调用
async function init(){
for(let n = 0;n < core;n++){
let data = await download(tsList+`${addZero(n,4)}.ts`,addZero(n,4),n)
}
}
//启动程序
init().then(function(data) {
console.log(data);
});