(5) 中文处理专题

来源:互联网 发布:h5模板源码下载 编辑:程序博客网 时间:2024/05/16 07:13

中文乱码大致会出现在三种情况:
1. 表单提交
① GET
针对 URL 中的内容

 String name = new String            ( request.getParameter("username").getBytes("iso-8859-1") , "utf-8" );

GET 通过 URL 提交的数据,向服务器传递数据时,采用的是 iso 编码
当服务器接收到数据后,先解码成字节数组,然后在编码成 utf-8

② POST
针对 HTTP 中请求体中的内容

request.setCharacterEncoding("utf-8");

2 超链接

<a href="http://bai.com?name=你好">点击</a>

通过超链接传递数据时,编码方式和 GET 请求处理方式一致。

3 重定向
同理,用 GET 的处理方式

response.sendRedirect("http://bai.com?name=你好");



下载文件时,乱码解决:

String name = java.net.URLEncoder.encode("五环.mp3","utf-8");response.setHeader("Content-Disposition", "attachment;filename="+ name);
1 0
原创粉丝点击