下面的例子是经历过测试的,没有问题。
由于我在调用webservice之前,通过filter已经把字符集编码变成GBK,而xfire 数据传输原理,它是通过ISO-8859-1的编码格式转化成GBK,所以很多的时候转换过后的字符集是GBK的乱码!
import java.io.UnsupportedEncodingException;
public class test {
public static void main(String arg[]){
try {
/**
* xfire 数据传输原理,它是通过ISO-8859-1的编码格式转化成GBK
* 所以,如果之前的编码格式是GBK的话相当于
* getBytes("ISO-8859-1"),"GBK")也就是把中文直接以ISO-8859-1的格式,转化成了GBK
*
* 所以传到服务器的时候就变成了乱码
*
* 所以在服务器端调用的时候就需要getBytes("GBK"),"utf-8"
* String xmlStr_tp = new String(xmlStr.getBytes("GBK"),"UTF-8");
*
*/
String s ="你好,我很满意";
String s1 = new String(s.getBytes("GBK"),"utf-8");
//哈哈
String s3 = new String(s.getBytes("GBK"),"utf-8");
String s5 = new String(s.getBytes("utf-8"),"ISO-8859-1");
String s6 = new String(s5.getBytes("ISO-8859-1"),"GBK");
String s4 = new String(s.getBytes("utf-8"),"GB2312");
System.out.println("s1==="+s1);
System.out.println("s4==="+s4);
System.out.println("s3==="+s3);
System.out.println("s6==="+s6);
String s2 = new String(new String(s.getBytes("GBK"),"ISO-8859-1").getBytes("ISO-8859-1"),"GBK");
System.out.println("s2==="+s2);
String tempStr = "中文";//准备转换的字符
String result =java.net.URLEncoder.encode(tempStr,"utf-8");
System.out.println("result==="+result);
// System.out.println("ISO-GBK" +new String(haha.getBytes("GBK"),"ISO-8859-1"));
// System.out.println("ISO-GBK" +new String(haha.getBytes("ISO-8859-1"),"GBK"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}