页面静态话,释放http连接异常

页面静态化时,执行getMethod.releaseConnection();时,释放http连接异常,异常信息:

Exception in thread "main" java.lang.NullPointerException
at com.test.HtmlGenerator.createHtmlPage(HtmlGenerator.java:94)
at com.test.HtmlGenerator.main(HtmlGenerator.java:121)

解决方案:

1.确认get请求的url没有问题(比如没有被重定向),可以通过浏览器访问

2.需要的jar包:commons-httpclient-3.1.jar、commons-codec-1.10.jar,缺少commons-codec-1.10.jar包,也会报错

java代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class HtmlGenerator
{
	HttpClient httpClient = null; // HttpClient实例
	GetMethod getMethod = null; // GetMethod实例
	//	PostMethod postMethod = null;
	BufferedWriter fw = null;
	String page = null;
	String webappname = null;
	BufferedReader br = null;
	InputStream in = null;
	StringBuffer sb = null;
	String line = null;

	// 构造方法
	public HtmlGenerator(String webappname)
	{
		this.webappname = webappname;

	}

	/** 根据模版及参数产生静态页面 */
	public boolean createHtmlPage(String url, String htmlFileName)
	{
		boolean status = false;
		int statusCode = 0;
		try
		{
			// 创建一个HttpClient实例充当模拟浏览器
			httpClient = new HttpClient();
			// 设置httpclient读取内容时使用的字符集
			httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
			// 创建GET方法的实例
			//			postMethod = new PostMethod(url);
			getMethod = new GetMethod(url);
			// 使用系统提供的默认的恢复策略,在发生异常时候将自动重试3次
			//			postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
			getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
			// 设置Get方法提交参数时使用的字符集,以支持中文参数的正常传递
			//			postMethod.addRequestHeader("Content-Type", "text/html;charset=UTF-8");
			getMethod.addRequestHeader("Content-Type", "text/html;charset=UTF-8");
			// 执行Get方法并取得返回状态码,200表示正常,其它代码为异常
			statusCode = httpClient.executeMethod(getMethod);
			if(statusCode != 200)
			{
				System.out.println("静态页面引擎在解析" + url + "产生静态页面" + htmlFileName + "时出错!");
				// logger.fatal();
			}
			else
			{
				// 读取解析结果
				sb = new StringBuffer();
				in = getMethod.getResponseBodyAsStream();
				// br = new BufferedReader(new
				// InputStreamReader(in));//此方法默认会乱码,经过长时期的摸索,下面的方法才可以
				br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
				while((line = br.readLine()) != null)
				{
					sb.append(line + "\n");
				}
				if(br != null)
					br.close();
				page = sb.toString();
				// 将页面中的相对路径替换成绝对路径,以确保页面资源正常访问
				page = formatPage(page);
				// 将解析结果写入指定的静态HTML文件中,实现静态HTML生成
				writeHtml(htmlFileName, page);
				status = true;
			}
		}
		catch(Exception ex)
		{
			System.out.println("静态页面引擎在解析" + url + "产生静态页面" + htmlFileName + "时出错:" + ex.getMessage());
			// logger.fatal();
		}
		finally
		{
			// 释放http连接
			getMethod.releaseConnection();
		}
		return status;
	}

	// 将解析结果写入指定的静态HTML文件中
	private synchronized void writeHtml(String htmlFileName, String content) throws Exception
	{
		fw = new BufferedWriter(new FileWriter(htmlFileName));
		OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(htmlFileName), "UTF-8");
		fw.write(page);
		if(fw != null)
			fw.close();
	}

	// 将页面中的相对路径替换成绝对路径,以确保页面资源正常访问
	private String formatPage(String page)
	{
		page = page.replaceAll("\\.\\./\\.\\./\\.\\./", webappname + "/");
		page = page.replaceAll("\\.\\./\\.\\./", webappname + "/");
		page = page.replaceAll("\\.\\./", webappname + "/");
		return page;
	}
	
	//测试方法  
    public static void main(String[] args){  
        HtmlGenerator h = new HtmlGenerator("webappname");  
        h.createHtmlPage("http://127.0.0.1:8080/test1/index.jsp","c:/a.html");  
        System.out.println("静态页面已经生成到c:/a.html");  
          
    }
}

commons-httpclient-3.1.jar下载地址: http://download.csdn.net/download/kkkder/9986550

commons-codec-1.10.jar下载地址:http://download.csdn.net/download/kkkder/9986553

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值