博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Tomcat 改服务器编码(Java 修改字符串编码格式)
阅读量:7113 次
发布时间:2019-06-28

本文共 1657 字,大约阅读时间需要 5 分钟。

对于客户端发来的汉字,我们一般需要转码:

------------------------------------------------------------------------------------

request.setCharacterEncoding("UTF-8");//这样设置客户机发来数据文字格式只对post方式有效

String line = request.getParameter("username");  

System.out.println(line);

-------------------------------------------------------------

String line = request.getParameter("username");

line=(new String(line.getBytes("iso8859-1"),"UTF-8"));//对于get方式,只能这样了。

-------------------------------------------------------------------------------------

有没有办法不用这么麻烦?有,配置Tomcat服务器的server.xml 中的connector

先看一下API:

URIEncoding

This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used.

 

意思是如果不配置这个URIEncoding 那么采用默认的ISO-8859-1

     <Connector port="8080" protocol="HTTP/1.1"

               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8"/>

这样配置即可。

 

 

 

还有一个方法:

useBodyEncodingForURI

This specifies if the encoding specified in contentType should be used for URI query parameters, instead of using the URIEncoding. This setting is present for compatibility with Tomcat 4.1.x, where the encoding specified in the contentType, or explicitly set using Request.setCharacterEncoding method was also used for the parameters from the URL. The default value is false.

 

     <Connector port="8080" protocol="HTTP/1.1"

               connectionTimeout="20000"
               redirectPort="8443" useBodyEncodingForURI="true"/>

这样一改,那么:

request.setCharacterEncoding("UTF-8");//这样设置客户机发来数据文字格式就不只对post方式有效了,对get方式也有效。

 

 

Tomcat 的log乱码解决方式:

catalina.bat 中找到这一句

set LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties"      在后边加上:-Dfile.encoding="UTF-8" 

转载地址:http://ryghl.baihongyu.com/

你可能感兴趣的文章
web程序员标准环境之DreamWeaver【推荐】
查看>>
springMvc源码学习之:利用springMVC随时随地获取HttpServletRequest等对象
查看>>
无限分页
查看>>
iOS - UIColor
查看>>
Java最最常用的100个类排序(非官方)
查看>>
C#如何控制方法的执行时间,超时则强制退出方法执行
查看>>
【Python】模块之subprocess
查看>>
由一条报警信息发现的一系列问题
查看>>
Oracle Executable Binary Mismatch Detected
查看>>
Mysql Innodb中的Linux native异步I/O(一) 内存结构的初始化
查看>>
WM Activate Storage Bin Type Search(十四)
查看>>
nim的引用和指针
查看>>
DirectUI: Become windowless
查看>>
Python 数据结构_队列
查看>>
NAS数据迁移初探
查看>>
打破医院围墙 数字化平台之上的想象力
查看>>
Teradata首席分析官Bill Franks:数据分析变革犹如一场工业革命
查看>>
Linux下安装并使用Java开发opencv的配置
查看>>
AdTime: DMC量身定制的企业数据分析师
查看>>
《数字逻辑设计与计算机组成》一2.3 规范表达式
查看>>