java gbk轉utf8


我的情況:

沒有部署前的spring boot項目:前台請求的參數編碼格式是utf8
在打成jar包后:前台請求的參數編碼格式是gbk

 

代碼:

String reqUrl = "";
String str = new String(reqUrl.getBytes(), "gbk");
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
reqUrl = new String(bytes);


其實可以先判斷其編碼格式,再進行編碼格式的轉換更好
String iso8859 = new String(reqUrl.getBytes("iso8859-1"));
String gbk = new String(reqUrl.getBytes("gbk"));
if(iso8859.equals(reqUrl)){
String str = new String(reqUrl.getBytes(), "iso8859-1");
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
reqUrl = new String(bytes);
System.out.println("iso8859");
}else if(gbk.equals(reqUrl)){
String str = new String(reqUrl.getBytes(), "gbk");
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
reqUrl = new String(bytes);
System.out.println("gbk");
}


歡迎糾錯.........


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM