Java将网页上的js下载下来。
2020-12-13 05:56
标签:iss ace with == port ringbuf static final set Java将网页上的js下载下来。 标签:iss ace with == port ringbuf static final set 原文地址:https://www.cnblogs.com/csh520mjy/p/11155895.html 1 import java.io.BufferedReader;
2 import java.io.File;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
6 import java.net.HttpURLConnection;
7 import java.net.URL;
8
9 import com.hanweb.common.util.FileUtil;
10
11 /**
12 * 获取网页js文件,请求时间会很长
13 *
14 * @author csh
15 *
16 */
17 public class DowloadJSUtil {
18
19
20 //例子http://www.caoxian.gov.cn/script/0/1809251344255856.js
21
22 /**
23 * 读取js
24 * @param url
25 * @param encode
26 * @return
27 */
28 public static String getHtmlContent(URL url) {
29 StringBuffer contentBuffer = new StringBuffer();
30
31 int responseCode = -1;
32 HttpURLConnection con = null;
33 try {
34 con = (HttpURLConnection) url.openConnection();
35 con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");// IE代理进行下载
36 con.setConnectTimeout(60000);
37 con.setReadTimeout(60000);
38 // 获得网页返回信息码
39 responseCode = con.getResponseCode();
40 if (responseCode == -1) {
41 String re = url.toString() + " : connection is failure...";
42 con.disconnect();
43 return re;
44 }
45 if (responseCode >= 400) // 请求失败
46 {
47 String re = "请求失败:get response code: " + responseCode;
48 con.disconnect();
49 return re;
50 }
51
52 InputStream inStr = con.getInputStream();
53 InputStreamReader istreamReader = new InputStreamReader(inStr, "UTF-8");
54 BufferedReader buffStr = new BufferedReader(istreamReader);
55
56 String str = null;
57 while ((str = buffStr.readLine()) != null)
58 contentBuffer.append(str).append("\r\n");
59 inStr.close();
60 } catch (IOException e) {
61 e.printStackTrace();
62 contentBuffer = null;
63 System.out.println("error: " + url.toString());
64 } finally {
65 con.disconnect();
66 }
67 return contentBuffer.toString();
68 }
69
70 /**
71 * 获取js内容
72 * @param url
73 * @param encode
74 * @return
75 */
76 public static String getHtmlContentS(String url) {
77 if (!url.toLowerCase().startsWith("http://")) {
78 url = "http://" + url;
79 }
80 try {
81 URL rUrl = new URL(url);
82 return getHtmlContent(rUrl);
83 } catch (Exception e) {
84 e.printStackTrace();
85 return "网址错误!";
86 }
87
88 }
89
90 /**
91 * 获取到js内容生成一个对应js文件
92 * @param url
93 * @return
94 */
95 public static boolean mkDirFile(URL url,File file ) {
96 boolean isSuccess=false;
97 String htmlContent = "";
98 try {
99 htmlContent = getHtmlContent(url);
// 下载到一个js文件中,需要之前创建一个js文件
100 isSuccess = FileUtil.writeStringToFile(file, htmlContent);
101 } catch (Exception e) {
102 e.printStackTrace();
103 }
104 return isSuccess;
105 }
106 }