频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

使用java得到网页编码格式

来源:CSDN 作者:佚名 出处:巧巧读书 2008-06-19 进入讨论组
下一页 1 2 

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 block

System.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;

}

更多文章 更多内容请看Java环境安装配置Java编程开发手册Java网络及通讯编程专题,或进入讨论组讨论。
下一页 1 2 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章