qiyadeng

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

          中文,又是中文!

          Posted on 2005-10-20 16:12 qiyadeng 閱讀(619) 評論(0)  編輯  收藏

          該死的!不得不讓我說臟話了!莫名其妙的問題。
          現(xiàn)在算是比較清楚了。最近正在做一個分布式的系統(tǒng)的整合。中心數(shù)據(jù)庫用的utf8的編碼,以前的老的系統(tǒng)用的是latin1的編碼。
          在latin1的編碼中插入和查詢數(shù)據(jù):
          不用在連接字符上花功夫。
          只要下面這個類,把中文轉(zhuǎn)換為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("編碼轉(zhuǎn)換錯誤:"+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("編碼轉(zhuǎn)換錯誤:"+e.getMessage()); 
          45            return ""
          46        }

          47    }

          48
          49}

          50

          在utf8編碼的那一段更簡單,所有的編碼設(shè)為utf8。
          上次mysql中文問題提到過,就不再提了。
          另外使用hibernate的時候,也會出現(xiàn)一些中文問題,這時候需要進行如下設(shè)置:
          在hibernate.cfg.xml的配置文件中加入:
          <property name="connection.characterEncoding">UTF-8</property>
          同樣不需要在連接字符串上加入?yún)?shù)。
          然后使用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>
          對應(yīng)的類為:
          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;
              }

          }



          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 蓬莱市| 陇南市| 揭阳市| 洞口县| 长寿区| 潼南县| 榆树市| 南投县| 苏尼特右旗| 湖州市| 新干县| 龙胜| 陇南市| 民权县| 徐水县| 三门峡市| 土默特左旗| 绿春县| 怀柔区| 诸城市| 泸州市| 桐庐县| 鲁甸县| 高尔夫| 太湖县| 三台县| 湘西| 二连浩特市| 正阳县| 廉江市| 弥勒县| 若尔盖县| 武安市| 二连浩特市| 方正县| 运城市| 梨树县| 通渭县| 临高县| 商丘市| 顺平县|