67,512
社区成员
发帖
与我相关
我的任务
分享
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"没有关系。
自以为是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
我曾经和你犯过一样的错误。。。
/**
* 以指定的格式来读取输入流
*/
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();
}
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();
}
}
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"没有关系。
自以为是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之间互有关系吧
<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"/>