再提乱码问题GBK->UTF-8

mindon 2005-12-26 04:23:11
使用
out.println(s.getBytes("GBK"), "UTF-8")

当 s = "测试" 时输出正常

当 s = "测 试" 时就成了“[]? []?”

当 s = "计算机" 时,输出成了 "计算??"

这样的半乱码问题一般用什么方式处理?
...全文
247 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
guishuanglin 2006-01-02
  • 打赏
  • 举报
回复
Jsp+tomcat+bean中文乱码问题解决方法javabean中参数有乱码
1) 所有的jsp页面指定字符编码方式,如:Charest=gb2312,Charest=UTF-8等等
2) 在应用服务器中的server.xml方件中找到设置服务器端口的行,一般是这样开头:”<Connector port="8080"”,
3) 在找到的行"<Connector"开头的字符串后加上“URIEncoding="UTF-8"“,保存文件
--------------------------------------------
jsp页面有乱码解决方法
所有的jsp页面指定字符编码方式,如:Charest=gb2312,Charest=UTF-8等等
<%@ page contentType="text/html; charset=UTF-8">
--------------------------------------------
jsp单个中文参数乱码解决方法
用这个转换一下:
<%!String trans(String chi)
{
string result =null;
byte temp[];
temp=chi.getBytes("iso=8859-1");
result= new String(temp);
}
%>
或者直接这样:
<%
request.setCharacterEncoding("UTF-8");
out.println(request.getParameter("参数ID")
%>
--------------------------------------------------

mindon 2005-12-27
  • 打赏
  • 举报
回复
IE 暂时解决方案

<SCRIPT LANGUAGE="vbScript">
<!--
Function URLEncoding(vstrIn)
strReturn = ""
For i = 1 To Len(vstrIn)
ThisChr = Mid(vStrIn,i,1)
If Abs(Asc(ThisChr)) < &HFF Then
strReturn = strReturn & ThisChr
Else
innerCode = Asc(ThisChr)
If innerCode < 0 Then
innerCode = innerCode + &H10000
End If
Hight8 = (innerCode And &HFF00)\ &HFF
Low8 = innerCode And &HFF
strReturn = strReturn & "%" & Hex(Hight8) & "%" & Hex(Low8)
End If
Next
URLEncoding = strReturn
End Function
//-->
</SCRIPT>

提交时采用GBK编码(以上VBScript函数URLEncoding实现),不使用encodeURI的UTF-8编码,这样目标页面就不需转码,而且一切正常。(真是迫不得已才会用VBScript)
mindon 2005-12-27
  • 打赏
  • 举报
回复
这里的substring似乎不存在问题,先转码再indexOf和substring也是一样的结果(不过效率高些)

String q = request.getParameter("q");
q = new String(q.getBytes("GBK"), "UTF-8");
String[] qarr = q.split("\r\n");
...
ifc.setIfcDesc(qarr[k].substring(0,m));
...

虽然还是一样的结果,但效率高多了,至少不在循环中转码了。


我昨天细想了一下,发现情况可能是这样的:

我通过encodeURI把参数q提交到这个jsp的时候,在request.getParameter("q")中由于
<%@ page contentType="text/html; charset=GBK" %> 的缘故,已经成为了GBK编码,也就是说

String q = request.getParameter("q");

这里的q内容是UTF-8的,但却已经用GBK编码了(已经错误编码了),即已经是乱码了,所以

ifc.setIfcDesc(new String(qarr[k].substring(0,m).getBytes("GBK"),"UTF-8"));

只不过是恢复其原来面目,可是这转换过程可能发生了点小问题导致了这种状况。
SoulOfEdge 2005-12-27
  • 打赏
  • 举报
回复
学习
  • 打赏
  • 举报
回复
qarr.length
new String(qarr[k].substring(0,m).getBytes("GBK"),"UTF-8"));

单字节和双字节你不要搞混了


你能不能转完再substring

iso-8859_1


零上三度 2005-12-26
  • 打赏
  • 举报
回复
当 s = "计算机" 时,输出成了 "计算??"

这个跟字体有关,在日文系统下看汉字,如果选择SimSun,就会看到正确的文字,其他字体就可能出现问号

另外,如果你用多国语言版os,会出现乱码的问题,如果用纯日文os+汉字字库,会好一些(我们的系统还是有时出问题。)
其实,用unicode最好,不明白小日本为什么要求不用unicode
mindon 2005-12-26
  • 打赏
  • 举报
回复
to jsp_servlet_javabean

getBytes("iso-8859-1"),"UTF-8"));

我也踹(try)过了,

getBytes("GBK"),"UTF-8"))

似乎是唯一能看到正确汉字的方式,除了个别的字显示不正常外
  • 打赏
  • 举报
回复
getBytes("iso-8859-1"),"UTF-8"));
mindon 2005-12-26
  • 打赏
  • 举报
回复
<%@ page contentType="text/html; charset=GBK" %<html>
<head>
<title>信息收集</title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<%
// 使用XMLHTTP提交的经过encodeURI的参数
String q = request.getParameter("q");
String[] qarr = q.split("\r\n");
// DAO
InfoCollecDAO ifcdao = new InfoCollecDAO();
boolean ifk = ifcdao.keep();
try {
for(int k=0; k<qarr.length; k++) {
int m = qarr[k].indexOf(",");
int n = qarr[k].lastIndexOf(",");
if(m<n) {
InfoCollec ifc = new InfoCollec();
ifc.setIfcName(qarr[k].substring(n+1));
ifc.setIfcDesc(new String(qarr[k].substring(0,m).getBytes("GBK"),"UTF-8")); // 这样保存到 SQLSERVER 大部分汉字才正常
ifcdao.add(ifc, null);
}
}
} catch(Exception e) {
out.println("fail::" +e.getMessage());
} finally {
out.println(q); // 在xmlhttp的返回结果中汉字也发现部分不能正常显示
ifcdao.release(ifk);
}
%>
</body>
</html>

web.xml中使用了
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.mobilecode.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>

编译
InfoCollecDAO.java 和 InfoCollec.java均采用-encoding为GBK编译
leekooqi 2005-12-26
  • 打赏
  • 举报
回复
怎么这么用呢?

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧