页面传入后台的参数 乱码问题处理
2011-03-30 10:06
357 查看
JS里JSON传入时,data:'name='+escape(escape("大大大"));
action类中要调用unescape(name)进行解码 将%u5927%u5927%u5927转为“大大大”存入数据库
public static String unescape(String src) {
if (src == null)
return null;
StringBuffer tmp = new StringBuffer();
tmp.ensureCapacity(src.length());
int lastPos = 0, pos = 0;
char ch;
while (lastPos < src.length()) {
pos = src.indexOf("%", lastPos);
if (pos == lastPos) {
if (src.charAt(pos + 1) == 'u') {
ch = (char) Integer.parseInt(src
.substring(pos + 2, pos + 6), 16);
tmp.append(ch);
lastPos = pos + 6;
} else if (src.charAt(pos + 1) == ' '
|| src.charAt(pos + 1) == ';') {
tmp.append(src.substring(pos, pos + 1));
lastPos = pos + 1;
} else {
ch = (char) Integer.parseInt(src
.substring(pos + 1, pos + 3), 16);
tmp.append(ch);
lastPos = pos + 3;
}
} else {
if (pos == -1) {
tmp.append(src.substring(lastPos));
lastPos = src.length();
} else {
tmp.append(src.substring(lastPos, pos));
lastPos = pos;
}
}
}
return tmp.toString();
}
action类中要调用unescape(name)进行解码 将%u5927%u5927%u5927转为“大大大”存入数据库
public static String unescape(String src) {
if (src == null)
return null;
StringBuffer tmp = new StringBuffer();
tmp.ensureCapacity(src.length());
int lastPos = 0, pos = 0;
char ch;
while (lastPos < src.length()) {
pos = src.indexOf("%", lastPos);
if (pos == lastPos) {
if (src.charAt(pos + 1) == 'u') {
ch = (char) Integer.parseInt(src
.substring(pos + 2, pos + 6), 16);
tmp.append(ch);
lastPos = pos + 6;
} else if (src.charAt(pos + 1) == ' '
|| src.charAt(pos + 1) == ';') {
tmp.append(src.substring(pos, pos + 1));
lastPos = pos + 1;
} else {
ch = (char) Integer.parseInt(src
.substring(pos + 1, pos + 3), 16);
tmp.append(ch);
lastPos = pos + 3;
}
} else {
if (pos == -1) {
tmp.append(src.substring(lastPos));
lastPos = src.length();
} else {
tmp.append(src.substring(lastPos, pos));
lastPos = pos;
}
}
}
return tmp.toString();
}
相关文章推荐
- jsp页面传入后台Action乱码问题解决方案
- js页面之间以及vue $http请求向后台传递参数中文乱码问题解决
- JavaScript利用URL向后台传入中文参数乱码问题解决之道
- 关于jsp/html页面向后台传输参数时会出现乱码问题
- 解决页面传到后台含中文参数时会乱码问题
- web前端页面与后台交互时发生中文参数传递出现乱码问题
- 前台页面使用utf-8编码,url中包含中文参数时后台获取参数乱码问题解决
- JavaScript利用URL向后台传入中文参数乱码问题解决之道!
- springmvc Jsp页面向后台传递中文参数乱码问题
- JavaScript利用URL向后台传入中文参数乱码问题解决之道!
- JavaScript利用URL向后台传入中文参数乱码问题解决之道
- JavaScript利用URL向后台传入中文参数乱码问题解决之道
- 解决前端地址参数传入后台乱码问题
- JavaScript利用URL向后台传入中文参数乱码问题解决之道!
- 后台 与 js 处理Get方式提交的中文参数乱码问题
- JavaScript利用URL向后台传入中文参数乱码问题解决之道!
- 前后端分离的j2ee的web项目,使用vue方式,即{{}}方式批量传入前端参数,遇到一个一个问题,页面上一开始有{{}},怎么处理?
- jsp页面处理参数文字乱码
- struts2表单提交的中文参数后台乱码问题解决
- 关于Struts中jsp页面上传字符参数变乱码的问题