由写Grails过滤不良信息的Service引发的中文乱码问题
来源:互联网 发布:sql round 编辑:程序博客网 时间:2024/05/01 06:02
由写Grails过滤不良信息的Service引发的中文乱码问题
在一个Grails项目里面,我想写一个过滤不良信息的Service,而将过滤的规则放置在xml文件之中。
以下xml文件放置不良信息,包含单词、词组和句法,支持正则表达式:
<filter>
<words>
<word>fuck</word>
<word>反人民</word>
<word>去死吧</word>
<word>kill</word>
<word>bitch</word>
</words>
<phrases>
<phrase>ass hole</phrase>
<phrase>beat you</phrase>
</phrases>
<grammars>
<grammar>杀死*你</grammar>
<grammar>kick .*ass</grammar>
<grammar>impud.nce</grammar>
</grammars>
</filter>
<words>
<word>fuck</word>
<word>反人民</word>
<word>去死吧</word>
<word>kill</word>
<word>bitch</word>
</words>
<phrases>
<phrase>ass hole</phrase>
<phrase>beat you</phrase>
</phrases>
<grammars>
<grammar>杀死*你</grammar>
<grammar>kick .*ass</grammar>
<grammar>impud.nce</grammar>
</grammars>
</filter>
然后是一个完成过滤功能的Service:
class FilterService ...{
def private static node = new XmlParser().parse(new File('./config/filter.xml'))
static String filter(String source) ...{
def substitution = node.substitution.text()
def sensitive = [node.words.word*.text().join('|'),
node.phrases.phrase*.text().join('|'),
node.grammars.grammar*.text().join('|')].join('|')
if(!source || !sensitive)return source
return source.replaceAll(sensitive,substitution)
}
}
def private static node = new XmlParser().parse(new File('./config/filter.xml'))
static String filter(String source) ...{
def substitution = node.substitution.text()
def sensitive = [node.words.word*.text().join('|'),
node.phrases.phrase*.text().join('|'),
node.grammars.grammar*.text().join('|')].join('|')
if(!source || !sensitive)return source
return source.replaceAll(sensitive,substitution)
}
}
就这样的代码,结果在Grails运行时出现中文问题,并且不止是页面,在filter方法里面就已经无法正确显示中文,而Service中同样的代码在普通Groovy应用程序中就没有中文问题。
按照以前J2EE开发的经验,尝试使用如下方法:
String resultString = new String(testString.getBytes("iso-8859-1"),"UTF-8")
依旧乱码……
再次尝试:增加定义的运行参数-Dfile.encoding=UTF-8
无效……
xml文件中加入
<?xml version="1.0" encoding="UTF-8"?>
仍然无效……
琢磨了半天,得出解决方案:
譬如打算将整个项目编码统一成UTF-8格式的,我使用Eclipse开发,我的Eclipse默认的的编码是GBK的,那么这个可以保持不变,在项目上单击右键,选择Properties中的Resource,将Text file encoding设置成UTF-8。
注意:这样的后果是项目groovy等文件中原本使用GBK的中文会变成乱码!因此请选择合适的编码。
之后删除运行参数-Dfile.encoding=UTF-8,为什么使用了自定义运行编码参数反而出乱码,我也不知道。
再做一则改动
//将原来的
def node = new XmlParser().parse(new File('./config/filter.xml'))
//改为
def node = new XmlParser().parseText(new File('./config/filter.xml').getText())
def node = new XmlParser().parse(new File('./config/filter.xml'))
//改为
def node = new XmlParser().parseText(new File('./config/filter.xml').getText())
最后,页面上面老规矩,加上<meta http-equiv="Content-Type" content="text/html; charset=GBK" />就可以了。
如果你的编码是GBK的,一样处理。
- 由写Grails过滤不良信息的Service引发的中文乱码问题
- 由webdings引发的乱码问题
- 由get请求引发的中文乱码整理
- jsp中过滤不良信息问题
- 由Typedef引发的问题
- 由UseSubmitBehavior引发的问题
- 由static引发的问题
- 由引用引发的问题
- 由LaunchMode引发的问题
- FileWrite写中文乱码的问题
- BufferedWriter.write()写中文乱码的问题
- 关于tomcat或jetty布署grails时ajax中文乱码的问题
- 由++操作引发的问题
- 由const引发的版本控制问题
- 由一个问题引发的思考
- 由py2exe引发的问题及解决方案
- 由object不能比较引发的问题
- 由 EditorBrowableAttribute 引发的一些问题
- 微软STL,hash_map 的效率问题
- 百团大战之谜:历史上为什么引起那么多的批评?
- “不依靠个别开发高手”的误解
- 疯狂的程序员37
- ASP编程入门进阶(十八):FSO组件之文件操作(上)
- 由写Grails过滤不良信息的Service引发的中文乱码问题
- [转帖]名牌运动鞋正货和仿鞋的知识
- OpenBSD 4.2安装指南(2)
- 8皇后(tiger语言描述)
- rs.open 与conn.execute()的区别
- 新版本又开始了......
- 疯狂的程序员38
- HTTP method GET is not supported by this URL
- 素数遍历方法比较(Find the prime number, with performance monitoring function)