Java将网页上的js下载下来。

  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 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值