download file

var Jsoup = Java.type('org.jsoup.Jsoup');
var Response = Java.type('org.jsoup.Connection.Response');
var FileOutputStream = Java.type('java.io.FileOutputStream');
var BufferedOutputStream = Java.type('java.io.BufferedOutputStream');
var File = Java.type('java.io.File');
var URL = Java.type('java.net.URL');
var HashMap = Java.type("java.util.HashMap");
var crawle = function() {
	var map = new HashMap();
    var list = Jsoup.connect("xxxx").get().select(".course_chapter_list a");
	var baseUrl="xxx";
	print("分析页面中。。。");
    for each(var link in list){
        var url = baseUrl+link.attr("href"); 
		var list = Jsoup.connect(url).get().select(".downlist a");
		for each(var link in list){
			var url = link.attr("href"); 
			var title = link.attr("title");
			map.put(url,title);
		}
    }
	print(key+":"+map.get(key));
	var i=0;
	print("开始下载文件。。。");
	for each (var key in map.keySet()){
		i++;
		var fileExt =key.substring(key.lastIndexOf("."));
		print("下载第"+i+"个文件:"+map.get(key));
		var url = new URL(key);
		var ips = url.openStream();
		var out = new BufferedOutputStream(new FileOutputStream("down/"+ map.get(key)+fileExt));
		for (var b; (b = ips.read()) != -1;) {
		    out.write(b);
		}
		out.close();
		ips.close();
	} 
	print("共下载"+i+"个文件");
};
crawle();

你可能感兴趣的:(download)