Web开发的编码解决中文乱码
中文乱码有两个环节会出现
第一,从请求体中获得的数据
从请求体中获得的数据要为其进行编码,默认为ISO-8859-1,所以在使用getParameter()时先调用setCharacterEncoding("utf-8")方法对数据进行编码。。
这仅仅对post有效,对get时,因为在生成URL的时候已经对其编码,所以要使用String的构造方法对其重新编码,new String(str.getBytes("iso-8859-1"),"utf-8");
第二,响应给浏览器的数据
响应时,首先要设置给浏览器的文件MIME类型 setContentType("text/html;charset=utf-8")
其次需要设置响应的字符编码,setCharacterType("utf-8")
这样可以解决乱码问题
常见MIME类型
按照功能排列的MIME_type类型
类型/子类型 | 扩展名 |
---|---|
application/envoy | evy |
application/fractals | fif |
application/futuresplash | spl |
application/hta | hta |
application/internet-property-stream | acx |
application/mac-binhex40 | hqx |
application/msword | doc |
application/msword | dot |
application/octet-stream | * |
application/octet-stream | bin |
application/octet-stream | class |
application/octet-stream | dms |
application/octet-stream | exe |
application/octet-stream | lha |
application/octet-stream | lzh |
application/oda | oda |
application/olescript | axs |
application/pdf | |
application/pics-rules | prf |
application/pkcs10 | p10 |
application/pkix-crl | crl |
application/postscript | ai |
application/postscript | eps |
application/postscript | ps |
application/rtf | rtf |
application/set-payment-initiation | setpay |
application/set-registration-initiation | setreg |
application/vnd.ms-excel | xla |
application/vnd.ms-excel | xlc |
application/vnd.ms-excel | xlm |
application/vnd.ms-excel | xls |
application/vnd.ms-excel | xlt |
application/vnd.ms-excel | xlw |
application/vnd.ms-outlook | msg |
application/vnd.ms-pkicertstore | sst |
application/vnd.ms-pkiseccat | cat |
application/vnd.ms-pkistl | stl |
application/vnd.ms-powerpoint | pot |
application/vnd.ms-powerpoint | pps |
application/vnd.ms-powerpoint | ppt |
application/vnd.ms-project | mpp |
application/vnd.ms-works | wcm |
application/vnd.ms-works | wdb |
application/vnd.ms-works | wks |
application/vnd.ms-works | wps |
application/winhlp | hlp |
application/x-bcpio | bcpio |
application/x-cdf | cdf |
application/x-compress | z |
application/x-compressed | tgz |
application/x-cpio | cpio |
application/x-csh | csh |
application/x-director | dcr |
application/x-director | dir |
application/x-director | dxr |
application/x-dvi | dvi |
application/x-gtar | gtar |
application/x-gzip | gz |
application/x-hdf | hdf |
application/x-internet-signup | ins |
application/x-internet-signup | isp |
application/x-iphone | iii |
application/x-javascript | js |
application/x-latex | latex |
application/x-msaccess | mdb |
application/x-mscardfile | crd |
application/x-msclip | clp |
application/x-msdownload | dll |
application/x-msmediaview | m13 |
application/x-msmediaview | m14 |
application/x-msmediaview | mvb |
application/x-msmetafile | wmf |
application/x-msmoney | mny |
application/x-mspublisher | pub |
application/x-msschedule | scd |
application/x-msterminal | trm |
application/x-mswrite | wri |
application/x-netcdf | cdf |
application/x-netcdf | nc |
application/x-perfmon | pma |
application/x-perfmon | pmc |
application/x-perfmon | pml |
application/x-perfmon | pmr |
application/x-perfmon | pmw |
application/x-pkcs12 | p12 |
application/x-pkcs12 | pfx |
application/x-pkcs7-certificates | p7b |
application/x-pkcs7-certificates | spc |
application/x-pkcs7-certreqresp | p7r |
application/x-pkcs7-mime | p7c |
application/x-pkcs7-mime | p7m |
application/x-pkcs7-signature | p7s |
application/x-sh | sh |
application/x-shar | shar |
application/x-shockwave-flash | swf |
application/x-stuffit | sit |
application/x-sv4cpio | sv4cpio |
application/x-sv4crc | sv4crc |
application/x-tar | tar |
application/x-tcl | tcl |
application/x-tex | tex |
application/x-texinfo | texi |
application/x-texinfo | texinfo |
application/x-troff | roff |
application/x-troff | t |
application/x-troff | tr |
application/x-troff-man | man |
application/x-troff-me | me |
application/x-troff-ms | ms |
application/x-ustar | ustar |
application/x-wais-source | src |
application/x-x509-ca-cert | cer |
application/x-x509-ca-cert | crt |
application/x-x509-ca-cert | der |
application/ynd.ms-pkipko | pko |
application/zip | zip |
audio/basic | au |
audio/basic | snd |
audio/mid | mid |
audio/mid | rmi |
audio/mpeg | mp3 |
audio/x-aiff | aif |
audio/x-aiff | aifc |
audio/x-aiff | aiff |
audio/x-mpegurl | m3u |
audio/x-pn-realaudio | ra |
audio/x-pn-realaudio | ram |
audio/x-wav | wav |
image/bmp | bmp |
image/cis-cod | cod |
image/gif | gif |
image/ief | ief |
image/jpeg | jpe |
image/jpeg | jpeg |
image/jpeg | jpg |
image/pipeg | jfif |
image/svg+xml | svg |
image/tiff | tif |
image/tiff | tiff |
image/x-cmu-raster | ras |
image/x-cmx | cmx |
image/x-icon | ico |
image/x-portable-anymap | pnm |
image/x-portable-bitmap | pbm |
image/x-portable-graymap | pgm |
image/x-portable-pixmap | ppm |
image/x-rgb | rgb |
image/x-xbitmap | xbm |
image/x-xpixmap | xpm |
image/x-xwindowdump | xwd |
message/rfc822 | mht |
message/rfc822 | mhtml |
message/rfc822 | nws |
text/css http://www.dreamdu.com | css |
text/h323 | 323 |
text/html | htm |
text/html | html |
text/html | stm |
text/iuls | uls |
text/plain | bas |
text/plain | c |
text/plain | h |
text/plain | txt |
text/richtext | rtx |
text/scriptlet | sct |
text/tab-separated-values | tsv |
text/webviewhtml | htt |
text/x-component | htc |
text/x-setext | etx |
text/x-vcard | vcf |
video/mpeg | mp2 |
video/mpeg | mpa |
video/mpeg | mpe |
video/mpeg | mpeg |
video/mpeg | mpg |
video/mpeg | mpv2 |
video/quicktime | mov |
video/quicktime | qt |
video/x-la-asf | lsf |
video/x-la-asf | lsx |
video/x-ms-asf | asf |
video/x-ms-asf | asr |
video/x-ms-asf | asx |
video/x-msvideo | avi |
video/x-sgi-movie | movie |
x-world/x-vrml | flr |
x-world/x-vrml | vrml |
x-world/x-vrml | wrl |
x-world/x-vrml | wrz |
x-world/x-vrml | xaf |
x-world/x-vrml | xof |
Web开发的编码解决中文乱码的更多相关文章
- Web(Jsp+ Servlet)开发中如何解决中文乱码问题
1.中文乱码的成因 编码的字符集和解码的字符集不一致. 2.web开发过程中可能出现的乱码的位置及解决方案 ①request乱码 在向服务器传递数据时,所传递的中文有可能出现乱码. post请求(协议 ...
- Servlet中通过过滤器实现统一的手动编码(解决中文乱码)
1.web.xml片段: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi= ...
- 关于web.xml中配置Spring字符编码过滤器以解决中文乱码的问题
当出现中文乱码问题,Spring中可以利用CharacterEncodingFilter过滤器解决,如下代码所示: <!-- Spring字符编码过滤器:解决中文乱码问题 --> < ...
- java web 中有效解决中文乱码问题-pageEncoding与charset区别, response和request的setCharacterEncoding 区别
这里先写几个大家容易搞混的编码设置代码: 在jsp代码中的头部往往有这两行代码 pageEncoding是jsp文件本身的编码contentType的charset是指服务器发送给客户端时的内容编码J ...
- java web过滤器实际应用(解决中文乱码 html标签转义功能 敏感字符过滤功能)
转载地址:http://www.cnblogs.com/xdp-gacl/p/3952405.html 在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可 ...
- Spring编码过滤器:解决中文乱码
Spring编码过滤器:解决中文乱码 针对问题: 前台JSP页面和JAVA代码中使用了不同的字符集进行编码的时候就会出现表单提交的数据或者上传/下载中文名称文件出现乱码的问题: 解决方案: web.x ...
- SSH项目web.xml文件的常用配置【struts2的过滤器、spring监听器、解决Hibernate延迟加载问题的过滤器、解决中文乱码的过滤器】
配置web.xml(struts2的过滤器.spring监听器.解决Hibernate延迟加载问题的过滤器.解决中文乱码的过滤器) <!-- 解决中文乱码问题 --> <filter ...
- qt creator修改程序编码(解决中文乱码问题)的方法
qt creator修改程序编码(解决中文乱码问题)的方法 qt creator修改程序编码的功能有几处. 1.edit - select encoding 选择载入(显示)编码和储存编码,其中GB2 ...
- (原创)Linux下MySQL 5.5/5.6的修改字符集编码为UTF8(彻底解决中文乱码问题)
« CloudStack+XenServer详细部署方案(10):高级网络功能应用 (总结)CentOS Linux 5.x在GPT分区不能引导的解决方法 » 2013-1 11 (原创)Linux下 ...
随机推荐
- 存储过程和SQL语句比较
做为SQL存储过程和.NET的新手,下面的指导还是很有用的,自己这一段刚刚接触这些东西,搜集了一些相关的东西,能使新手较容易上手,当然啦,要精通和熟练应用,还是要看更多更深的资料的,高手请不要见笑.以 ...
- GO语言学习(十九)Go 错误处理
Go 错误处理 Go 语言通过内置的错误接口提供了非常简单的错误处理机制. error类型是一个接口类型,这是它的定义: type error interface { Error() string } ...
- SpringMVC之类型转换Converter
(转载:http://blog.csdn.net/renhui999/article/details/9837897) 1.1 目录 1.1 目录 1.2 前言 1.3 ...
- 展示C代码覆盖率的gcovr工具简单介绍及相关命令使用演示样例
(本人正在參加2015博客之星评选,诚邀你来投票,谢谢:username=zhouzxi">http://vote.blog.csdn.net/blogstar2015/candida ...
- 2017.1-TOP5 Android开源库
Colorful (Github) Colorful简单实用,通过这个开源库可以通过编码的方式来改变应用的主题,不再需要定义不同的style dependencies { compile 'com.g ...
- amazeui学习笔记--css(常用组件16)--文章页Article
amazeui学习笔记--css(常用组件16)--文章页Article 一.总结 1.基本使用:文章内容页的排版样式,包括标题.文章元信息.分隔线等样式. .am-article 文章内容容器 .a ...
- Linux CentOS PhpMyAdmin安装--转载
原文地址:https://www.centos.bz/2011/04/linux-centos-phpmyadmin-install/ 安装好PHP,Apache和MySQL程序后,为了管理MySQL ...
- Android Set与List之间转化
List list = new ArrayList(set); Set set = new HashSet(list); //但是有一点,转换当中可能要丢失数据,尤其是从list转换到set的时候,因 ...
- css函数——calc()和attr()
css也有函数?好吧,我孤陋寡闻了.这里记录一下学习情况. calc()函数 定义:用于动态计算长度值 支持版本:css3 运算符前后都需要保留一个空格,例如:width: calc(100% - 1 ...
- DBeaver笔记-快捷键篇
公司使用的是PostgreSQL数据库,可以使用pgAdmin或者DBeaver进行连接该数据库.个人更喜欢用DBeaver,因为其界面更加美观,操作也相对简单.对于习惯了eclipse的开发者来说, ...