qiyadeng

专注于Java示例及教程
posts - 84, comments - 152, trackbacks - 0, articles - 34

中文,又是中文!

Posted on 2005-10-20 16:12 qiyadeng 阅读(612) 评论(0)  编辑  收藏

该死的!不得不让我说脏话了!莫名其妙的问题。
现在算是比较清楚了。最近正在做一个分布式的系统的整合。中心数据库用的utf8的编码,以前的老的系统用的是latin1的编码。
在latin1的编码中插入和查询数据:
不用在连接字符上花功夫。
只要下面这个类,把中文转换为latin1的编码,因为默认的连接是lanti1的所以还要另外的连接字符串吗?

 1/*
 2 * Created on 2005-8-15
 3 *
 4 * TODO To change the template for this generated file go to
 5 * Window - Preferences - Java - Code Style - Code Templates
 6 */

 7package com.motel168.util;
 8
 9/**
10 * @author qiya
11 * 
12 * TODO To change the template for this generated type comment go to Window -
13 * Preferences - Java - Code Style - Code Templates
14 */

15public class Chinese {
16    public static String toChinese(String iso)
17        String gb; 
18        try
19            if(iso.equals(""|| iso == null)
20                return ""
21            }
 
22            else
23                iso = iso.trim(); 
24                gb = new String(iso.getBytes("ISO-8859-1"),"GB2312"); 
25                return gb; 
26            }
 
27        }
catch(Exception e)
28            System.err.print("编码转换错误:"+e.getMessage()); 
29            return ""
30        }

31    }

32    public static String toLatin(String iso)
33        String gb; 
34        try
35            if(iso.equals(""|| iso == null)
36                return ""
37            }
 
38            else
39                iso = iso.trim(); 
40                gb = new String(iso.getBytes("GB2312"),"ISO-8859-1"); 
41                return gb; 
42            }
 
43        }
catch(Exception e)
44            System.err.print("编码转换错误:"+e.getMessage()); 
45            return ""
46        }

47    }

48
49}

50

在utf8编码的那一段更简单,所有的编码设为utf8。
上次mysql中文问题提到过,就不再提了。
另外使用hibernate的时候,也会出现一些中文问题,这时候需要进行如下设置:
在hibernate.cfg.xml的配置文件中加入:
<property name="connection.characterEncoding">UTF-8</property>
同样不需要在连接字符串上加入参数。
然后使用Filter:
在web.xml中加入如下信息:
    <filter>
        <filter-name>filter-name</filter-name>
        <filter-class>com.motel168.util.SetEncodeFilter</filter-class>
        <init-param>
            <param-name>defaultencoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>filter-name</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
对应的类为:
package com.motel168.util;

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 SetEncodeFilter implements Filter {

    protected FilterConfig filterConfig = null;

    protected String defaultEncoding = null;

    public void init(FilterConfig arg0) throws ServletException {
        this.filterConfig = arg0;
        this.defaultEncoding = filterConfig.getInitParameter("defaultencoding");

    }

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

        request.setCharacterEncoding(selectEncoding(request));
        chain.doFilter(request, response);

    }

    public void destroy() {
        this.defaultEncoding = null;
        this.filterConfig = null;

    }

    protected String selectEncoding(ServletRequest request) {
        return this.defaultEncoding;
    }

}



只有注册用户登录后才能发表评论。


网站导航: