问征夫以前路
感谢所有关心过支持过我的人, 感谢所有恨过我嘲笑过我的人 !
posts - 30,comments - 147,trackbacks - 0

          textarea中特殊字符回车换行存入数据库丢失数据的问题已解决,现把解决方法昭告天下:)
          解决这两类问题的时候,必不可少的就是要有一个java类。
         

  1/*
  2 * Escape.java
  3 *
  4 * Created on 2007年11月19日, 下午1:41
  5 *
  6 * To change this template, choose Tools | Template Manager
  7 * and open the template in the editor.
  8 */

  9
 10package com.bjnni.cims.common;
 11
 12/**
 13 * 处理特殊字符,如:(!@#$%^&*=)的编码和解码类
 14 * @author ken  <zhanghhui@126.com>
 15 */

 16public class Escape {
 17    private final static String[] hex = {
 18        "00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F",
 19        "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F",
 20        "20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F",
 21        "30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F",
 22        "40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F",
 23        "50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F",
 24        "60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F",
 25        "70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F",
 26        "80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F",
 27        "90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F",
 28        "A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF",
 29        "B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF",
 30        "C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF",
 31        "D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF",
 32        "E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF",
 33        "F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF"
 34    }
;
 35    private final static byte[] val = {
 36        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
 37        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
 38        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
 39        0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
 40        0x3F,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
 41        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
 42        0x3F,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
 43        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
 44        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
 45        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
 46        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
 47        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
 48        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
 49        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
 50        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
 51        0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F
 52    }
;
 53    public static String escape(String s) {
 54        StringBuffer sbuf = new StringBuffer();
 55        int len = s.length();
 56        for (int i = 0; i < len; i++{
 57            int ch = s.charAt(i);
 58            if (ch == ' '{                        // space : map to '+'
 59                sbuf.append('+');
 60            }
 else if ('A' <= ch && ch <= 'Z'{    // 'A'..'Z' : as it was
 61                sbuf.append((char)ch);
 62            }
 else if ('a' <= ch && ch <= 'z'{    // 'a'..'z' : as it was
 63                sbuf.append((char)ch);
 64            }
 else if ('0' <= ch && ch <= '9'{    // '0'..'9' : as it was
 65                sbuf.append((char)ch);
 66            }
 else if (ch == '-' || ch == '_'       // unreserved : as it was
 67                    || ch == '.' || ch == '!'
 68                    || ch == '~' || ch == '*'
 69                    || ch == '\'' || ch == '('
 70                    || ch == ')'{
 71                sbuf.append((char)ch);
 72            }
 else if (ch <= 0x007F{              // other ASCII : map to %XX
 73                sbuf.append('%');
 74                sbuf.append(hex[ch]);
 75            }
 else {                                // unicode : map to %uXXXX
 76                sbuf.append('%');
 77                sbuf.append('u');
 78                sbuf.append(hex[(ch >>> 8)]);
 79                sbuf.append(hex[(0x00FF & ch)]);
 80            }

 81        }

 82        return sbuf.toString();
 83    }

 84    public static String unescape(String s) {
 85        StringBuffer sbuf = new StringBuffer();
 86        int i = 0;
 87        int len = s.length();
 88        while (i < len) {
 89            int ch = s.charAt(i);
 90            if (ch == '+'{                        // + : map to ' '
 91                sbuf.append(' ');
 92            }
 else if ('A' <= ch && ch <= 'Z'{    // 'A'..'Z' : as it was
 93                sbuf.append((char)ch);
 94            }
 else if ('a' <= ch && ch <= 'z'{    // 'a'..'z' : as it was
 95                sbuf.append((char)ch);
 96            }
 else if ('0' <= ch && ch <= '9'{    // '0'..'9' : as it was
 97                sbuf.append((char)ch);
 98            }
 else if (ch == '-' || ch == '_'       // unreserved : as it was
 99                    || ch == '.' || ch == '!'
100                    || ch == '~' || ch == '*'
101                    || ch == '\'' || ch == '('
102                    || ch == ')'{
103                sbuf.append((char)ch);
104            }
 else if (ch == '%'{
105                int cint = 0;
106                if ('u' != s.charAt(i+1)) {         // %XX : map to ascii(XX)
107                    cint = (cint << 4| val[s.charAt(i+1)];
108                    cint = (cint << 4| val[s.charAt(i+2)];
109                    i+=2;
110                }
 else {                            // %uXXXX : map to unicode(XXXX)
111                    cint = (cint << 4| val[s.charAt(i+2)];
112                    cint = (cint << 4| val[s.charAt(i+3)];
113                    cint = (cint << 4| val[s.charAt(i+4)];
114                    cint = (cint << 4| val[s.charAt(i+5)];
115                    i+=5;
116                }

117                sbuf.append((char)cint);
118            }

119            i++;
120        }

121        return sbuf.toString();
122    }

123    public static void main(String[] args) {
124        String stest = "中文1234 abcd[]()<+>,.~\\";
125        System.out.println(stest);
126        System.out.println(escape(stest));
127        System.out.println(unescape(escape(stest)));
128    }

129}

130

  这个类里面定义了escape和unescape两个方法,escape用于对特殊字符编码,unescape用于解码。在添加页面,用到testarea控件,在数据入库前,先要用escape进行编码。然后在更改页面显示的时候,要调用unescape方法进行解码,要记得在页面里把Escape类引进来。这样testarea的回车换行以及textarea中特殊字符存入数据库丢失数据的问题就解决了。
posted on 2007-11-20 11:50 kenzhang 阅读(6518) 评论(7)  编辑  收藏

FeedBack:
# re: 解决textarea特殊字符回车换行存入数据库数据丢失的问题
2007-11-20 21:41 | 隔叶黄莺
没必要搞的这么复杂,如果你的是正解,这就意味着用了textarea就必须使这么一大段代码,确切一点说,这会是谁的bug呢  回复  更多评论
  
# re: 解决textarea特殊字符回车换行存入数据库数据丢失的问题
2007-11-20 21:42 | 冷面阎罗
支持一下  回复  更多评论
  
# re: 解决textarea特殊字符回车换行存入数据库数据丢失的问题
2007-11-21 09:03 | 阿蜜果
继续努力!嘻嘻  回复  更多评论
  
# re: 解决textarea特殊字符回车换行存入数据库数据丢失的问题
2007-11-21 09:08 | kenzhang
@隔叶黄莺
如果项目用的是struts,用的是<html:testarea>标签,那么我上面写的问题就不会出现,当然也就用不到escape类。
这个问题只是针对我做的这个项目(jsp+servlet),因为一些数据的增、删、改、查都是在jsp页面实现的,其中也用到了很多javascript,所以在testarea中解决特殊字符就会有些复杂了!  回复  更多评论
  
# re: 解决textarea特殊字符回车换行存入数据库数据丢失的问题
2007-11-21 09:16 | 隔叶黄莺
统一设置UTF-8字符集呀  回复  更多评论
  
# re: 解决textarea特殊字符回车换行存入数据库数据丢失的问题
2007-11-21 09:20 | kenzhang
@隔叶黄莺
我们每个页面都统一用UTF-8字符集。  回复  更多评论
  
# re: 解决textarea特殊字符回车换行存入数据库数据丢失的问题
2007-12-04 17:16 | hj
tyt
hgjdy  回复  更多评论
  

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


网站导航: