|
得到响应转换成字符串用utf-8解码后,有个别汉字和标点符号中文乱码是为什么? |
|
![]() |
解码的方式有问题。
|
![]() |
是用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吗?
是的话打开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();
}
}
|
![]() |
你试把GBK改成utf-8;不行的话就在url里加encoding为utf-8
|
![]() |
代码怎么写? |
![]() 20分 |
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解码?怎么解码? |
![]() |
测试了几种解码方式,只有new String(sTotalString.getBytes(“GBK”),”UTF-8″)能解码出汉字来 |
![]() 20分 |
说一下楼主的错误吧,我说楼主的解码方式有问题,果然是。
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,所以才造成了乱码 经测试sTotalString += sCurrentLine +”\n”;我把这个换行去掉了变成sTotalString += sCurrentLine;仍然有个别乱码,乱码的地方也一样,这里可以确定乱码跟+”\n”没有关系。
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;
}
仍然有个别乱码,乱码的地方也一样。 |
![]() |
你这样读取的方式当然会出错。。。 |
![]() |
找了俩天,终于解决了。感谢10楼!
|

