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