package com.tag;import java.net.MalformedURLException;import java.net.URL;import org.apache.commons.httpclient.Header;import org.apache.commons.httpclient.HeaderElement;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.NameValuePair;import org.apache.commons.httpclient.methods.GetMethod;import toptrack.tools.JQueryBase;/** * 得到網頁編碼格式 * @author dl */public class JHtmlUpdateCheck {/**常值內容編碼識別類*/private static cpdetector.io.CodepageDetectorProxy detector = cpdetector.io.CodepageDetectorProxy.getInstance(); static {detector.add(new cpdetector.io.HTMLCodepageDetector(false)); detector.add(cpdetector.io.JChardetFacade.getInstance());}/** *<br>方法說明:得到網頁編碼格式 *<br>輸入參數:strUrl 網頁連結; timeout 逾時設定 *<br>傳回型別:網頁編碼 */public static String getEncoding(String strUrl, int timeout) {String strEncoding = null;HttpClient client = new HttpClient();client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout); GetMethod method = new GetMethod(strUrl); method.setFollowRedirects( true ); int statusCode; try {statusCode = client.executeMethod(method);if( statusCode != -1) {//從http頭得到網頁編碼 strEncoding = getContentCharSet(method.getResponseHeader("Content-Type")); if (strEncoding != null) { method.releaseConnection(); return strEncoding; }//通過解析meta得到網頁編碼 String strHtml = method.getResponseBodyAsString().toLowerCase(); StringBuffer strBuffer = new StringBuffer(); int pos = JQueryBase.getTagText(strHtml, "<meta", ">", strBuffer, false, 0); while (strBuffer.length() > 0) { StringBuffer strEncodingBuffer = new StringBuffer(); JQueryBase.getTagText(strBuffer.toString(), "charset=", "/"", strEncodingBuffer, 0); if (strEncodingBuffer.length() > 0) { strEncoding = strEncodingBuffer.toString(); method.releaseConnection(); return strEncoding; } strBuffer = new StringBuffer(); pos = JQueryBase.getTagText(strHtml, "<meta", ">", strBuffer, false, pos); } //分析位元組得到網頁編碼 strEncoding = getFileEncoding(strUrl, timeout); //設定預設網頁字元編碼 if (strEncoding == null) strEncoding = "GBK"; }method.releaseConnection();} catch (Exception e) {// TODO Auto-generated catch blockSystem.out.println(e.getClass() + "對" + strUrl + "提取網頁編碼資訊出錯");return null;}return strEncoding;}/** *<br>方法說明:通過http頭得到網頁編碼資訊 *<br>輸入參數:contentheade rhttp頭 *<br>傳回型別:網頁編碼 */protected static String getContentCharSet(Header contentheader) { String charset = null; if (contentheader != null) { HeaderElement values[] = contentheader.getElements(); if (values.length == 1) { NameValuePair param = values[0].getParameterByName("charset"); if (param != null) { charset = param.getValue(); } } } return charset; } /** *<br>方法說明:通過網頁內容識別網頁編碼 *<br>輸入參數:strUrl 網頁連結; timeout 逾時設定 *<br>傳回型別:網頁編碼 */public static String getFileEncoding(String strUrl, int timeout) {java.nio.charset.Charset charset = null; URL f;try {f = new URL(strUrl);} catch (MalformedURLException e) {// TODO Auto-generated catch blockSystem.out.println(e.getClass() + strUrl + "無效");return null;}try { charset = detector.detectCodepage(f); } catch (Exception e) {System.out.println(e.getClass() + "分析" + strUrl + "的編碼失敗");} if (charset != null)return charset.name();return null;}}