Java中正确解码_第1页
Java中正确解码_第2页
Java中正确解码_第3页
Java中正确解码_第4页
Java中正确解码_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java 中正确解码用中正确解码用 Javascript escape 编码的编码的 中文字符中文字符 做页面开发时使用了 Javascript 的 escape 函数进行 URL 编码 如果 url 中有中文 结果在 JSP 或 Servlet 中用 request getParameter 时取不到参数 只返回一个空值 经过 Baidu 的搜索 结合我自己的工 作实际 再加上网友的一些帮助 终于找到了问题所在及解决的办法 本来 Javascript 中的 escape 是将中文按 ISO 8859 1字符集进行 URL 编码的 那样通过 request getParameter 是能直接获取到请求参数的 但后来的 Javascript 将 escape 换成了 Unicode 字符集编 码 如此一来 在 JSP 和 Servlet 中就没法直接拿到请求参数了 而且此时的 request 获得的参数值都是空 值 想了好久 也测试了不少 但是还是百思不得其解 最后借助于网络 终于解决了此问题 我露出 了灿烂的笑容 解决办法 1 首先对中文字符进行两次 escape 编码 如要传参数 name 值为 黄修群 则 url 的格式为 name escape escape 黄修群 这样一来 在 request getParameter 就能取到编码后的参数了 但是参 数去编码的形式 如下 u9EC4 u4FEE u7FA4 获得的页面参数为 获取 request 参数值 u9EC4 u4FEE u7FA4 此时在使用 URIDecoder decode 方法 则出现解码错误 错误信息如下 2010 5 19 15 28 03 org apache catalina core ApplicationDispatcher invoke 严重 Servlet service for servlet jsp threw exception java lang IllegalArgumentException URLDecoder Illegal hex characters in escape pattern For input string u9 at URLDecoder decode URLDecoder java 173 at com mapgis vfd plugins vfdwebserver VFDWebServer getPageParameterList VFDWebServer java 674 可见是解码出现了问题了 也许有人问 那我在客户端非要只用一次 escape 方法编码 会出现什么样的结果 请看下面 2010 5 19 14 59 38 org apache tomcat util http Parameters processParameters 警告 Parameters Character decoding failed Parameter skipped java io CharConversionException isHexDigit at org apache tomcat util buf UDecoder convert UDecoder java 88 at org apache tomcat util buf UDecoder convert UDecoder java 49 at org apache tomcat util http Parameters urlDecode Parameters java 412 at org apache tomcat util http Parameters processParameters Parameters java 394 看到上面的提示信息 可以断定是解码出现问题了 而获得的请求对象去是空的 如下 获取 request 参数值 null 2 由于取到的参数是 u9EC4 u4FEE u7FA4格式的 没法用常规的 URLDecoder decode 来进行解 码 还好 这世上的牛人够多 在网上直接找到了一个工具类 能实现 Javascript 中 escape 及 unescape 式的编解码 源代码如下 java JavaScript escape unescape 编码的 Java 实现 author jackyz keep this copyright info while using this method by free public class Escape private final static String hex 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF private final static byte val 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 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x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F public static String escape String s StringBuffer sbuf new StringBuffer int len s length for int i 0 i len i int ch s charAt i if ch space map to sbuf append else if A ch else if a ch else if 0 ch else if ch ch unreserved as it was ch ch ch ch ch ch ch sbuf append char ch else if ch 8 sbuf append hex 0 x00FF return sbuf toString public static String unescape String s StringBuffer sbuf new StringBuffer int i 0 int len s length while i len int ch s charAt i if ch map to sbuf append else if A ch else if a ch else if 0 ch else if ch ch unreserved as it was ch ch ch ch ch ch ch sbuf append char ch else if ch int cint 0 if u s charAt i 1 XX map to ascii XX cint cint 4 val s charAt i 1 cint cint 4 val s charAt i 2 i 2 else uXXXX map to unicode XXXX cint cint 4 val s charAt i 2 cint cint 4 val s charAt i 3 cint cint 4 val s charAt i 4 cint cint 4 val s charAt i 5 i 5 sbuf append char cint i return sbuf toString public static void main String args String stest 黄修群1234 abcd System out println stest System out println escape stest System out println unescape escape stest java JavaScript escape unescape 编码的 Java 实现 author jackyz keep this copyright info while using this method by free public class Escape private final static String hex 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF private final static byte val 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x00 0 x01 0 x02 0 x03 0 x04 0 x05 0 x06 0 x07 0 x08 0 x09 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x0A 0 x0B 0 x0C 0 x0D 0 x0E 0 x0F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3F 0 x3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uffer int len s length for int i 0 i len i int ch s charAt i if ch space map to sbuf append else if A ch else if a ch else if 0 ch else if ch ch unreserved as it was ch ch ch ch ch ch ch sbuf append char ch else if ch 8 sbuf append hex 0 x00FF return sbuf toString public static String unescape S

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论