使用java得到網頁編碼格式

來源:互聯網
上載者:User
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;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.