分享

Tomcat及MySQL中文乱码问题解决方案总览

 昵称2807 2005-11-18
Tomcat和MySQL组合作为Web开发的基础,已经很流行了,但其中的乱码问题让人烦心,经过一段时间的学习和实践,现在有了一些方法消除乱码。

我用的平台是Tomcat 5.5.12+MySQL 4.1.14 操作系统为WinXP sp2 和 Redhat 7.2

一、Tomcat配置的地方,主要针对5.5.X版本,其他低版本可能无效

Tomcat安装到系统时,如果系统的内码为GBK,则能少很多问题....

经反复查找后,发现影响的东西,在Web.XML中 和Server.XML配置一下,就可以消除相关的影响....

其中conf\Server.XML中,在Connector节中加入设置,如:

其中conf\Web.XML中,加入


        jsp
 
            parameterEncoding
            gbk
       

  
            inputEncoding
            gbk
       

  
            outputEncoding
            gbk
       

  
            fileEncoding
            gbk
       

       
            javaEncoding
            gbk
       

二、加入过滤器能够解决的问题,针对post提交有效

***************SetCharacterEncodingFilter.java

package com.cecp.filter;

import javax.servlet.*;
import java.io.IOException;

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中,加入


    Set Character Encoding
    com.cecp.filter.SetCharacterEncodingFilter
   
        encoding
        GBK
   

   
        ignore
        true
   


    Set Character Encoding
      /*

二、MySQL配置的地方 大部分针对4.1.12

 4.1本身支持多语言
 4.1中 库和表编码设置为GBK ,那么在PhpAdmin和Java中可以处理中文,在其他工具中,需要打开连接后加入"Set Names GBK"后,才能使用中文的查询和更新

 4.1中,如果设置库为Itainl,可以保持和4.0的兼容,并在其他工具,如Delphi,PB中,可以直接使用中文,可以在过渡旧数据时使用...

 4.0默认编码为 Itainl,但可以设置服务器ini [mysqld] default-character-set=gbk,其效果,相当于表内容为GBK,但库为itain1的方式

四、JDBC驱动串中能够解决的问题

在Jsp或者Bean中直接输入jdbc串时,如下
jdbc:mysql://127.0.0.1/cecpdmV90_test?useUnicode=true&characterEncoding=GBK

在XML配置文件中输入jdbc串时,如下
jdbc:mysql://localhost/pdmtest?useUnicode=true&characterEncoding=GBK

五、WebWork托管修正的方法

       WebWork.property文件中,可以配置一下编码和语言
       webwork.locale=zh_CN
       webwork.i18n.encoding=GBK

六、代码中修正的方法

使用以下函数手工转码.. toCN(vValue);

public static String toCN(String vValue) {
        try {
            return new String(vValue.getBytes("ISO-8859-1"), "GBK");
        } catch (Exception e) {
            return vValue;
        }
    }

七、中文文件上载的问题

中文文件上载后,应该更名保存比较好

因为链接不能直接访问中文文件名,所以,要么采用更名下载的方式,要么就读到内存中,输出下载

现在,我的处理方式,是上载时,保存文件为另一个名字,如Guid,再保存GUID和原有的中文文件名,下载时,通过数据库查询到原有的中文文件名,读取GUID文件后,赋中文名字到客户端,这样能比较方便的处理

当然也可以保持文件原名保存,但容易出现重名,而且,同样不能直接根据文件名下载,只能使用getFile.jsp?FileName=文件名.doc的方式,下载真正的文件

另外,有几点心得,用GBK实在是好麻烦...以后还是考虑用UTF-8,像DW8等工具,能很好的处理UTF-8中文,Java本身也是支持UTF-8的,这种方式实在是很不理想,且麻烦。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多