大家好,又见面了,我是你们的朋友全栈君。
在做接口联调的时候出现访问对方的时候需要把编码转成gb18030格式的,我这边默认是utf8,这个困扰了很长时间,在网上百度发现大部分字符串转编码都是使用string.getByte(“编码格式”)的方式字节转码,可事实上这样是行不通的。原因有点难说,这里我就说一下可行的方案。
@Test
public void toObject() throws UnsupportedEncodingException{
System.out.println("2".equals(null));
String str = "ab丁亦凝";//编译环境默认是utf8格式
byte[] bytes = str.getBytes(Charset.forName("GB18030"));//这一步就是转成gb18030格式的字节码
for (byte b : bytes)
{
System.out.print(b + " ");
}
//字节码转成gb18030的字符串
String str4 = new String(bytes, "GB18030");
System.out.println(str4);
System.out.println();
//再转回utf8
byte[] bytes4 = str4.getBytes(Charset.forName("UTF-8"));
for (byte b : bytes4)
{
System.out.print(b + " ");
}
System.out.println();
String str44 = new String(bytes4, "UTF-8");
System.out.println("---"+str44);//abc你好
}
下面再介绍几种其他格式的,其实方式都一样:
@Test
public void aaa(){
System.out.println("default charset : "+Charset.defaultCharset());
String str = "abc你好";//string with UTF-8 charset
byte[] bytes = str.getBytes(Charset.forName("UTF-8"));//convert to byte array with UTF-8 encode
for (byte b : bytes)
{
System.out.print(b + " ");
}
System.out.println();
try
{
String str1 = new String(bytes, "UTF-8");//to UTF-8 string
String str2 = new String(bytes, "ISO-8859-1");//to ISO-8859-1 string
String str3 = new String(bytes, "GBK");//to GBK string
String str4 = new String(bytes, "GB18030");
System.out.println(str1);//abc你好
System.out.println(str2);//abc??????
System.out.println(str3);//abc浣犲ソ
System.out.println(str4);
System.out.println();
byte[] bytes2 = str2.getBytes(Charset.forName("ISO-8859-1"));
for (byte b : bytes2)
{
System.out.print(b + " ");
}
System.out.println();
String str22 = new String(bytes2, "UTF-8");
System.out.println(str22);//abc你好
System.out.println();
byte[] bytes3 = str3.getBytes(Charset.forName("GBK"));
for (byte b : bytes3)
{
System.out.print(b + " ");
}
System.out.println();
String str33 = new String(bytes3, "UTF-8");
byte[] bytes4 = str4.getBytes(Charset.forName("GB18030"));
for (byte b : bytes4)
{
System.out.print(b + " ");
}
System.out.println();
String str44 = new String(bytes4, "UTF-8");
System.out.println("---"+str44);//abc你好
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
}
最后在联调接口通讯中,注意两点:
1,在最终传输的字节数组中修改编码,
2,对方返回的信息,要先使用对方编码转成字符串,再转成自己需要的编码
在下面的例子中有讲解:
private String sendReq(Object req) throws Exception {
String reqXml = XStreamHandler.toXml(req);
//加入报文头
reqXml=HEADER+reqXml;
//计算报文长度
int len=reqXml.getBytes().length;
String bodyStr = String.format("%04d", len);
//加入报文长度
reqXml=bodyStr+reqXml;
//utf8转gb18030 下面两行就是多余的,因为不是最终修改编码的位置
byte[] bytes = reqXml.getBytes(Charset.forName("GB18030"));
reqXml = new String(bytes, "GB18030");
logger.info("组装好的最终报文是:"+reqXml);
YakMessage msg = new YakMessage();
ByteBuffer buffer = ByteBuffer.allocate(reqXml.getBytes().length);
buffer.put(reqXml.getBytes(Charset.forName("GB18030"))); //这里才是有用的,因为参数传递最终还是在io流中以字节格式传输的
//设置YakMessage
msg.setRawMessage(buffer.array());
buffer.clear();
//发送请求
YakMessage response = FBHYShortTermSocketClient.write(msg);
String respXml = new String(response.getRawMessage(),Charset.forName("GB18030"));//这里对面返回的文字编码是GB18030,
//gb18030转utf8
byte[] bytes2 = respXml.getBytes(Charset.forName("UTF-8"));
respXml = new String(bytes2, "UTF-8");
return respXml;
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106078.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...