针对firefox与IE对中文的编码处理不一致,IE总是以ISO-8859-1编码,而firefox则是以GB2312或者UTF8,所以可以采用如下方法来兼容IE和Firefox。
首先对得到到字符串进行编码判断,如果是ISO-8859-1,则进行编码转换,否则就不用转换了。
判断编码的方法如下:
public static String getEncoding(String str) {
String encode = "GB2312";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s = encode;
return s;
}
} catch (Exception exception) {
}
encode = "ISO-8859-1";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s1 = encode;
return s1;
}
} catch (Exception exception1) {
}
encode = "UTF-8";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s2 = encode;
return s2;
}
} catch (Exception exception2) {
}
encode = "GBK";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s3 = encode;
return s3;
}
} catch (Exception exception3) {
}
return "";
}
本文介绍了一种兼容Firefox和IE浏览器的编码处理方法。通过判断字符串的编码格式(ISO-8859-1、GB2312、UTF-8或GBK),确保不同浏览器下中文字符的正确显示。

被折叠的 条评论
为什么被折叠?



