Java 利用 HttpURLConnection 读取页面 返回字节流(生成静态页面)

来源:互联网 发布:鹰眼监控软件注册码 编辑:程序博客网 时间:2024/05/17 09:29

注:若需要被静态化的 页面中 使用了 response.sendRedirect跳转,则最后静态页面为 最终跳转后的页面。

而那些 使用js 跳转的 比如 window.location.href 则 无效,直接作为js代码在生成的静态页面中,并执行。

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. //保存为文件  
[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. import java.io.*;  
  2. public class GenerateIndexPage {  
  3.       
  4.     protected static String defaultToFile = "frame/main_Null.html";  
  5.     protected static String defaultFromFile = "http://localhost:8080/stfb/frame/main_Null.jsp";  
  6.     String result = "";  
  7.       
  8.     public String genHtml(String fromFile,String toFile) throws Exception {  
  9.         if("".equals(fromFile)||fromFile==null){  
  10.             fromFile = defaultFromFile;  
  11.         }  
  12.         if("".equals(toFile)||toFile==null){  
  13.             toFile = defaultToFile;  
  14.         }  
  15.         java.net.URL url = new java.net.URL(fromFile);  
  16.         java.net.HttpURLConnection conn =(java.net.HttpURLConnection) url.openConnection();  
  17.         try{  
  18.             if (conn.getResponseCode() == 200) {  
  19.               
  20.                 java.io.InputStream is = (java.io.InputStream) conn.getContent();  
  21.                 try{  
  22.                     ConfigInfo cfn = new ConfigInfo();  
  23.                     String server_path = cfn.getValue("server_path");  
  24.                     String savePath = server_path+"\\"+toFile;  
  25.                     FileOutputStream baos = new FileOutputStream(new File(savePath));  
  26.                     int buffer = 1024;  
  27.                     byte[] b = new byte[buffer];  
  28.                     int n = 0;  
  29.                     while ((n = is.read(b, 0, buffer)) > 0) {  
  30.                         baos.write(b, 0, n);  
  31.                     }  
  32.                     //String s = new String(baos.toByteArray(), WEATHER_HTML_CHARSET);  
  33.                     is.close();  
  34.                     baos.close();  
  35.                     result = "生成成功";  
  36.                 }catch(Exception e){  
  37.                     result="写文件过程出错,取消生成。";  
  38.                 }  
  39.             }else{  
  40.                 result="获得链接过程出错,取消生成。";              
  41.             }  
  42.         }catch(Exception e){  
  43.                 e.printStackTrace();  
  44.                 result="获得内容过程出错,取消生成。";  
  45.         }  
  46.         return result;  
  47.     }  
  48.       
  49.       
  50. }  


//返回字节流

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. public String getHtml(JspWriter out) throws Exception {  
  2.     //System.setProperty("http.proxyHost", "isaserver");System.setProperty("http.proxyPort", "80");  
  3.     java.net.URL url = new java.net.URL("http://weather.china.com.cn/city/58362_zx.html");  
  4.     java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();  
  5.     if (conn.getResponseCode() == 200) {  
  6.         java.io.InputStream is = (java.io.InputStream) conn.getContent();  
  7.         java.io.ByteArrayOutputStream baos =   
  8.             new java.io.ByteArrayOutputStream();  
  9.           
  10.         int buffer = 1024;  
  11.         byte[] b = new byte[buffer];  
  12.         int n = 0;  
  13.         while ((n = is.read(b, 0, buffer)) > 0) {  
  14.             baos.write(b, 0, n);  
  15.         }  
  16.         String s = new String(baos.toByteArray(), "UTF-8");  
  17.         is.close();  
  18.         baos.close();  
  19.         return s;  
  20.     }  
  21.     return "";  
  22. }  



0 0
原创粉丝点击