我从一个只支持gbk编码的插件中得到一个串,但我的项目是用utf-8的,所以显示的中文乱码,我该如何做才能显示汉字呢

解决方案 »

  1.   

    源码如下,这个插件只支持GBK格式编码,我的项目是utf-8的,我调用的是他的方法得到字符串,应该也是gbk的吧,
    public static void main(String[] args){
      String name=null;
      String password=null;
      String message = null;  try {
    name = new String("是否".getBytes("UTF-8"),"GBK");
    password = new String("11111".getBytes("UTF-8"),"GBK");
    message = new String("发送短信测试".getBytes("UTF-8"),"GBK");
    } catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
    }
        
      Sender sms = new Sender(name,password);String returnCode = sms.massSend("13633618954",message ,"时间","特服代码");
    String encode = null;
    try{
    encode = new String(returnCode.getBytes("GBK"),"UTF-8");
    }catch(Exception e){
    e.printStackTrace();
    }
    System.out.println(encode);
      } 
     
     
      

  2.   

    你的项目用UTF-8没关系啊,只要你项目所在的平台支持GBK就可以了:try {
    name = new String("是否".getBytes("iso8859-1"),"GBK");
    password = new String("11111".getBytes("iso8859-1"),"GBK");
    message = new String("发送短信测试".getBytes("iso8859-1"),"GBK");
    } catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
    }
    //这里就是你说的插件返回的值?  
    Sender sms = new Sender(name,password);
    String returnCode = sms.massSend("13633618954",message ,
    new String("时间".getBytes("iso8859-1"),"GBK"),
    new String("特服代码".getBytes("iso8859-1"),"GBK"));String encode = null;
    try{
    //这里是多余的吧?
    encode = new String(returnCode.getBytes("iso8859-1"),"GBK");
    }catch(Exception e){
    e.printStackTrace();
    }
    System.out.println(encode);