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
查看全文
相关阅读:
SQL 查询第n条到第m条的数据
Linq 中查询一个表中指定的字段
归并排序与逆序对
补码拾遗
堆排序
It is time to cut the Gordian Knot!
蛋疼
[引]Microsoft Visual Studio .NET 2005 预发行版
关于VS2005中自动生成TableAdapter的事务处理
关于释放ASPNET进程的内存占用问题.
原文地址:https://www.cnblogs.com/zdcaolei/p/2122939.html
最新文章
2012/8/4 struts2中action名称的搜索顺序
2012/8/4 指定需要struts2处理的请求后缀
2012/8/4 struts2中action配置的各项默认值
ADB server didn't ACK
android 用命令初始化数据
隐藏不想让git status显示出来的文件
android devices offline
获取SD卡可用空间和文件的大小
反编译apk
两种在C#中查看硬盘ID的方法(转载)
热门文章
如何理解反射?
从数据库中读取出数据,然后用XML输出
js 获取地址栏中的参数
C# 发送邮件代码
限制文本框输入内容
js 只允许文本框输入数字
js 倒计时代码
ASP.NET 基本的文件上传代码
【转】Linq使用Group By分析
【转】LINQ to SQL语句(6)之Group By/Having
Copyright © 2011-2022 走看看