java中文乱码解决之道(八)—–解决URL中文乱码问题

我们主要通过两种情势提交向服务器发送要求:URL、表单。而表单情势1般都不会出现乱码问题,乱码问题主要是在URL上面。通过前面几篇博客的介绍我们知道URL向服务器发送要求编码进程实在是实在太混乱了。不同的操作系统、不同的阅读器、不同的网页字符集,将致使完全不同的编码结果。如果程序员要把每种结果都斟酌进去,是否是太恐怖了?有无办法,能够保证客户端只用1种编码方法向服务器发出要求?

有!这里我主要提供以下几种方法

1、javascript

使用javascript编码不给阅读器插足的机会,编码以后再向服务器发送要求,然后在服务器中解码。在掌握该方法的时候,我们需要料及javascript编码的3个方法:escape()、encodeURI()、encodeURIComponent()。

escape

采取SIO Latin字符集对指定的字符串进行编码。所有非ASCII字符都会被编码为%xx格式的字符串,其中xx表示该字符在字符集中所对应的16进制数字。例如,格式对应的编码为%20。它对应的解码方法为unescape()。

201501150002

事实上escape()不能直接用于URL编码,它的真正作用是返回1个字符的Unicode编码值。比如上面“我是cm”的结果为%u6211%u662Fcm,其中“我”对应的编码为6211,“是”的编码为662F,“cm”编码为cm。

注意,escape()不对"+"编码。但是我们知道,网页在提交表单的时候,如果有空格,则会被转化为+字符。服务器处理数据的时候,会把+号处理成空格。所以,使用的时候要谨慎。

encodeURI

对全部URL进行编码,它采取的是UTF⑻格式输出编码后的字符串。不过encodeURI除ASCII编码外对1些特殊的字符也不会进行编码如:! @ # $& * ( ) = : / ; ? + ‘。

201501150003

encodeURIComponent

把URI字符串采取UTF⑻编码格式转化成escape格式的字符串。相对encodeURI,encodeURIComponent会更加强大,它会对那些在encodeURI()中不被编码的符号(; / ? : @ & = + $ , #)统统会被编码。但是encodeURIComponent只会对URL的组成部份进行个别编码,而不用于对全部URL进行编码。对应解码函数方法decodeURIComponent。

固然我们1般都是使用encodeURI方来进行编码操作。所谓的javascript两次编码后台两次解码就是使用该方法。javascript解决该问题有1次转码、两次转码两种解决方法。

1次转码

javascript转码:

var url = ‘<s:property value="webPath" />/ShowMoblieQRCode.servlet?name=我是cm’;
window.location.href = encodeURI(url);

转码后的URL:http://127.0.0.1:8080/perbank/ShowMoblieQRCode.servlet?name=%E6%88%91%E6%98%AFcm

后台处理:

String name = request.getParameter("name");
System.out.println("前台传入参数:" + name);
name = new String(name.getBytes("ISO⑻859⑴"),"UTF⑻");
System.out.println("经过解码后参数:" + name);

输出结果:

前台传入参数:??????cm
经过解码后参数:我是cm

2次转码

javascript

var url = ‘<s:property value="webPath" />/ShowMoblieQRCode.servlet?name=我是cm’;
window.location.href = encodeURI(encodeURI(url));

转码后的url:http://127.0.0.1:8080/perbank/ShowMoblieQRCode.servlet?name=%25E6%2588%2591%25E6%2598%25AFcm

后台处理:

String name = request.getParameter("name");
System.out.println("前台传入参数:" + name);
name = URLDecoder.decode(name,"UTF⑻");
System.out.println("经过解码后参数:" + name);

输出结果:

前台传入参数:E68891E698AFcm

经过解码后参数:我是cm


filter

使用过滤器,过滤器LZ提供两种,第1种设置编码,第2种直接在过滤器中进行解码操作。

过滤器1

该过滤器是直接设置request的编码格式的。

public class CharacterEncoding implements Filter {

private FilterConfig config ;
String encoding = null;

public void destroy() {
config = null;
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}

public void init(FilterConfig config) throws ServletException {
this.config = config;
//获得配置参数
String str = config.getInitParameter("encoding");
if(str!=null){
encoding = str;
}
}

}

配置:

<!– 中文过滤器的配置 –>
<filter>
<filter-name>chineseEncoding</filter-name>
<filter-class>com.test.filter.CharacterEncoding</filter-class>

<init-param>
<param-name>encoding</param-name>
<param-value>utf⑻</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>chineseEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

过滤器2

该过滤器在处理方法中将参数直接进行解码操作,然后将解码后的参数重新设置到request的attribute中。

public class CharacterEncoding implements Filter {
protected FilterConfig filterConfig ;
String encoding = null;

public void destroy() {
this.filterConfig = null;
}

/**
* 初始化
*/
public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}

/**
* 将 inStr 转为 UTF⑻ 的编码情势
*
* @param inStr 输入字符串
* @return UTF – 8 的编码情势的字符串
* @throws UnsupportedEncodingException
*/
private String toUTF(String inStr) throws UnsupportedEncodingException {
String outStr = "";
if (inStr != null) {
outStr = new String(inStr.getBytes("iso⑻859⑴"), "UTF⑻");
}
return outStr;
}

/**
* 中文乱码过滤处理
*/
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse, FilterChain chain) throws IOException,
ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;

// 取得要求的方式 (1.post or 2.get), 根据不同要求方式进行不同处理
String method = request.getMethod();
// 1. 以 post 方式提交的要求 , 直接设置编码为 UTF⑻
if (method.equalsIgnoreCase("post")) {
try {
request.setCharacterEncoding("UTF⑻");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
// 2. 以 get 方式提交的要求
else {
// 取出客户提交的参数集
Enumeration<String> paramNames = request.getParameterNames();
// 遍历参数集取出每一个参数的名称及值
while (paramNames.hasMoreElements()) {
String name = paramNames.nextElement(); // 取出参数名称
String values[] = request.getParameterValues(name); // 根据参数名称取出其值
// 如果参数值集不为空
if (values != null) {
// 遍历参数值集
for (int i = 0; i < values.length; i++) {
try {
// 回圈顺次将每一个值调用 toUTF(values[i]) 方法转换参数值的字元编码
String vlustr = toUTF(values[i]);
values[i] = vlustr;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
// 将该值以属性的情势藏在 request
request.setAttribute(name, values);
}
}

}
// 设置响应方式和支持中文的字元集
response.setContentType("text/html;charset=UTF⑻");

// 继续履行下1个 filter, 无1下个 filter 则履行要求
chain.doFilter(request, response);
}
}

配置:

<!– 中文过滤器的配置 –>
<filter>
<filter-name>chineseEncoding</filter-name>
<filter-class>com.test.filter.CharacterEncoding</filter-class>
</filter>

<filter-mapping>
<filter-name>chineseEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

其他

1、设置pageEncoding、contentType

<%@ page language="java" contentType="text/html;charset=UTF⑻" pageEncoding="UTF⑻"%>

2、设置tomcat的URIEncoding

在默许情况下,tomcat服务器使用的是ISO⑻859⑴编码格式来编码的,URIEncoding参数对get要求的URL进行编码,所以我们只需要在tomcat的server.xml文件的<Connector>标签中加上URIEncoding="utf⑻"便可。


—–原文出自:http://cmsblogs.com/?p=1526,请尊重作者辛苦劳动成果,转载说明出处.

—–个人站点:http://cmsblogs.com

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » java中文乱码解决之道(八)—–解决URL中文乱码问题

28 评论

  1. avlosulfon 100mg cost allegra uk perindopril 4mg tablet

  2. order colchicine 0.5mg pills generic gloperba no deposit casino

  3. clarithromycin us biaxin oral meclizine cost

  4. free spins no deposit casino casino slot casino online games

  5. buy doxycycline 200mg for sale buy aralen pill oral medrol cost

  6. order tadacip 20mg for sale tadacip tablet amoxicillin online

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡