环境  : 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();
   }
 }

解决方案 »

  1.   

    new String(connect("S",s,"1").getBytes("ISO-8859-1
    "));
      

  2.   


    //这样还是乱码
    System.out.println(new String(connect("S",s,"1").getBytes("ISO8859-1")));
    出来的也是 : ???????????? 
      

  3.   

    设置下POST方法的HEAD,设置为“UTF-8”或是“GBK2312”
      

  4.   

    如果再不行就在MySql的连接字符串后面指定一下字符集的编码格式,或者写个过滤器也可以啊
      

  5.   

    建议,从数据库、JSP页面、IDE统一都用utf-8,这样省了考虑编码问题
      

  6.   

    确认乱码是哪一步骤的 然后再进行处理 转码是不需要的
    LOOK
    http://topic.csdn.net/u/20090910/18/df5a7313-d27a-49ed-8274-2a11b629371b.html
      

  7.   

    问题目前解决了: 
    我通过 抓包 工具来查看了下, 我是从数据库 ISO8859-1 -->  获取时就GBK了, 
    那么我用流读取的时候就是GBK。 而输入字符流 有默认的字符编码(UTF8)的。
    所以通过输入流来读取GBK值时 就出现部分字符转换有问题。 解决办法 : 将输入字符流 的默认编码 改为 gbk  就可以了。谢谢各位的关注和帮忙 。