zoukankan
html css js c++ java
使用java得到网页编码格式
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; }
欢迎转载,转载请注明出处。本文出自:
http://www.cnblogs.com/zdcaolei
0
查看全文
相关阅读:
团队项目冲刺七
团队项目冲刺六
团队项目冲刺5
团队项目冲刺4
团队项目测试计划
团队项目冲刺第三天进度和遇到问题
团队项目冲刺第二天进度和问题
博客园用户体验
团队项目冲刺第一天进度和问题
团队项目风险
原文地址:https://www.cnblogs.com/zdcaolei/p/2122939.html
最新文章
Mysql的InnoDB引擎-4.表(1)
Mysql的InnoDB引擎-4.InnoDB特性
Mysql的InnoDB引擎-3.CheckPoint技术、Master Thread
Mysql的InnoDB引擎-2.InnoDB介绍
Mysql的InnoDB引擎-1.引擎介绍
BIO、NIO、AIO
TCP包问题
Socke基础
JAVA虚拟机JVM-9.容器刨析HashMap
JAVA虚拟机JVM-8.容器问题刨析List
热门文章
springmvc demo
maven-tomcat7;IOC;AOP;数据库远程连接
mybatis 视频总结
反射机制,jvm,class类型
c3p0;jdbc;dbcp;mybatis;ubutils;
c3p0;maven;model;
spring;maven;github;ssm;分层;timestamp;mvn;
maven;spring;pom
maven;cargo;仓库
maven;tomcat配置
Copyright © 2011-2022 走看看