日期:2014-05-16  浏览次数:20588 次

Linux下的JSP乱码解决方法

我在JSP中用的是gb2312编码
而LINUX系统默认的是UTF-8
所以导致在LINUX下运行的网站,获取数据库中的中文全是乱码,具体解决:
1.在ROOT用户下
vi /etc/sysconfig/i18n
将原来的:
LANG="zh_CN.UTF-8"
SUPPORTED="zh_CN.UTF-8:zh_CN:zh"
SYSFONT="latarcyrheb-sun16"
改为
# vi /etc/sysconfig/i18n 修改该文件的内容 # 表示被注释了

#LANG="zh_CN.UTF-8"
#SUPPORTED="zh_CN.UTF-8:zh_CN:zh"
#SYSFONT="latarcyrheb-sun16"
LANG="zh_CN.GB2312"
LANGUAGE="zh_CN.GB2312:zh_CN"
SUPPORTED="zh_CN.GB2312:zh_CN:zh_CN.UTF-8"
SYSFONT="lat0-sun16"
SYSFONTACM="8859-15"
2.处理包含汉字字符的字符串时要指定gb2312或者GB2312编码
如: String caption = new String(caption.getBytes("iso-8859-1"), "gb2312");//传递的参数,指定编码

String templateContent = "";
FileInputStream fileinputstream = new FileInputStream(filePath);
// 读取模板文件
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes,"GB2312"); //指定编码