社区
Web 开发
帖子详情
高分求助:关于乱码的问题.
seikoo
2004-10-19 03:36:45
能不能判断一个字符串的编码方式呢?
假如说从web上传递过来一个字符串,我把这些字符串按GBK的方式编码后,有的正常显示,有的还是码?
不知道该如何编码?请高手指点迷津...
是不是跟web传来的字符串的编码有关呢?
如果原字符串是iso8859-1的编码,那就可以通过new String(temp.getBytes(),"GBK");的方式,转为GBK.
但有的字符仍为 "鑴楄祩??鑴欒矾鑴楁埉鑴欐埉"的形式
请高手给个指点.谢谢了...
...全文
178
15
打赏
收藏
高分求助:关于乱码的问题.
能不能判断一个字符串的编码方式呢? 假如说从web上传递过来一个字符串,我把这些字符串按GBK的方式编码后,有的正常显示,有的还是码? 不知道该如何编码?请高手指点迷津... 是不是跟web传来的字符串的编码有关呢? 如果原字符串是iso8859-1的编码,那就可以通过new String(temp.getBytes(),"GBK");的方式,转为GBK. 但有的字符仍为 "鑴楄祩??鑴欒矾鑴楁埉鑴欐埉"的形式 请高手给个指点.谢谢了...
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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 ;
}
vb/vb.net原创文章精粹(2)
[url=http://www.vbbaike.com/show.asp?id=101290]关于EXCEL转换成ACCESS库[/url] [url=http://www.vbbaike.com/show.asp?id=101289]关于vb生成exe后出现的
问题
,急!!!![/url] [url=http://www.vbbaike.com/show.asp?id=101...
vb/vb.net原创文章精粹(3)
[url=http://www.vbbaike.com/show.asp?id=101350]请教VB6.0+数据库的
问题
!请各位高手指教![/url] [url=http://www.vbbaike.com/show.asp?id=101349]用VBA能否实现对WORD登陆密码的修改?[/url] [url=http://www.vbbaike.com/show.asp?...
关于一个用VB编写的PING工具的
问题
我在互联网上找到一个用VB编写的程序,是PING一个计算机的IP是否能通的工具,我改了改,使用ADO重数据库中循环读取IP地址,并将测试后的状态写入到数据库,作完后运行是可以测试,但是速度太慢了,每循环测试一个IP地址都需要接近1秒的时间,如果IP多的话时间就太长了,有其他办法让测试时间缩短吗?多线程如何做呢?谢谢各位帮忙!!! VB代码: Option Explicit Private...
根据根据图片的url怎么取得图片ImageView对象
我用的是SQLite数据库,表中只有只有与图片相关的 图片名称和图片地址这两个字段,那么怎么查出返回一个ImageView对象呢?图片是根据url从网上搜到的, 大家帮帮忙想想怎么得到,谢谢啦.... http://www.buildapp.net/android/s.asp?q=p_w_picpath 如何判断当前状态下gps开关是打开还是关闭 依然是音乐播放器的
问题
...
android开发每日汇总【2011-11-08】
[url=http://www.buildapp.net/android/show.asp?id=41700]关于Spinner的值怎么用sharedpreferences保存[/url] [url=http://www.buildapp.net/android/show.asp?id=41699]java代码怎样读取res/raw/*.wmv[/url] [url=http...
Web 开发
81,094
社区成员
341,711
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章