http://www.chedong.com/tech/hello_unicode.html

解决方案 »

  1.   

    String str1 = new String(str.getBytes("iso-8859-1"),"GB2312");
      

  2.   

    java中文问题详解解决java中文问题:
    针对applet和awt:
        1:)
        Font f = new Font(UIResource.getString( "Default_font"),Font.PLAIN,12);
          UIManager.put("Label.font",f);
          UIManager.put("Label.foreground",Color.black);
          UIManager.put("Button.font",f);
          UIManager.put("Menu.font",f);
          UIManager.put("MenuItem.font",f);
          UIManager.put("List.font",f);
          UIManager.put("CheckBox.font",f);
          UIManager.put("RadioButton.font",f);
          UIManager.put("ComboBox.font",f);
          UIManager.put("TextArea.font",f);    2:)
          Font f = new Font("隶书",Font.PLAIN,15);
              UIManager.put("Button.font",font);
          UIManager.put("ToggleButton.font",font);
          UIManager.put("RadioButton.font",font);
          UIManager.put("CheckBox.font",font);
          UIManager.put("ColorChooser.font",font);
          UIManager.put("ToggleButton.font",font);
          UIManager.put("ComboBox.font",font);
          UIManager.put("ComboBoxItem.font",font);
          UIManager.put("InternalFrame.titleFont",font);
          UIManager.put("Label.font",font);
          UIManager.put("List.font",font);
          UIManager.put("MenuBar.font",font);
          UIManager.put("Menu.font",font);
          UIManager.put("MenuItem.font",font);
          UIManager.put("RadioButtonMenuItem.font",font);
          UIManager.put("CheckBoxMenuItem.font",font);
          UIManager.put("PopupMenu.font",font);
          UIManager.put("OptionPane.font",font);
          UIManager.put("Panel.font",font);
          UIManager.put("ProgressBar.font",font);
          UIManager.put("ScrollPane.font",font);
          UIManager.put("Viewport",font);
          UIManager.put("TabbedPane.font",font);
          UIManager.put("TableHeader.font",font);
          UIManager.put("TextField.font",font);
          UIManager.put("PasswordFiled.font",font);
          UIManager.put("TextArea.font",font);
          UIManager.put("TextPane.font",font);
          UIManager.put("EditorPane.font",font);
          UIManager.put("TitledBorder.font",font);
          UIManager.put("ToolBar.font",font);
          UIManager.put("ToolTip.font",font);
          UIManager.put("Tree.font",font); 3:)针对jsp和servlet:
    解决办法:
    第一:
     1:在jsp页面加入:
    <%@ page contentType="text/html; charset=gb2312" %>
     2:在servlet里面:
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=gb2312");//这是重要的3:上面的如果在不行就用如下的方法在数据入库前进行调用:
    public static String UnicodeToChinese(String s){
      try{
         if(s==null||s.equals("")) return "";
         String newstring=null;
         newstring=new String(s.getBytes("ISO8859_1"),"gb2312");
         return newstring;
        }
      catch(UnsupportedEncodingException e)
      {
      return s;
      }
      }public static String ChineseToUnicode(String s){
      try{
      if(s==null||s.equals("")) return "";
      String newstring=null;
      newstring=new String(s.getBytes("gb2312"),"ISO8859_1");
       return newstring;
      }
      catch(UnsupportedEncodingException e)
      {
      return s;
     }
      }4:)解决weblogic/webshpere中文问题:
    在web.xml文件中需要配置中文环境。r如下:
    <context-param>
      <param-name>weblogic.httpd.inputCharset./*</param-name>
      <param-value>GB2312</param-value>
    </context-param> 5:)解决基于weblogic的jsp中下载WORD文档时,在浏览中显示乱码的中文问题:
    在web.xml文件中的<web-app>标签内添加如下内容:
    <mime-mapping> 
    <extension>doc</extension> 
    <mime-type>application/msword</mime-type> 
    </mime-mapping> <mime-mapping> 
    <extension>xls</extension> 
    <mime-type>application/msexcel</mime-type> 
    </mime-mapping><mime-mapping> 
    <extension>pdf</extension> 
    <mime-type>application/pdf</mime-type> 
    </mime-mapping>6:)javamail附件中文乱码:
    /*
            @从BodyPart中提取使用ISO-8859-1编吗的文件名
            @因为BodyPart.getFilename()过程已经对文件名作了一次编码,有时不能直接使用
        */
        public static String getISOFileName(Part body){
            //设置一个标志,判断文件名从Content-Disposition中获取还是从Content-Type中获取
            boolean flag=true;
            if(body==null){
                return null;
            }
            String[] cdis;
            try{
                cdis=body.getHeader("Content-Disposition");
            }
            catch(Exception e){
                return null;
            }
            if(cdis==null){
                flag=false;
            }
            if(!flag){
                try{
                    cdis=body.getHeader("Content-Type");
                }
                catch(Exception e){
                    return null;
                }
            }
            if(cdis==null){
                return null;
            }
            if(cdis[0]==null){
                return null;
            }
            //从Content-Disposition中获取文件名
            if(flag){
                int pos=cdis[0].indexOf("filename=");
                if(pos<0){
                    return null;
                }
                //如果文件名带引号
                if(cdis[0].charAt(cdis[0].length()-1)=='"'){
                    return cdis[0].substring(pos+10,cdis[0].length()-1);
                }
                return cdis[0].substring(pos+9,cdis[0].length());
            }
            else{
                int pos=cdis[0].indexOf("name=");
                if(pos<0){
                    return null;
                }
                //如果文件名带引号
                if(cdis[0].charAt(cdis[0].length()-1)=='"'){
                    return cdis[0].substring(pos+6,cdis[0].length()-1);
                }
                return cdis[0].substring(pos+5,cdis[0].length());
            }
        }
    --------------------------
    让生命时刻充满着激情!
      

  3.   

    中文乱码问题
    1.字节和unicode
    java内核是unicode的,就连class文件也是,但是很多媒体,包括文件/流的保存方式是使用字节流的。因此java要对这些字节流经行转化。char是unicode的,而byte是字节。java中byte/char互转的函数在sun.io的包中间有。其中ByteToCharConverter类是中调度,可以用来告诉你,你用的convertor。其中两个很常用的静态函数是 
    public static ByteToCharConverter getDefault(); 
    public static ByteToCharConverter getConverter(String encoding); 
    如果你不指定converter,则系统会自动使用当前的encoding,gb平台上用gbk,en平台上用8859_1。
    byte ——〉char:
    "你"的gb码是:0xc4e3 ,unicode是0x4f60
    String encoding = "gb2312";
    byte b[] = {(byte)'\u00c4',(byte)'\u00e3'};
    ByteToCharConverter converter = ByteToCharConverter.getConverter(encoding);
    char c[] = converter.convertAll(b);
    for (int i = 0; i < c.length; i++) {
    System.out.println(Integer.toHexString(c[i]));
    }
    结果是什么?0x4f60
    如果encoding ="8859_1",结果又是什么?0x00c4,0x00e3
    如果代码改为
    byte b[] = {(byte)'\u00c4',(byte)'\u00e3'};
    ByteToCharConverter converter = ByteToCharConverter. getDefault();
    char c[] = converter.convertAll(b);
    for (int i = 0; i < c.length; i++) {
    System.out.println(Integer.toHexString(c[i]));
    }
    结果将又是什么?根据平台的编码而定。char ——〉byte:
    String encoding = "gb2312";
    char c[] = {'\u4f60'};
    CharToByteConverter converter = CharToByteConverter.getConverter(encoding);
    byte b[] = converter.convertAll(c);
    for (int i = 0; i < b.length; i++) {
    System.out.println(Integer.toHexString(b[i]));
    }
    结果是什么?0x00c4,0x00e3
    如果encoding ="8859_1",结果又是什么?0x3f
    如果代码改为
    String encoding = "gb2312";
    char c[] = {'\u4f60'};
    CharToByteConverter converter = CharToByteConverter.getDefault();
    byte b[] = converter.convertAll(c);
    for (int i = 0; i < b.length; i++) {
    System.out.println(Integer.toHexString(b[i]));
    }
    结果将又是什么?根据平台的编码而定。
    很多中文问题就是从这两个最简单的类派生出来的。而却有很多类不直接支持把encoding输入,这给我们带来诸多不便。很多程序难得用encoding了,直接用default的encoding,这就给我们移植带来了很多困难。2.utf-8
    utf-8是和unicode一一对应的,其实现很简单
    7位的unicode: 0 _ _ _ _ _ _ _ 
    11位的unicode: 1 1 0 _ _ _ _ _ 1 0 _ _ _ _ _ _ 
    16位的unicode: 1 1 1 0 _ _ _ _ 1 0 _ _ _ _ _ _ 1 0 _ _ _ _ _ _ 
    21位的unicode: 1 1 1 1 0 _ _ _ 1 0 _ _ _ _ _ _ 1 0 _ _ _ _ _ _ 1 0 _ _ _ _ _ _ 
    大多数情况是只使用到16位以下的unicode: 
    "你"的gb码是:0xc4e3 ,unicode是0x4f60
    0xc4e3的二进制:
    1100 ,0100 ,1110 ,0011
    由于只有两位我们按照两位的编码来排,但是我们发现这行不通,因为第7位不是0因此,返回"?"
    0x4f60的二进制:
    0100 ,1111 ,0110 ,0000
    我们用utf-8补齐,变成:
    1110 ,0100 ,1011 ,1101 ,1010 ,0000
    e4--bd-- a0
    于是返回:0xe4,0xbd,0xa0。3.string和byte[]
    string其实核心是char[],然而要把byte转化成string,必须经过编码。string.length()其实就是char数组的长度,如果使用不同的编码,很可能会错分,造成散字和乱码。
    例如:
    String encoding = “”;
    byte [] b={(byte)'\u00c4',(byte)'\u00e3'}; 
    String str=new String(b,encoding);  
    如果encoding=8859_1,会有两个字,但是encoding=gb2312只有一个字这个问题在处理分页是经常发生 。4.Reader,Writer / InputStream,OutputStream
    Reader和Writer核心是char,InputStream和OutputStream核心是byte。但是Reader和Writer的主要目的是要把char读/写InputStream/OutputStream。
    例如:
    文件test.txt只有一个"你"字,0xc4,0xe3
    String encoding = "gb2312";
    InputStreamReader reader = new InputStreamReader(new FileInputStream(
    "text.txt"), encoding);
    char c[] = new char[10];
    int length = reader.read(c);
    for (int i = 0; i < length; i++) {
    System.out.println(c[i]);
    }
    结果是什么?你
    如果encoding ="8859_1",结果是什么???两个字符,表示不认识。
    反过来的例子自己做。5.我们要对java的编译器有所了解:
    javac ?encoding
    我们常常没有用到encoding这个参数。其实encoding这个参数对于跨平台的操作是很重要的。如果没有指定encoding,则按照系统的默认encoding,gb平台上是gb2312,英文平台上是iso8859_1。
    java的编译器实际上是调用sun.tools.javac.main的类,对文件进行编译,这个类有compile函数中间有一个encoding的变量,-encoding的参数其实直接传给encoding变量。编译器就是根据这个变量来读取java文件的,然后把用utf-8形式编译成class文件。
    例子代码:
    String str = "你";
    FileWriter writer = new FileWriter("text.txt");
    write.write(str);
    writer.close();如果用gb2312编译,你会找到e4 bd a0的字段 ;
    如果用8859_1编译, 00c4 00e3的二进制: 
    0000,0000 ,1100,0100 ,0000,0000 ,1110,0011
    因为每个字符都大于7位,因此用11位编码: 
    1100,0001,1000,0100,1100,0011,1010,0011 
    c1-- 84-- c3--  a3 
    你会找到c1 84 c3 a3 。但是我们往往忽略掉这个参数,因此这样往往会有跨平台的问题:
    样例代码在中文平台上编译,生成zhclass
    样例代码在英文平台上编译,输出enclass
    (1).  zhclass在中文平台上执行ok,但是在英文平台上不行 
    (2). enclass在英文平台上执行ok,但是在中文平台上不行
    原因:
    (1). 在中文平台上编译后,其实str在运行态的char[]是0x4f60, 在中文平台上运行,filewriter的缺省编码是gb2312,因此 chartobyteconverter会自动用调用gb2312的converter,把str转化成byte输入到fileoutputstream中,于是0xc4,0xe3放进了文件。 
    但是如果是在英文平台下,chartobyteconverter的缺省值是8859_1, filewriter会自动调用8859_1去转化str,但是他无法解释,因此他会输出"?"
    (2). 在英文平台上编译后,其实str在运行态的char[]是0x00c4 0x00e3, 在中文平台上运行,中文无法识别,因此会出现??;
    在英文平台上,0x00c4-->0xc4,0x00e3->0xe3,因此0xc4,0xe3被放进了文件。6. 其它原因:<%@ page contentType="text/html; charset=GBK" %>
    设置浏览器的显示编码,如果response的数据是utf8编码,显示将是乱码,但是乱码和上述原因还不一样。7. 发生编码的地方:
     从数据库到java程序 byte——〉char&#61548;
     从java程序到数据库 char——〉byte&#61548;
    &#61548; 从文件到java程序 byte——〉char
     从java程序到文件 char——〉byte&#61548;
     从java程序到页面显示&#61548; char——〉byte
     从页面form提交数据到java程序byte——〉char&#61548;
     从流到java程序byte——〉char&#61548;
    &#61548; 从java程序到流char——〉byte谢志钢的解决方法:
    我是使用配置过滤器的方法解决中文乱码的:<web-app>
    <filter>
    <filter-name>RequestFilter</filter-name>
    <filter-class>net.golden.uirs.util.RequestFilter</filter-class>
    <init-param>
    <param-name>charset</param-name>
    <param-value>gb2312</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>RequestFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
    </filter-mapping>
    </web-app>
    public void doFilter(ServletRequest req, ServletResponse res,
    FilterChain fChain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    HttpSession session = request.getSession();
    String userId = (String) session.getAttribute("userid");
    req.setCharacterEncoding(this.filterConfig.getInitParameter("charset")); // 设置字符集?
    实际上是设置了byte ——〉char的encoding
    try {
    if (userId == null || userId.equals("")) {
    if (!request.getRequestURL().toString().matches(
    ".*/uirs/logon/logon(Controller){0,1}\\x2Ejsp$")) {
    session.invalidate();
    response.sendRedirect(request.getContextPath() +
    "/uirs/logon/logon.jsp");
    }
    }
    else { // 看看是否具有信息上报系统的权限
    if (!net.golden.uirs.util.UirsChecker.check(userId, "信息上报系统",
    net.golden.uirs.util.UirsChecker.ACTION_DO)) {
    if (!request.getRequestURL().toString().matches(
    ".*/uirs/logon/logon(Controller){0,1}\\x2Ejsp$")) {
    response.sendRedirect(request.getContextPath() +
    "/uirs/logon/logonController.jsp");
    }
    }
    }
    }
    catch (Exception ex) {
    response.sendRedirect(request.getContextPath() +
    "/uirs/logon/logon.jsp");
    }
    fChain.doFilter(req, res);
    }
      

  4.   

    解决办法:  
    第一:  
     1:在jsp页面加入:  
    <%@  page  contentType="text/html;  charset=gb2312"  %>  
     2:在servlet里面:  
       public  void  doGet(HttpServletRequest  request,  HttpServletResponse  response)  throws  ServletException,  IOException  {  
           response.setContentType("text/html;  charset=gb2312");//这是重要的  
     
    3:上面的如果在不行就用如下的方法在数据入库前进行调用:  
    public  static  String  UnicodeToChinese(String  s){  
       try{  
             if(s==null  &brvbar;  &brvbar;s.equals(""))  return  "";  
             String  newstring=null;  
             newstring=new  String(s.getBytes("ISO8859_1"),"gb2312");  
             return  newstring;  
           }  
       catch(UnsupportedEncodingException  e)  
       {  
       return  s;  
       }  
       }  
     
    public  static  String  ChineseToUnicode(String  s){  
       try{  
       if(s==null  &brvbar;  &brvbar;s.equals(""))  return  "";  
       String  newstring=null;  
       newstring=new  String(s.getBytes("gb2312"),"ISO8859_1");  
         return  newstring;  
       }  
       catch(UnsupportedEncodingException  e)  
       {  
       return  s;  
     }  
       }
      

  5.   

    呵呵,没那么复杂拉。。就用pleonheart的方法把页面上输入的中文存入数据库,然后从数据库往外读的时候不用任何转换啦,直接显示就是中文的。