高分求助:关于乱码的问题.

seikoo 2004-10-19 03:36:45
能不能判断一个字符串的编码方式呢?

假如说从web上传递过来一个字符串,我把这些字符串按GBK的方式编码后,有的正常显示,有的还是码?
不知道该如何编码?请高手指点迷津...

是不是跟web传来的字符串的编码有关呢?

如果原字符串是iso8859-1的编码,那就可以通过new String(temp.getBytes(),"GBK");的方式,转为GBK.

但有的字符仍为 "鑴楄祩??鑴欒矾鑴楁埉鑴欐埉"的形式

请高手给个指点.谢谢了...
...全文
178 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
seikoo 2004-10-20
  • 打赏
  • 举报
回复
public class Charset {

public static boolean isCS(String str){
if(null==str) return false;
if(str.trim()=="") return false;
byte[] bytes=str.getBytes();
if(bytes.length<2)
return false;
byte aa=(byte)0xB0;
byte bb=(byte)0xF7;
byte cc=(byte)0xA1;
byte dd=(byte)0xFE;
if(bytes[0]>=aa && bytes[0]<=bb){
if(bytes[1]<cc || bytes[1] > dd){
return false;
}
return true;
}
return false;
}
public static boolean isBig5(String str){
if(null==str) return false;
if(str.trim()=="") return false;
byte[] bytes=str.getBytes();
if(bytes.length<2)
return false;
byte aa=(byte)0xB0;
byte bb=(byte)0xF7;
byte cc=(byte)0xA1;
byte dd=(byte)0xFE;
if(bytes[0]>=aa && bytes[0]<=bb){
if(bytes[1]<cc || bytes[1] > dd){
return true;
}
return false;
}
return false;
}
}
还是我自己来吧...看看吧.如何判断中文简体和繁体的.
windydenny 2004-10-20
  • 打赏
  • 举报
回复
<%@ page contentType="text/html; charset=big5" language="java" import="java.sql.*" errorPage="" %>
....
<meta http-equiv="Content-Type" content="text/html; charset=big5">
anewjavaboy 2004-10-20
  • 打赏
  • 举报
回复
简单的说,你要知道web上传递过来的字符串的编码,它是和包含该表单的页面采用相同的编码。

你看一下,包含表单的那个页面,你有没有写response.setContentType()方法,若没有,则包含表单的页面的编码为ISO-8859-1,用它来为中文编码,肯定会错!所以你应加上response.setContentType()方法,编码为GBK,GB2312,UTF-8都可。

若你加了response.setContentType()方法,设它的编码为ch,然后你再在处理表单数据的页面加上request.setCharacterEncoding("ch"),然后再添加response.setContentType("text/html;charset=ch")即可。

告你一个一劳永逸的方法:
1,在你输出表单的那个页面,加上response.setContentType("text/html;charset=UTF-8")
2,在你处理表单的那个页面,加上request.setCharacterEncoding("UTF-8")
3,在你处理表单的那个页面,加上requestsetContentType("text/html;charset=UTF-8")
完成以上三步,你的页面不管是输入什麽语言都不会出现乱码。
GONHON 2004-10-20
  • 打赏
  • 举报
回复
用一下request.setCharacterEncoding("GB2312")
试试,我用后有用可以显示
seikoo 2004-10-20
  • 打赏
  • 举报
回复
不是很准确...只做为参考吧...
thinkner 2004-10-19
  • 打赏
  • 举报
回复
哪就把你的代码拿出来让大家看了后再说吧,对症下药吧
seikoo 2004-10-19
  • 打赏
  • 举报
回复
我把这些字符串按GBK的方式编码后,有的正常显示,有的还是乱码,后者是莫名其妙的繁体字.
seikoo 2004-10-19
  • 打赏
  • 举报
回复
我把这些字符串按GBK的方式编码后,有的正常显示,有的还是码
seikoo 2004-10-19
  • 打赏
  • 举报
回复
你们都没懂...看看我问的是什么...
wjpsdm 2004-10-19
  • 打赏
  • 举报
回复
byte[] by;
String ss=request.getParameter("ss1");
by=ss.getBytes("iso-8859-1");
ss=new String(by);
ynniebo 2004-10-19
  • 打赏
  • 举报
回复
这里有一个比较简单的方法
写一个过滤器:如SetCharacterEncodingFilter.java
package 所在包;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;


public class SetCharacterEncodingFilter implements Filter {

protected String encoding = null;

protected FilterConfig filterConfig = null;

protected boolean ignore = true;

public void destroy() {

this.encoding = null;
this.filterConfig = null;

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {

if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}

chain.doFilter(request, response);

}


public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;

}


protected String selectEncoding(ServletRequest request) {

return (this.encoding);

}


}

写完以后,在web.xml里面加上
加在<web-app>下面
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>包名.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
就这么两步,如果你用转换方法来做的话.可能操作数据库的过程中还会出现乱码.这个方法不会出现.我也是用这个方法.
nuboy 2004-10-19
  • 打赏
  • 举报
回复
我顶
panzi667 2004-10-19
  • 打赏
  • 举报
回复
好东东,学习,
zhz586 2004-10-19
  • 打赏
  • 举报
回复
在对数据库操作时转好,页面上就不需要了!
////////////////中文转为字符串(进入数据库)
public static String toISO8859(String str) {
if (str == null) {
return null;
}
try {
return new String(str.getBytes("ISO8859_1"));
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}

////////////////字符串转为中文(从数据库读出)
public static String toGBK(String str) {
if (str == null) {
return null;
}
try {
return new String(str.getBytes("ISO8859_1"), "GB2312");
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
vbscript1981 2004-10-19
  • 打赏
  • 举报
回复
1.http://www.jdon.com/jive/article.jsp?forum=16&thread=13852

这是一个好帖!看了之后,保你不会再出现中文的问题! 并且一劳永逸!

2.给你一个转换函数
//转换为gb2313码
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 ;
}

81,094

社区成员

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

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