首页 >> 知识 >> Groovy 中文乱码问题处理

Groovy 中文乱码问题处理

一个简单的 url get 发现json中有中文乱码 : def get = new URL("http://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 代码也就没有问题了 。

网站地图