URLConnection请求页面个别汉字和标点符号中文乱码是为什么?

allenlee 2013-09-26 03:41:44
得到响应转换成字符串用utf-8解码后,有个别汉字和标点符号中文乱码是为什么?
例如:大型汽车
使馆汽车
领馆汽车
境外汽车
外籍汽车
两??三轮摩托??/option>
轻便摩托??/option>
使馆摩托??/option>
领馆摩托??/option>
...全文
2313 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
kkkllk 2014-07-09
  • 打赏
  • 举报
回复
找了俩天,终于解决了。感谢10楼!
失落夏天 2013-10-01
  • 打赏
  • 举报
回复
引用 11 楼 FastThinking 的回复:
[quote=引用 10 楼 AA5279AA 的回复:] 说一下楼主的错误吧,我说楼主的解码方式有问题,果然是。 sTotalString += sCurrentLine +"\n"; 你有这么一句是么。 你这时候其实sTotalString是乱码(相对于GBK,而相对于UTF-8是正常的),而你又添加了一个GBK的换行,等到换成UTF-8格式的时候,自然这个换行就乱码了。 你用下面的这种方式来读:

/**
	 * 以指定的格式来读取输入流
	 */
	public static String readStrByCode(InputStream is,String code){
		StringBuilder builder=new StringBuilder();
		BufferedReader reader=null;
		
		try {
			 reader = new BufferedReader(new InputStreamReader(is,code));
			 String line;
			 while((line=reader.readLine())!=null){
				 builder.append(line+"\n");
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				reader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return builder.toString();
	}
你好,用你的方法解决了问题。

 String sTotalString = readStrByCode(l_urlStream,"UTF-8");
        System.out.println(sTotalString);//显示正常
测试

InputStreamReader isr = new InputStreamReader(is);
System.out.println("new InputStreamReader(is).getEncoding() is "+isr.getEncoding());
这里读取时不指定编码,默认用了GBK编码,而服务器响应页面编码是UTF-8,所以才造成了乱码 打印输出
引用
new InputStreamReader(is).getEncoding() is GBK
经测试sTotalString += sCurrentLine +"\n";我把这个换行去掉了变成sTotalString += sCurrentLine;仍然有个别乱码,乱码的地方也一样,这里可以确定乱码跟+"\n"没有关系。 自以为是readLine换行造成的,于是变换一下换行的位置,看看乱码的地方是不是会变, 改代码如下:

 InputStreamReader isr = new InputStreamReader(is);
			 System.out.println("new InputStreamReader(is).getEncoding() is "+isr.getEncoding());
			 reader = new BufferedReader(isr);
			 char [] cbuf = new char[1024];
			 while( reader.read(cbuf)!=-1){
				 String str = new String(cbuf);
				 sTotalString += str;
			 }
仍然有个别乱码,乱码的地方也一样。 我的问题应该是UTF-GBK-UTF出现的问题,看了一下网上有java gbk转utf-8乱码问题我想应该跟UTF和GBK之间互有关系吧 [/quote] 你这样读取的方式当然会出错。。。 至于原因。。你看一下 http://bbs.csdn.net/topics/390578551 我曾经和你犯过一样的错误。。。
失落夏天 2013-09-27
  • 打赏
  • 举报
回复
说一下楼主的错误吧,我说楼主的解码方式有问题,果然是。 sTotalString += sCurrentLine +"\n"; 你有这么一句是么。 你这时候其实sTotalString是乱码(相对于GBK,而相对于UTF-8是正常的),而你又添加了一个GBK的换行,等到换成UTF-8格式的时候,自然这个换行就乱码了。 你用下面的这种方式来读:

/**
	 * 以指定的格式来读取输入流
	 */
	public static String readStrByCode(InputStream is,String code){
		StringBuilder builder=new StringBuilder();
		BufferedReader reader=null;
		
		try {
			 reader = new BufferedReader(new InputStreamReader(is,code));
			 String line;
			 while((line=reader.readLine())!=null){
				 builder.append(line+"\n");
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				reader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return builder.toString();
	}
allenlee 2013-09-27
  • 打赏
  • 举报
回复
引用 1 楼 AA5279AA 的回复:
解码的方式有问题。
测试了几种解码方式,只有new String(sTotalString.getBytes("GBK"),"UTF-8")能解码出汉字来
allenlee 2013-09-27
  • 打赏
  • 举报
回复
引用 7 楼 u010684923 的回复:
http://jjzx.lywww.com/index.php?m=Index&a=jdwfcx&useUnicode=true&characterEncoding=utf-8 ; 你的问题好像不在这里,你输出时带了编码GBK,而读取(InputStream)的时候没有解码.需要decode.
试了一下还是不行,new String(sTotalString.getBytes("GBK"),"UTF-8")不算是解码吗?您的意思是对InputStream解码?怎么解码?
闲去野鹤 2013-09-27
  • 打赏
  • 举报
回复
http://jjzx.lywww.com/index.php?m=Index&a=jdwfcx&useUnicode=true&characterEncoding=utf-8 ; 你的问题好像不在这里,你输出时带了编码GBK,而读取(InputStream)的时候没有解码.需要decode.
allenlee 2013-09-27
  • 打赏
  • 举报
回复
引用 5 楼 u010684923 的回复:
你试把GBK改成utf-8;不行的话就在url里加encoding为utf-8
代码怎么写?
闲去野鹤 2013-09-27
  • 打赏
  • 举报
回复
你试把GBK改成utf-8;不行的话就在url里加encoding为utf-8
allenlee 2013-09-27
  • 打赏
  • 举报
回复
引用 3 楼 u010684923 的回复:
是用tomcat吗? 是的话打开sever.xml 找到 <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/> 改为: <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
不是tomcat,是模拟请求,代码如下
package com.test;

import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
import java.io.OutputStreamWriter;  
import java.net.URL;  
import java.net.URLConnection;  
  
public class TestPost {  
  
    public static void testPost() throws IOException {  
  
        /** 
         * 首先要和URL下的URLConnection对话。 URLConnection可以很容易的从URL得到。比如: // Using 
         *  java.net.URL and //java.net.URLConnection 
         */  
        URL url = new URL("http://jjzx.lywww.com/index.php?m=Index&a=jdwfcx");  
        URLConnection connection = url.openConnection();  
          
        /** 
         * 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。 
         * 通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做: 
         */  
        connection.setDoOutput(true);  
        /** 
         * 最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如: ... 
         */  
        OutputStreamWriter out = new OutputStreamWriter(connection  
                .getOutputStream(), "GBK");  //8859_1
        out.write("hpzl=02&fzjq=Q&hphm=55555&clsbdh=1111"); //post的关键所在!  
        // remember to clean up  
        out.flush();  
        out.close();  
        
        /** 
         * 这样就可以发送一个看起来象这样的POST:  
         * POST /jobsearch/jobsearch.cgi HTTP 1.0 ACCEPT: 
         * text/plain Content-type: application/x-www-form-urlencoded 
         * Content-length: 99 username=bob password=someword 
         */  
        // 一旦发送成功,用以下方法就可以得到服务器的回应:  
        String sCurrentLine;  
        String sTotalString;  
        sCurrentLine = "";  
        sTotalString = "";  
        InputStream l_urlStream = connection.getInputStream();  
       
        // 传说中的三层包装阿!  
        BufferedReader l_reader = new BufferedReader(new InputStreamReader(  
                l_urlStream));  
        while ((sCurrentLine = l_reader.readLine()) != null) {  
            sTotalString += sCurrentLine +"\n";  
  
        }  
        String encode = connection.getContentEncoding();
        System.out.println("the ContentEncoding is "+encode);
        String contentType = connection.getContentType();
        System.out.println("the contentType is "+contentType);
        System.out.println(new String(sTotalString.getBytes("GBK"),"UTF-8"));  
    }  
  
    public static void main(String[] args) throws IOException {  
  
        testPost();  
  
    }  
  
}  
allenlee 2013-09-27
  • 打赏
  • 举报
回复
引用 10 楼 AA5279AA 的回复:
说一下楼主的错误吧,我说楼主的解码方式有问题,果然是。 sTotalString += sCurrentLine +"\n"; 你有这么一句是么。 你这时候其实sTotalString是乱码(相对于GBK,而相对于UTF-8是正常的),而你又添加了一个GBK的换行,等到换成UTF-8格式的时候,自然这个换行就乱码了。 你用下面的这种方式来读:

/**
	 * 以指定的格式来读取输入流
	 */
	public static String readStrByCode(InputStream is,String code){
		StringBuilder builder=new StringBuilder();
		BufferedReader reader=null;
		
		try {
			 reader = new BufferedReader(new InputStreamReader(is,code));
			 String line;
			 while((line=reader.readLine())!=null){
				 builder.append(line+"\n");
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				reader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return builder.toString();
	}
你好,用你的方法解决了问题。

 String sTotalString = readStrByCode(l_urlStream,"UTF-8");
        System.out.println(sTotalString);//显示正常
测试

InputStreamReader isr = new InputStreamReader(is);
System.out.println("new InputStreamReader(is).getEncoding() is "+isr.getEncoding());
这里读取时不指定编码,默认用了GBK编码,而服务器响应页面编码是UTF-8,所以才造成了乱码 打印输出
引用
new InputStreamReader(is).getEncoding() is GBK
经测试sTotalString += sCurrentLine +"\n";我把这个换行去掉了变成sTotalString += sCurrentLine;仍然有个别乱码,乱码的地方也一样,这里可以确定乱码跟+"\n"没有关系。 自以为是readLine换行造成的,于是变换一下换行的位置,看看乱码的地方是不是会变, 改代码如下:

 InputStreamReader isr = new InputStreamReader(is);
			 System.out.println("new InputStreamReader(is).getEncoding() is "+isr.getEncoding());
			 reader = new BufferedReader(isr);
			 char [] cbuf = new char[1024];
			 while( reader.read(cbuf)!=-1){
				 String str = new String(cbuf);
				 sTotalString += str;
			 }
仍然有个别乱码,乱码的地方也一样。 我的问题应该是UTF-GBK-UTF出现的问题,看了一下网上有java gbk转utf-8乱码问题我想应该跟UTF和GBK之间互有关系吧
闲去野鹤 2013-09-26
  • 打赏
  • 举报
回复
是用tomcat吗? 是的话打开sever.xml 找到 <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/> 改为: <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
闲去野鹤 2013-09-26
  • 打赏
  • 举报
回复
是用tomcat吗? 是的话打开sever.xml 找到

 <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" />
改为
 <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
失落夏天 2013-09-26
  • 打赏
  • 举报
回复
解码的方式有问题。

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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