流年似水博客开通了,本站主要是写关于Web和大数据方面内容,正在更新中,欢迎大家光临!
  1. 文章:97 篇
  2. 总浏览:69,662 次
  3. 评论:22条
  4. 最后更新:2020-06-08
  5. 分类目录:39 个

JavaWeb之中文乱码问题

Web基础 xy@xy 838℃ 14评论

解决中文乱码问题

字符集

1.  字符(Character)是各种文字和符号的总称,包括各个国家文字、标点符号、图形符号、数字等。字符集(Character set)是多个字符的集合。

2.  任何数据在计算机内部实际上都是以二进制的形式保存的,所以我们看得见的字符本质上都是以二进制的形式保存的。而字符集实际上就是看得见的字符和二进制数之间的一张对应表。

汉字

编码方式

编码

二进制

‘中’

GB2312

D6D0

1101 0110-1101 0000

UTF-16

4E2D

0100 1110-0010 1101

UTF-8

E4B8AD

1110 0100-1011 1000-1010 1101

编码与解码

 

1.  编码:将字符转换为二进制数

‘中’ → E4B8AD → 1110 0100-1011 1000-1010 1101

2.   解码:将二进制数转换为字符

1110 0100-1011 1000-1010 1101 → E4B8AD → ‘中’

3.   乱码:编码与解码使用的字符集不一致就会产生乱码。

如下图:使用 UTF-8 编码,使用GBK 解码

image1.png

所以,解决乱码问题的根本方法就是统一编码和解码的字符集

在Web应用中统一字符集

1.  请求参数中含有中文

·         请求由浏览器发送给服务器

o    由浏览器编码

§  浏览器使用HTML页面指定的字符集编码,只需要将HTML页面的字符集指定为UTF-8即可

o    由服务器解码

§  默认字符集是IOS-8859-1

o    解决方案

·         对于POST请求:post请求在Servlet中解码,默认的字符集是ISO-8859-1,不支持中文,所以我们需要修改Servlet的字符集为UTF-8。使用request.setCharacterEncoding("UTF-8");设置request对象使用UTF-8字符集对请求体中的内容进行解码。但需要注意的是,这个操作一定要在调用getParameter()方法之前进行。

·         对于GET请求:GET请求是在URL地址栏中传递请求参数的,它会被Tomcat服务器自动解码,而Tomcat服务器默认的字符集也是ISO-8859-1,

所以我们需要修改Tomcat服务器的字符集为UTF-8。由于请求参数是包含在请求行内的,无法通过request对象进行设置,所以需要在Server.xml文件中修改Connector标签,添加URIEncoding="utf-8"属性。

image2.png

在eclipse中的Tomcat镜像中修改即可生效。但如果将来删除当前Tomcat镜像,重新关联Tomcat,则会使设置失效,所以在Tomcat配置文件中最好也进行配置。

 

2.   服务器响应中含有中文

·         响应由服务器发送个浏览器

o    由服务器编码

§  默认使用ISO-8859-1进行编码

o    由浏览器解码

§  默认使用GBK进行解码

o    解决方案

·         方法1:设置响应头

response.setHeader("Content-Type","text/html;charset=utf-8");

·         方法2:设置响应的内容类型

response.setContentType("text/html;charset=utf-8");

·         通过这种方式可以在响应头中告诉浏览器响应体的编码方式是UTF-8;同时服务器也会采用该字符集进行编码

但需要注意的是,两种方法一定要在response.getWriter()之前进行。

 下载文件过程中乱码问题

            //使用该方式谷歌、IE浏览器好使,火狐不好使

             fileName = URLEncoder.encode(fileName, "UTF-8");

            //但是火狐使用Base64编码,得需要调用如下代码:

            fileName = "=?utf-8?b?"+new BASE64Encoder().encode(fileName.getBytes())+"?=";

            //设置响应头

            response.setHeader("Content-Disposition", "attachment; filename="+fileName);

转载请注明:流年似水 » JavaWeb之中文乱码问题

喜欢 (5)or分享 (0)

Warning: copy(https://cn.gravatar.com/avatar/?s=54&d=%2Fwp-content%2Fthemes%2Fyusi1.0%2Fimg%2Fdefault.png&r=g): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /usr/share/nginx/html/timewentby/wp-content/themes/yusi1.0/functions.php on line 239

Warning: copy(/wp-content/themes/yusi1.0/img/default.png): failed to open stream: No such file or directory in /usr/share/nginx/html/timewentby/wp-content/themes/yusi1.0/functions.php on line 243
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(14)个小伙伴在吐槽

  1. Warning: copy(https://cn.gravatar.com/avatar/ecfa0f1c1d4eb131ca8fd210bb7bd372?s=54&d=%2Fwp-content%2Fthemes%2Fyusi1.0%2Fimg%2Fdefault.png&r=g): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /usr/share/nginx/html/timewentby/wp-content/themes/yusi1.0/functions.php on line 239
    我的测试评论
    l, xy2019-08-08 21:51 Reply

  2. Warning: copy(https://cn.gravatar.com/avatar/ecfa0f1c1d4eb131ca8fd210bb7bd372?s=54&d=%2Fwp-content%2Fthemes%2Fyusi1.0%2Fimg%2Fdefault.png&r=g): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /usr/share/nginx/html/timewentby/wp-content/themes/yusi1.0/functions.php on line 239
    是的呢
    l, xy2019-08-08 21:52 Reply

  3. Warning: copy(https://cn.gravatar.com/avatar/ecfa0f1c1d4eb131ca8fd210bb7bd372?s=54&d=%2Fwp-content%2Fthemes%2Fyusi1.0%2Fimg%2Fdefault.png&r=g): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /usr/share/nginx/html/timewentby/wp-content/themes/yusi1.0/functions.php on line 239
    对不对呀
    l, xy2019-08-08 21:52 Reply

  4. Warning: copy(https://cn.gravatar.com/avatar/ecfa0f1c1d4eb131ca8fd210bb7bd372?s=54&d=%2Fwp-content%2Fthemes%2Fyusi1.0%2Fimg%2Fdefault.png&r=g): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /usr/share/nginx/html/timewentby/wp-content/themes/yusi1.0/functions.php on line 239
    fly了
    l, xy2019-08-08 21:57 Reply

  5. Warning: copy(https://cn.gravatar.com/avatar/ecfa0f1c1d4eb131ca8fd210bb7bd372?s=54&d=%2Fwp-content%2Fthemes%2Fyusi1.0%2Fimg%2Fdefault.png&r=g): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /usr/share/nginx/html/timewentby/wp-content/themes/yusi1.0/functions.php on line 239
    测试
    l, xy2019-08-08 21:58 Reply

  6. Warning: copy(https://cn.gravatar.com/avatar/ecfa0f1c1d4eb131ca8fd210bb7bd372?s=54&d=%2Fwp-content%2Fthemes%2Fyusi1.0%2Fimg%2Fdefault.png&r=g): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /usr/share/nginx/html/timewentby/wp-content/themes/yusi1.0/functions.php on line 239
    在测下
    l, xy2019-08-08 22:00 Reply

  7. Warning: copy(https://cn.gravatar.com/avatar/ecfa0f1c1d4eb131ca8fd210bb7bd372?s=54&d=%2Fwp-content%2Fthemes%2Fyusi1.0%2Fimg%2Fdefault.png&r=g): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /usr/share/nginx/html/timewentby/wp-content/themes/yusi1.0/functions.php on line 239
    我的
    l, xy2019-08-08 22:01 Reply

  8. Warning: copy(https://cn.gravatar.com/avatar/ecfa0f1c1d4eb131ca8fd210bb7bd372?s=54&d=%2Fwp-content%2Fthemes%2Fyusi1.0%2Fimg%2Fdefault.png&r=g): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /usr/share/nginx/html/timewentby/wp-content/themes/yusi1.0/functions.php on line 239
    废了
    l, xy2019-08-08 22:02 Reply

  9. Warning: copy(https://cn.gravatar.com/avatar/ecfa0f1c1d4eb131ca8fd210bb7bd372?s=54&d=%2Fwp-content%2Fthemes%2Fyusi1.0%2Fimg%2Fdefault.png&r=g): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /usr/share/nginx/html/timewentby/wp-content/themes/yusi1.0/functions.php on line 239
    黑秀秀hi而徐徐徐,杜拉拉升职记呀,呼啦啦啦啦啦啦啦啦啦啦啦阿里阿拉山口精神分裂解耦if大局观我我打局欧冠一都给减掉降低了肌肤胜利的减肥 靳绥东if技术都交付iOSU盾覅偶
    l, xy2019-08-08 22:02 Reply

  10. Warning: copy(https://cn.gravatar.com/avatar/908c510169af4fefc0c04b61dc9534b7?s=54&d=%2Fwp-content%2Fthemes%2Fyusi1.0%2Fimg%2Fdefault.png&r=g): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /usr/share/nginx/html/timewentby/wp-content/themes/yusi1.0/functions.php on line 239
    帮你测一下
    前端小白兔2019-09-18 17:30 Reply

  11. Warning: copy(https://cn.gravatar.com/avatar/908c510169af4fefc0c04b61dc9534b7?s=54&d=%2Fwp-content%2Fthemes%2Fyusi1.0%2Fimg%2Fdefault.png&r=g): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /usr/share/nginx/html/timewentby/wp-content/themes/yusi1.0/functions.php on line 239
    再测一下
    前端小白兔2019-09-18 17:31 Reply

  12. Warning: copy(https://cn.gravatar.com/avatar/908c510169af4fefc0c04b61dc9534b7?s=54&d=%2Fwp-content%2Fthemes%2Fyusi1.0%2Fimg%2Fdefault.png&r=g): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /usr/share/nginx/html/timewentby/wp-content/themes/yusi1.0/functions.php on line 239
    测下表情 😎
    前端小白兔2019-09-18 17:31 Reply

  13. Warning: copy(https://cn.gravatar.com/avatar/908c510169af4fefc0c04b61dc9534b7?s=54&d=%2Fwp-content%2Fthemes%2Fyusi1.0%2Fimg%2Fdefault.png&r=g): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /usr/share/nginx/html/timewentby/wp-content/themes/yusi1.0/functions.php on line 239
    我没有头像!
    前端小白兔2019-09-18 17:32 Reply

    • Warning: copy(https://cn.gravatar.com/avatar/ecfa0f1c1d4eb131ca8fd210bb7bd372?s=54&d=%2Fwp-content%2Fthemes%2Fyusi1.0%2Fimg%2Fdefault.png&r=g): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /usr/share/nginx/html/timewentby/wp-content/themes/yusi1.0/functions.php on line 239
      我盡快修復,謝謝!
      l, xy2019-09-18 18:43 Reply