ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-02-04 11:49:50  阅读:350  来源: 互联网

标签:java String Java io baos new net HttpURLConnection 页面


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

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

//保存为文件
import java.io.*;
public class GenerateIndexPage {
	
	protected static String defaultToFile = "frame/main_Null.html";
	protected static String defaultFromFile = "http://localhost:8080/stfb/frame/main_Null.jsp";
    String result = "";
	
	public String genHtml(String fromFile,String toFile) throws Exception {
		if("".equals(fromFile)||fromFile==null){
			fromFile = defaultFromFile;
		}
		if("".equals(toFile)||toFile==null){
			toFile = defaultToFile;
		}
		java.net.URL url = new java.net.URL(fromFile);
		java.net.HttpURLConnection conn =(java.net.HttpURLConnection) url.openConnection();
		try{
			if (conn.getResponseCode() == 200) {
			
				java.io.InputStream is = (java.io.InputStream) conn.getContent();
				try{
					ConfigInfo cfn = new ConfigInfo();
					String server_path = cfn.getValue("server_path");
					String savePath = server_path+"\\"+toFile;
					FileOutputStream baos = new FileOutputStream(new File(savePath));
					int buffer = 1024;
					byte[] b = new byte[buffer];
					int n = 0;
					while ((n = is.read(b, 0, buffer)) > 0) {
						baos.write(b, 0, n);
					}
					//String s = new String(baos.toByteArray(), WEATHER_HTML_CHARSET);
					is.close();
					baos.close();
					result = "生成成功";
				}catch(Exception e){
					result="写文件过程出错,取消生成。";
				}
			}else{
				result="获得链接过程出错,取消生成。";			
			}
		}catch(Exception e){
				e.printStackTrace();
				result="获得内容过程出错,取消生成。";
		}
		return result;
	}
	
	
}

 

//返回字节流

 

public String getHtml(JspWriter out) throws Exception {
	//System.setProperty("http.proxyHost", "isaserver");System.setProperty("http.proxyPort", "80");
	java.net.URL url = new java.net.URL("http://weather.china.com.cn/city/58362_zx.html");
	java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
	if (conn.getResponseCode() == 200) {
		java.io.InputStream is = (java.io.InputStream) conn.getContent();
		java.io.ByteArrayOutputStream baos = 
			new java.io.ByteArrayOutputStream();
		
		int buffer = 1024;
		byte[] b = new byte[buffer];
		int n = 0;
		while ((n = is.read(b, 0, buffer)) > 0) {
			baos.write(b, 0, n);
		}
		String s = new String(baos.toByteArray(), "UTF-8");
		is.close();
		baos.close();
		return s;
	}
	return "";
}


 

 

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

标签:java,String,Java,io,baos,new,net,HttpURLConnection,页面
来源: https://www.cnblogs.com/skinchqqhah/p/10351580.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有