本帖最后由 leecho571 于 2011-08-17 19:56:58 编辑

解决方案 »

  1.   

    sb.append(new String(str.getBytes("gb2312"), "gb2312"));
      

  2.   

               InputStreamReader isr=new InputStreamReader(url.openStream(),Charset.forName("GBK"));
      

  3.   


      楼上的方法可以,但是处理UTF-8编码的网站就不行了
      

  4.   


    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();这样就可以打印出“腾讯首页”。不用指定编码方式。
      

  5.   

    一般情况下用utf—8都是可以的
      

  6.   


    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);
      

  7.   


    关键是reader要设编码呢。Content-Type text/html; charset=utf-8