为了账号安全,请及时绑定邮箱和手机立即绑定

java 字符串字符串乱码问题?

java 字符串字符串乱码问题?

C#
www说 2019-03-05 10:06:23
String name = request.getParameter("name"); 在我的电脑上面name不是乱码,但到别人的电脑上,name会出现乱码,我想问问,出现这种情况会是什么原因造成的?name = new String(name.getBytes("ISO-8859-1"),"utf-8"); 我有这样设置过,这样的话我的电脑上会是乱码,但到别人的电脑上就能正常显示。 
查看完整描述

3 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

JSP在地址里直接传汉字是有这个乱码问题。可以采用

public String tostring(String s){
String str=s;
try{
byte bytes[]=str.getBytes("ISO-8859-1");
str=new String(bytes,"GBK");
return str;
}catch(Exception e){
return str;
}
}
public String ex_chinese(String str){
if(str==null){
str ="" ;
}
else{
try {
str = new String(str.getBytes("iso-8859-1"),"gb2312") ;
}
catch (Exception ex) {
}
}
return str ;
}

来转换!



查看完整回答
反对 回复 2019-03-31
?
万千封印

TA贡献1891条经验 获得超3个赞

应该是编码格式的问题吧
//ISO编码转为GBK编码
public static String ISOtoGBK(String s) {
String str = "";
try {
if (s == null || s == "" || s.equals("")) {
str = s;
} else {
str = new String(s.getBytes("ISO8859-1"),"GBK");
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}



查看完整回答
反对 回复 2019-03-31
?
婷婷同学_

TA贡献1844条经验 获得超8个赞

程序中的字符集要统一
页面上,action等
可以选择一个过滤器来过滤所有的字符,转换成你所需要的字符

查看完整回答
反对 回复 2019-03-31
  • 3 回答
  • 0 关注
  • 1377 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信