求java转码ISO-8859-1转UTF-8汉字显示不全 补救方案

2025-01-14 18:29:00
推荐回答(5个)
回答1:

给你写个过滤器。你试试。
public class ChStr {
public static String toChinese(String strvalue) {
try {
if (strvalue == null) {
return "";
} else {
strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK").trim();
return strvalue;
}
} catch (Exception e) {
return "";
}
}

//处理字符串中的空值
public static final String nullToString(String v, String toV) {
if (v == null || "".equals(v)) {
v = toV;
}
return v;
}
}

回答2:

呵呵,乱码确实比较常见,不足为奇.。

您的网页中文占多数推荐用GBK;
如果英文的比例比中文多,用UTF-8;

您要把<%@ page language="java" import="java.util.*" pageEncoding="GBK" Context="text/html;charset="GBK" %>
设置成GBK,或者UTF-8 ,还有一种GB2312; 任意一中编码格式都可以.。

请问你是什么提交方式 ,如果是get的话,用这种转码: String str=new String(str.getByte("ISO-8859-1"),"GBK");

如果你是post提交方式用 request.setcharactorEncodding("GBK");

回答3:

可以对含中文的字符串进行转码
例如:有个String类型变量str,含中文,试用方法
str = new String(str.getBytes("iso-8859-1"), "gbk");
这样返回得到的str就不会有乱码问题了

回答4:

java中 iso-8859-1 转utf-8汉字显示不全,一般是网络数据传输,比如两个工程之间,或者前台页面传到后台造成的,建议URLEncoder,URLDecoder可有效解决,如果还不行,直接使用base64

回答5:

1、在application.properties中的url中加入“useUnicode=true&characterEncoding=utf-8”,如:jdbc.url=jdbc:log4jdbc:mysql://localhost:3306/sql?useUnicode=true&characterEncoding=utf-8;
2、jsp页面加入: <%@ page contentType="text/html;charset=UTF-8"%>