环境 : 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();
}
}
{
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();
}
}
解决方案 »
- java中的类似PHP中的hash_hmac函数
- struts2在启动的时候跟请求的时候会发出警告,怎么回事?
- <many-to-one ...access="property"../>的access的问题
- 哪位仁兄帮忙写个项目描述,谢啦!
- 急,下拉列表的显示问题
- struts中向数据库插入数据,出现编码问题
- 如何清除DOS屏幕
- Struts高手,请指教一下!
- struts 中的多模块配置问题。。
- struts学习 ------- jpetStore例子 的问题
- [求助]Struts2+Spring+ibatis 动态更新实现不了,固定更新就可以
- ibator问题 数据库设置为bit类型 自动生成后成object类型!
"));
//这样还是乱码
System.out.println(new String(connect("S",s,"1").getBytes("ISO8859-1")));
出来的也是 : ????????????
LOOK
http://topic.csdn.net/u/20090910/18/df5a7313-d27a-49ed-8274-2a11b629371b.html
我通过 抓包 工具来查看了下, 我是从数据库 ISO8859-1 --> 获取时就GBK了,
那么我用流读取的时候就是GBK。 而输入字符流 有默认的字符编码(UTF8)的。
所以通过输入流来读取GBK值时 就出现部分字符转换有问题。 解决办法 : 将输入字符流 的默认编码 改为 gbk 就可以了。谢谢各位的关注和帮忙 。