Groovy 中文乱码问题处理


一个简单的 url get 发现json中有中文乱码 :
def get = new URL("https://httpbin.org/get").openConnection();
def getRC = get.getResponseCode();
println(getRC);
if (getRC.equals(200)) {
println(get.getInputStream().getText());
}


groovy 有个类可以处理流字节 编码
IOGroovyMethods 就这个类 
print(IOGroovyMethods.getText(get.getInputStream(), "utf-8"));

经过处理后无中文乱码

当然可以在groovy 环境中进行编码设定 :


Configuration config = new CompilerConfiguration();
config.setSourceEncoding("UTF-8");
// 设置该GroovyClassLoader的父ClassLoader为当前线程的加载器(默认)
GroovyClassLoader groovyClassLoader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), config);

 

在去加载groovy 代码也就没有问题了 。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM