指定了字符集,为什么还是乱码

mycolors 2016-05-19 02:48:41
要把这个页面的源代码抓下来,指定了字符集,什么还是乱码

import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class testa {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

try{
StringBuffer sb = new StringBuffer();
URL url=new URL("http://odds.500.com/fenxi/yazhi-572249.shtml");
InputStreamReader isr=new InputStreamReader(url.openStream(),"GB2312");
BufferedReader br=new BufferedReader(isr);

String str;
while((str=br.readLine())!=null)
{
sb.append(str);
System.out.println(str);



}

br.close();
isr.close();
}catch (MalformedURLException e) {
System.out.println("Address Error!");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}
...全文
209 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
imfang 2016-05-19
  • 打赏
  • 举报
回复
引用 2 楼 mycolors 的回复:
怎么看出来是GZIP压缩的。求教
1楼的代码已经有啦!

URL url=new URL("http://odds.500.com/fenxi/yazhi-572249.shtml");
URLConnection connection = url.openConnection();
Map<String,List<String>> headers = connection.getHeaderFields();
Set<String> s = headers.keySet();
Iterator<String> i = s.iterator();
while (i.hasNext()){
    String key = i.next();
    System.out.println(key+":"+" -- "+headers.get(key));
}
X-Via: -- [1.1 12024079D:8107 (Cdn Cache Server V2.0), 1.1 yidong204:5 (Cdn Cache Server V2.0)] null: -- [HTTP/1.1 200 OK] Transfer-Encoding: -- [chunked] Date: -- [Thu, 19 May 2016 08:37:50 GMT] P3P: -- [CP="NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM"] Content-Encoding: -- [gzip] Connection: -- [keep-alive] Content-Type: -- [text/html] Server: -- [nginx]
anakin_feng 2016-05-19
  • 打赏
  • 举报
回复
anakin_feng 2016-05-19
  • 打赏
  • 举报
回复
本来也觉得很奇怪为什么英文都乱码了,然后查看了头信息,看到了GZIP,觉得有点奇怪,就去百度了下
mycolors 2016-05-19
  • 打赏
  • 举报
回复
明白了,在request header ,谢谢!
mycolors 2016-05-19
  • 打赏
  • 举报
回复
怎么看出来是GZIP压缩的。求教
anakin_feng 2016-05-19
  • 打赏
  • 举报
回复
通过我对这个网站分析,发现它用的是gzip压缩,所以这里不能直接读
import java.io.*;
import java.net.*;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.GZIPInputStream;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        try{
            StringBuffer sb = new StringBuffer();
            URL url=new URL("http://odds.500.com/fenxi/yazhi-572249.shtml");
            URLConnection connection = url.openConnection();
            Map<String,List<String>> headers = connection.getHeaderFields();
            Set s = headers.keySet();
            Iterator<String> i = s.iterator();
            while (i.hasNext()){
                String key = i.next();
                System.out.println(key+":"+" -- "+headers.get(key));
            }
            GZIPInputStream g = new GZIPInputStream(connection.getInputStream());

            byte b[] = new byte[1024];
            int a = -1;
            while ((a=g.read(b))!=-1){
                sb.append(new String(b,0,a,"gb2312"));
            }
            System.out.println(sb);
            g.close();
        }catch (MalformedURLException e) {
            System.out.println("Address Error!");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧