您的位置:首页 > 理论基础 > 计算机网络

http请求中乱码------编码的转换

2016-02-15 10:45 645 查看
在http请求中如果包含了汉字,那么就会出现乱码,引文默认的编码是ISO的,为了正常的显示,需要将编码转化为UTF或者gbk,下面提供一个辅助类。

import java.io.UnsupportedEncodingException;

/**
* 汉字转码的辅助类
* @date 2015-09-06 10:29:22
* @author geenkDC
*
*/
public class ISO2UTF{

public static String iso2utf(String isoStr){
String utfStr = null;
try {
// 入参汉字的转码
utfStr = new String(isoStr.getBytes("ISO-8859-1"), "UTF-8");

} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return utfStr;
}
public static String iso2gbk(String isoStr){
String utfStr = null;
try {
// 入参汉字的转码
utfStr = new String(isoStr.getBytes("ISO-8859-1"), "GBK");

} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return utfStr;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: