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
查看全文
相关阅读:
Mysql自定义函数总结
MySQL的基本函数
Mysql存储过程总结
Mysql触发器总结
Mysql索引总结(二)
Mysql索引总结(一)
Mysql游标使用
别人的博客,留待后看
mysql外键约束总结
mysql视图总结
原文地址:https://www.cnblogs.com/zdcaolei/p/2122939.html
最新文章
命令序列 ; & && ||
UOJ33 [UR #2] 树上GCD 【点分治】【容斥原理】【分块】
LOJ6387 [THUPC2018] 绿绿与串串 【manacher】
LOJ2540 [PKUWC2018] 随机算法 【状压DP】
BZOJ5337 [TJOI2018] 碱基序列 【哈希】【动态规划】
BZOJ5334 [TJOI2018] 数学计算 【线段树分治】
BZOJ5338 [TJOI2018] Xor 【可持久化Trie树】【dfs序】
ZOJ1363 Chocolate 【生成函数】 【泰勒展开】
BZOJ4870 [六省联考2017] 组合数问题 【快速幂】
[ctsc2018] 混合果汁 【可持久化线段树】【二分答案】
热门文章
HDU4623 CRIME 【状压DP】【同类项合并】
web项目的一些常用设置
javaEE
JDBC
java集合
反射
XML
Java之网络编程
java 之多线程
IO流
Copyright © 2011-2022 走看看