解决方案 »
- pstmt=conn.prepareStatement(sql)空指针异常,求大神支招
- Spring Security 3 的错误。。大家帮忙看看
- jdk删错不了,我把文件给删了,但是重装不上了。怎么能重装呢?
- 请教关于如何学习j2ee的问题
- 谁有Java网站的网址?并且该网站是用SSH框架搭建的或其中的一种!
- (急)该做什么项目?
- 请问 struts 中怎样使用javascript?
- 求助各位,帮助菜鸟~
- jsp页面之间传递对象的方法
- java 中方法重写中的坑
- 通过wsdl生成webservice客户端问题
- 关于org.apache.commons.net.ftp.FTPClient的retrieveFile函数
楼上的方法可以,但是处理UTF-8编码的网站就不行了
StringBuffer sb = new StringBuffer();
URL url=new URL("http://www.qq.com");
InputStreamReader isr=new InputStreamReader(url.openStream());
BufferedReader br=new BufferedReader(isr);
String str;
while((str=br.readLine())!=null)
{
sb.append(str);
//sb.append(str);
//System.out.println(new String(str.getBytes("gb2312"),"utf-8"));
}
System.out.println(sb.substring(sb.indexOf("<title>"),sb.indexOf("</title>")));
br.close();
isr.close();这样就可以打印出“腾讯首页”。不用指定编码方式。
URL url = new URL("http://www.qq.com");
URLConnection connection = url.openConnection();
Map<String,List<String>> headers = connection.getHeaderFields();
String ctype = headers.get("Content-Type").get(0); // text/html; charset=GB2312
String charset = ctype.substring(ctype.lastIndexOf("=") +1 ); // GB2312InputStreamReader isr = new InputStreamReader(url.openStream(),charset);
关键是reader要设编码呢。Content-Type text/html; charset=utf-8