下载全部版本jQuery的java代码

来源:互联网 发布:python多线程共享变量 编辑:程序博客网 时间:2024/06/02 15:42

手动下载全部版本jQuery太慢。直接代码走起



package com.jquery;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.URL;import java.util.ArrayList;import java.util.List;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;/** *  * 将jquery网站上各个版本的jquery下载到本地 http://code.jquery.com/jquery/ */public class DownloadAll {private static final String JQUER_CODE_URL = "http://code.jquery.com/";//jquery列表网站private static String localPath = "E:\\data\\jquery\\";//本地存储路径public static void main(String[] args) {try {List<String> list = getUrls();writerJsFile(list);} catch (Exception e) {e.printStackTrace();}}private static List<String> getUrls() throws Exception {// 获取要下载js文件的名字列表List<String> list = new ArrayList<String>();URL rootUrl = new URL(JQUER_CODE_URL+"/jquery");Document doc = Jsoup.parse(rootUrl, 1000);Element content = doc.getElementById("content");Elements lis = content.getElementsByTag("li");int size = lis.size();for (int i = 0; i < size; i++) {Element li = lis.get(i);Elements as = li.getElementsByTag("a");if (as.size() > 0) {for (Element a : as) {String href = a.attr("href");if (href != null && href.endsWith(".js")) {String name = href.substring(1);list.add(name);}}}}return list;}public static void writerJsFile(List<String> list) throws IOException {for (int i = 0; i < list.size(); i++) {String jsName = list.get(i);String jsFileUrl = JQUER_CODE_URL + jsName;URL downUrl = new URL(jsFileUrl);HttpURLConnection conn = (HttpURLConnection) downUrl.openConnection();conn.setRequestProperty("User-Agent", "MSIE 7.0");InputStream in = conn.getInputStream();BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf8"));File jsFile = new File(localPath + jsName);jsFile.createNewFile();BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(jsFile, true), "utf-8"));String line;while ((line = reader.readLine()) != null) {writer.write(line + "\n");}writer.flush();writer.close();reader.close();}}}


0 0