我试着从中国的网站中阅读HTML并得到它们的<title>值。所有使用UTF-8编码的网站都能正常工作,但对于GB2312网站则不适用(例如,m.39.net,它显示的是39������_�й����ȵĽ����Ż���վ而不是39健康网_中国领先的健康门户网站)。
下面是我用来实现这一目标的代码:
URL url = new URL(urlstr);
URLConnection connection = url.openConnection();
inputStream = connection.getInputStream();
String content = IOUtils.toString(inputStream);发布于 2016-01-12 03:14:59
String content = IOUtils.toString(inputStream, "GB2312");可能会提供帮助。
如果您想检测网页的字符集,据我所知,有三种方法:
connection.getContentEncoding()获取header中描述的字符集;<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">或<meta charset="UTF-8"> (必须先下载HTML内容,然后读取几行);发布于 2016-01-12 03:23:36
你见过http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/IOUtils.html吗?
toString(byte[] input, String encoding)https://stackoverflow.com/questions/34730163
复制相似问题