迅雷的批量下载满好用。但是有两点我不太中意。在这个脚本里会有所增强
1、不能设置保存的文件名。
2、不能单独设置这批下载的线程限制。
使用方法
// 下载从编号001到编号020的图片,保存名为猫咪写真*.jpg 使用6个线程
jdlp http://bizhi.zhuoku.com/bizhi/200804/0422/cat/cat{*}.jpg d:\草原上的可爱猫咪\ 猫咪写真{*}.jpg 20 1 3 6
脚本备份
View Code
var rslt = [];
var urlPattern = arguments[0].replaceAll("(http|https)://", "");
var targetDir = arguments[1] || "d:\\";
var filenamePattern = arguments[2] || "图片";
var endIndx = arguments[3];
var startIndx = arguments[4];
var numlen = arguments[5];
var threadnum = arguments[6];
var service = java.util.concurrent.Executors.newFixedThreadPool(threadnum);
mkdir(targetDir);
function downloadTask(indx) {
var url = "http://" + urlPattern.replace("{*}", indx.toString());
var file = targetDir + filenamePattern.replace("{*}", indx.toString());
return function() {
// 生成一个url 生成目标文件
try {
println("downloading : " + url);
cp(url, file);
println("downloaded : " + url);
} catch(e) {
println("download error : " + url);
println("reasion :" + e);
}
}
}
function fillStr(str, c) {
var sb = new java.lang.StringBuilder(str.toString());
var len = sb.length();
if (len > numlen) {
return sb.toString().substring(0, len);
} else {
for (var cnt = 1; cnt <= (numlen - len); cnt++)
sb.insert(0, c);
return sb.toString();
}
}
function checkAllDone() {
for each(var futur in rslt) {
if (future.isDone() == false) return false;
}
return true;
}
for (var i = startIndx; i <= endIndx; i++) {
var indx = fillStr(i, '0');
var future = service.submit(new java.lang.Thread(downloadTask(indx)));
rslt.push(future);
}
service.shutdown();
// 等待结束
while (checkAllDone() == false) {
java.lang.Thread.sleep(1000);
}
println("~~~~over~~~");