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
查看全文
相关阅读:
【成功案例】智能企业的可持续转型
【成功案例】三菱重工
【成功案例】都乐(Dole)
【成功案例】佳能
【成功案例】DHL将美洲汇聚在一起
【成功案例】Bantotal
【成功案例】得益于GeneXus,毕马威墨西哥在员工培训方面节省了95%的费用
GeneXus低码工具为美国最大的大学图书销售商之一解决了五个问题
双均线系统
价量时空交易系统
原文地址:https://www.cnblogs.com/zdcaolei/p/2122939.html
最新文章
WPF之事件
WPF之属性
WPF之数据绑定
WPF之控件布局
WPF之x命名空间
WPF之XAML语法
解决npm很慢
Ubuntu装node.js和npm
另辟蹊径:聊聊DFL3小样本深度学习(一)
LAXCUS集群操作系统5.2标准版正式发布,欢迎下载使用!
热门文章
conda / cuda / screen 常用命令总结
C#设计模式02——原型模式的写法
图文ASP.Net MVC Razor页面中HtmlHelper帮助程序的写法
ASP.Net Core 5.0 MVC AppSettings配置文件读取,Startup 类中ConfigureServices 方法、Configure 方法的使用
C#设计模式01——单例模式的三种写法
扒一扒ProcessOn 新功能——一键编号、图形组合、左侧导航、画布水印、表格组件
ASP.NET Core 5.0 MVC 视图组件的用法
ASP.NET Core 5.0 MVC 页面标记帮助程序的使用
ASP.NET Core 5.0 MVC中的视图分类及使用——布局视图、启动视图、导入视图、详细视图、分部视图
扒一扒爱奇艺影视剧新功能——AI识别、GIF动画生成、画面截图涂鸦
Copyright © 2011-2022 走看看