模拟POST 返回值为乱码

liuhuan851002 2010-04-02 10:42:33
环境 : mysql 4.0.12数据库 + myeclipse7.0 + jdk 1.6U17

问题 : 我从数据库读出来的数据是乱码

如下:



过程:

1. 我用java编写的代码.

2. 这个版本的mysql 默认的字符集是 ISO8859-1

3. 我获取的方式是 通过 应用程序 模拟 POST 方法来获取的数据库的值.

代码如下:

// 调用POST方法

public static String connect(String sqltype,String sql,String gGebug)
{
HttpClient client = new HttpClient();
HttpMethod httpPost =getPostMethod(sqltype, sql,gGebug);
//HttpMethod httpPost =getGetMethod();
client.getHostConfiguration().setHost("192.168.1.6",88,"http");
try {
int status = client.executeMethod(httpPost);
if(status==200)
{
InputStream its=httpPost.getResponseBodyAsStream();
BufferedReader bf = new BufferedReader(new InputStreamReader((its)));
String line="";
String[] val=null;
while((line =bf.readLine())!= null)
{
// select sql return field value.
if("S".equals(sqltype))
{
if(line.contains("::"))
{
if(! line.contains("$$"))
{
String temp =line.replace("::", "!");
val = temp.split("!");
return val[1];
}
else
{
// ...
}
}
}

// return value : update num
if("U".equals(sqltype))
{
if(line.contains("$$"))
{
String temp =line.replace("$$", "!");
val = temp.split("!");
return val[1];
}
//return val;

}
// insert sql return success result.
if("I".equals(sqltype))
{
if(line.contains("$$"))
{
String temp =line.replace("$$", "!");
val = temp.split("!");
return val[0];
}
}
//System.out.println(EncodeTransfer.gb2iso(line));
}
}
else
{
System.out.println("fail return value "+status);
}
return null;
} catch (HttpException e) {
e.printStackTrace(System.err);
} catch (IOException e) {
e.printStackTrace(System.err);
}finally{
httpPost.releaseConnection();
}
return null;
}

//getPostMethod 模拟post的方法

private static HttpMethod getPostMethod(String exetype,String exesql,String debug)
{
PostMethod post = new PostMethod("/bug/include/DB_API.php");
NameValuePair sqltype = new NameValuePair("gSqlType",exetype);
NameValuePair sql = new NameValuePair("gSql",exesql);
NameValuePair gdebug = new NameValuePair("gDebug",debug);
post.setRequestBody(new NameValuePair[] {sqltype,sql,gdebug});

return post;
}

// 主方法

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

String sql = "select bba02 from bba where bba01=107";

try {

// 我获得的从数据库读出来的是UTF-8 但是这样转换还是出来的不正常 如:锟脚讹拷锟侥硷拷锟斤拷锟斤拷系统
System.out.println(new String(connect("S",s,"1").getBytes("UTF-8"),"GBK"));
// 这是没有转换 的结果 如: �Ŷ��ļ�����ϵͳ

System.out.println(connect("S",s,"1"));


} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

...全文
589 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
BigBird2012 2010-04-03
  • 打赏
  • 举报
回复
如果再不行就在MySql的连接字符串后面指定一下字符集的编码格式,或者写个过滤器也可以啊
injuer 2010-04-03
  • 打赏
  • 举报
回复
设置下POST方法的HEAD,设置为“UTF-8”或是“GBK2312”
liuhuan851002 2010-04-03
  • 打赏
  • 举报
回复
问题目前解决了:
我通过 抓包 工具来查看了下, 我是从数据库 ISO8859-1 --> 获取时就GBK了,
那么我用流读取的时候就是GBK。 而输入字符流 有默认的字符编码(UTF8)的。
所以通过输入流来读取GBK值时 就出现部分字符转换有问题。

解决办法 : 将输入字符流 的默认编码 改为 gbk 就可以了。

谢谢各位的关注和帮忙 。
liuhuan851002 2010-04-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 injuer 的回复:]
设置下POST方法的HEAD,设置为“UTF-8”或是“GBK2312”
[/Quote]

这个可以设置吗
whd3331933 2010-04-03
  • 打赏
  • 举报
回复
有待学习 出点声 继续观看
BearKin 2010-04-03
  • 打赏
  • 举报
回复
确认乱码是哪一步骤的 然后再进行处理 转码是不需要的
LOOK
http://topic.csdn.net/u/20090910/18/df5a7313-d27a-49ed-8274-2a11b629371b.html
coolbamboo2008 2010-04-03
  • 打赏
  • 举报
回复
建议,从数据库、JSP页面、IDE统一都用utf-8,这样省了考虑编码问题
liuhuan851002 2010-04-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 injuer 的回复:]
new String(connect("S",s,"1").getBytes("ISO-8859-1
"));
[/Quote]

//这样还是乱码
System.out.println(new String(connect("S",s,"1").getBytes("ISO8859-1")));
出来的也是 : ????????????
injuer 2010-04-02
  • 打赏
  • 举报
回复
new String(connect("S",s,"1").getBytes("ISO-8859-1
"));

67,513

社区成员

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

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