下载图片

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
const fs = require('fs')
const request = require('request')

var tsList = '图片地址.png'
core = 1//几线程运行

//下载文件
function download(url){
request.head(url,(err,res,body)=>{
console.log(res.statusCode)
if(res.statusCode == 200){
request(url).pipe(fs.createWriteStream(__dirname + `/out/pic.jpg`)).on('close',()=>{
console.log(url,'文件下载完成')
})
}
})
}
//异步调用
async function init(){
for(let n = 0;n < core;n++){
let data = await download(tsList)
}
return 'start'
}
//启动程序
init().then(function(data) {
console.log(data);
});