地址栏传递中文参数乱码在js里用escape转码


Posted in Javascript onAugust 28, 2013

乱码,乱码,头大,烦,话说设置Tomcat后可以解决,但有的时候不管用了,这时,我们可以考虑转码方式不直接传中文,在js里用escape转码,记得需要用两次,如:

var str=escape("这是中文参数");

str=escape(str);

xxx.do?name=str;

在后台通过一个类的方法再转回来:

Escape.unescape(request.getParameter("name"));

Escape类如下:

package com.laneasy.hy.util; /** 
* js编转码java实现类. 
* 
* @author yangjinde 
*/ 
public class Escape { 
/** The Constant hex. */ 
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" }; 
/** The Constant val. */ 
private final static byte[] val = { 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x00, 0x01, 
0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F }; 
/** 
* Escape. 
* 
* @param s 
* the s 
* 
* @return the string 
*/ 
/** 
* 编码 
* 
* @param s 
* @return 
*/ 
public static String escape(String s) { 
if (s != null) { 
StringBuffer sbuf = new StringBuffer(); 
int len = s.length(); 
for (int i = 0; i < len; i++) { 
int ch = s.charAt(i); 
if ('A' <= ch && ch <= 'Z') { 
sbuf.append((char) ch); 
} else if ('a' <= ch && ch <= 'z') { 
sbuf.append((char) ch); 
} else if ('0' <= ch && ch <= '9') { 
sbuf.append((char) ch); 
} else if (ch == '-' || ch == '_' || ch == '.' || ch == '!' 
|| ch == '~' || ch == '*' || ch == '\'' || ch == '(' 
|| ch == ')') { 
sbuf.append((char) ch); 
} else if (ch <= 0x007F) { 
sbuf.append('%'); 
sbuf.append(hex[ch]); 
} else { 
sbuf.append('%'); 
sbuf.append('u'); 
sbuf.append(hex[(ch >>> 8)]); 
sbuf.append(hex[(0x00FF & ch)]); 
} 
} 
return sbuf.toString(); 
} 
return null; 
} 
/** 
* Unescape. 
* 
* @param s 
* the s 
* 
* @return the string 
*/ 
/** 
* 解码 说明:本方法保证 不论参数s是否经过escape()编码,均能得到正确的“解码”结果 
* 
* @param s 
* @return 
*/ 
public static String unescape(String s) { 
if (s != null) { 
StringBuffer sbuf = new StringBuffer(); 
int i = 0; 
int len = s.length(); 
while (i < len) { 
int ch = s.charAt(i); 
if ('A' <= ch && ch <= 'Z') { 
sbuf.append((char) ch); 
} else if ('a' <= ch && ch <= 'z') { 
sbuf.append((char) ch); 
} else if ('0' <= ch && ch <= '9') { 
sbuf.append((char) ch); 
} else if (ch == '-' || ch == '_' || ch == '.' || ch == '!' 
|| ch == '~' || ch == '*' || ch == '\'' || ch == '(' 
|| ch == ')') { 
sbuf.append((char) ch); 
} else if (ch == '%') { 
int cint = 0; 
if ('u' != s.charAt(i + 1)) { 
cint = (cint << 4) | val[s.charAt(i + 1)]; 
cint = (cint << 4) | val[s.charAt(i + 2)]; 
i += 2; 
} else { 
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); 
} else { 
sbuf.append((char) ch); 
} 
i++; 
} 
return sbuf.toString(); 
} 
return null; 
} 
}
Javascript 相关文章推荐
IE6下JS动态设置图片src地址问题
Jan 08 Javascript
Javascript保存网页为图片借助于html2canvas库实现
Sep 05 Javascript
javascript入门教程基础篇
Nov 16 Javascript
Javascript 实现微信分享(QQ、朋友圈、分享给朋友)
Oct 21 Javascript
Bootstrap 轮播(Carousel)插件
Dec 26 Javascript
多个上传文件用js验证文件的格式和大小的方法(推荐)
Mar 09 Javascript
Angular中使用$watch监听object属性值的变化(详解)
Apr 24 Javascript
Vue.js手风琴菜单组件开发实例
May 16 Javascript
JS实现微信摇一摇原理解析
Jul 22 Javascript
Bootstrap + AngularJS 实现简单的数据过滤字符查找功能
Jul 27 Javascript
React 全自动数据表格组件——BodeGrid的实现思路
Jun 12 Javascript
微信小程序实现批量倒计时功能
Nov 01 Javascript
在JavaScript中实现类的方式探讨
Aug 28 #Javascript
实用的Jquery选项卡TAB示例代码
Aug 28 #Javascript
jQuery实现图片信息的浮动显示实例代码
Aug 28 #Javascript
JQuery筛选器全系列介绍
Aug 27 #Javascript
HTML Color Picker(js拾色器效果)
Aug 27 #Javascript
Js中的onblur和onfocus事件应用介绍
Aug 27 #Javascript
javascript:void(0)使用探讨
Aug 27 #Javascript
You might like
屏蔽浏览器缓存另类方法
2006/10/09 PHP
针对初学PHP者的疑难问答(1)
2006/10/09 PHP
php 三维饼图的实现代码
2008/09/28 PHP
php 面试碰到过的问题 在此做下记录
2011/06/09 PHP
ThinkPHP开发--使用七牛云储存
2017/09/14 PHP
PHP 使用二进制保存用户状态的实例
2018/01/29 PHP
Javascript &amp; DHTML 实例编程(教程)基础知识
2007/06/02 Javascript
firefox和IE系列的相关区别整理 以备后用
2009/12/28 Javascript
Javascript new关键字的玄机 以及其它
2010/08/25 Javascript
js实现表单多按钮提交action的处理方法
2015/10/24 Javascript
对象题目的一个坑 理解Javascript对象
2015/12/22 Javascript
基于JavaScript判断浏览器到底是关闭还是刷新(超准确)
2016/02/01 Javascript
jQuery实现点击水纹波动动画
2016/04/10 Javascript
js实现常用排序算法
2016/08/09 Javascript
jquery中用jsonp实现搜索框功能
2016/10/18 Javascript
node.js利用redis数据库缓存数据的方法
2017/03/01 Javascript
原生js仿浏览器滚动条效果
2017/03/02 Javascript
BootStrap Fileinput上传插件使用实例代码
2017/07/28 Javascript
vue ssr 实现方式(学习笔记)
2019/01/18 Javascript
vue 使用rules对表单字段进行校验的步骤
2020/12/25 Vue.js
[01:10:58]Spirit vs NB Supermajor小组赛 A组败者组决赛 BO3 第二场 6.2
2018/06/03 DOTA
python删除列表中重复记录的方法
2015/04/28 Python
Python实现保证只能运行一个脚本实例
2015/06/24 Python
python开发之list操作实例分析
2016/02/22 Python
Python OpenCV对本地视频文件进行分帧保存的实例
2019/01/08 Python
python numpy中cumsum的用法详解
2019/10/17 Python
tensorflow模型继续训练 fineturn实例
2020/01/21 Python
Python如何在循环内使用list.remove()
2020/06/01 Python
用HTML5制作烟火效果的教程
2015/05/12 HTML / CSS
AmazeUI中各种的导航式菜单与解决方法
2020/08/19 HTML / CSS
路由表示做什么用的?在linux环境中怎么来配置一条默认路由?
2013/06/07 面试题
药学专业毕业生求职信
2013/10/20 职场文书
俄罗斯商务邀请函
2014/01/26 职场文书
2015年元旦文艺汇演主持词
2014/03/26 职场文书
2014年教师节活动总结
2014/08/29 职场文书
公务员个人考察材料
2014/12/23 职场文书