java web返回中文亂碼問題及解決
java web返回中文亂碼
ajax返回中文亂碼問題
在瀏覽器按F12查看數(shù)據(jù)包
可以看到charset為 iso-8859-1,這是spring處理的編碼,需要在controller對(duì)應(yīng)映射方法上添加
produces = {“text/html;charset=utf-8”}
java中文亂碼,編碼識(shí)別測(cè)試匯總
1.手機(jī)顯示中文:GBK,UTF-8正常,ISO-8859-1亂碼。
2.寫入內(nèi)容到txt:UTF-8轉(zhuǎn)GBK,直接stream.write(str.getBytes(StrCharset.GBK));無效。
發(fā)現(xiàn)前面多了個(gè)問號(hào)?直接刪。暫時(shí)這樣處理了。
if(StrCharset.getEncoding(str).equals(StrCharset.ISO_8859_1))
stream.write(str.getBytes(StrCharset.ISO_8859_1));
else if(StrCharset.getEncoding(str).equals(StrCharset.UTF_8))
{
try{
byte[] b=str.getBytes(StrCharset.GBK);
stream.write(b,1,b.length-1);
//stream.write(new String(str.getBytes("GBK"),"GBK").getBytes());
}catch(Exception e)
{
stream.write(str.getBytes(StrCharset.GBK));
}
}
else
stream.write(str.getBytes(StrCharset.GBK));import java.nio.charset.Charset;
public class Encoding
{
public static String getEncoding(String str)
{
String encode;
encode = "UTF-16";
try
{
if(str.equals(new String(str.getBytes(), encode)))
{
return encode;
}
}
catch(Exception ex) {}
encode = "ASCII";
try
{
if(str.equals(new String(str.getBytes(), encode)))
{
return "字符串<< " + str + " >>中僅由數(shù)字和英文字母組成,無法識(shí)別其編碼格式";
}
}
catch(Exception ex) {}
encode = "ISO-8859-1";
try
{
if(str.equals(new String(str.getBytes(), encode)))
{
return encode;
}
}
catch(Exception ex) {}
encode = "GB2312";
try
{
if(str.equals(new String(str.getBytes(), encode)))
{
return encode;
}
}
catch(Exception ex) {}
encode = "UTF-8";
try
{
if(str.equals(new String(str.getBytes(), encode)))
{
return encode;
}
}
catch(Exception ex) {}
/*
*......待完善
*/
return "未識(shí)別編碼格式";
}
public static void main(String[] args)
{
//獲取系統(tǒng)默認(rèn)編碼
System.out.println("系統(tǒng)默認(rèn)編碼:" + System.getProperty("file.encoding")); //查詢結(jié)果GBK
//系統(tǒng)默認(rèn)字符編碼
System.out.println("系統(tǒng)默認(rèn)字符編碼:" + Charset.defaultCharset()); //查詢結(jié)果GBK
//操作系統(tǒng)用戶使用的語言
System.out.println("系統(tǒng)默認(rèn)語言:" + System.getProperty("user.language")); //查詢結(jié)果zh
System.out.println();
String s1 = "hi, nice to meet you!";
String s2 = "hi, 我來了!";
System.out.println(getEncoding(s1));
System.out.println(getEncoding(s2));
}
}
測(cè)試結(jié)果
// java獲取字符串編碼格式
public static String getEncoding(String str) {
String encode = "GB2312";
try {
if (str.equals(new String(str.getBytes(encode), encode))) { // 判斷是不是GB2312
String s = encode;
return s; // 是的話,返回“GB2312“,以下代碼同理
}
} catch (Exception exception) {
}
encode = "ISO-8859-1";
try {
if (str.equals(new String(str.getBytes(encode), encode))) { // 判斷是不是ISO-8859-1
String s1 = encode;
return s1;
}
} catch (Exception exception1) {
}
encode = "UTF-8";
try {
if (str.equals(new String(str.getBytes(encode), encode))) { // 判斷是不是UTF-8
String s2 = encode;
return s2;
}
} catch (Exception exception2) {
}
encode = "GBK";
try {
if (str.equals(new String(str.getBytes(encode), encode))) { // 判斷是不是GBK
String s3 = encode;
return s3;
}
} catch (Exception exception3) {
}
return "unknown"; // 如果都不是,說明輸入的內(nèi)容不屬于常見的編碼格式。
}各種編碼都顯示出去看看
System.out.println("中文");
System.out.println("中文".getBytes());
System.out.println("中文".getBytes("GB2312"));
System.out.println("中文".getBytes("ISO8859_1"));
System.out.println(new String("中文".getBytes()));
System.out.println(new String("中文".getBytes(), "GB2312"));
System.out.println(new String("中文".getBytes(), "ISO8859_1"));
System.out.println(new String("中文".getBytes("GB2312")));
System.out.println(new String("中文".getBytes("GB2312"), "GB2312"));
System.out.println(new String("中文".getBytes("GB2312"), "ISO8859_1"));
System.out.println(new String("中文".getBytes("ISO8859_1")));
System.out.println(new String("中文".getBytes("ISO8859_1"), "GB2312"));
System.out.println(new String("中文".getBytes("ISO8859_1"), "ISO8859_1"));//判斷當(dāng)前字符串的編碼格式
if(destination.equals(new String(destination.getBytes("iso8859-1"), "iso8859-1")))
{
destination=new String(destination.getBytes("iso8859-1"),"utf-8");
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java連接MySQL數(shù)據(jù)庫實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了java連接MySQL數(shù)據(jù)庫實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-06-06
Springboot處理跨域的實(shí)現(xiàn)方式(附Demo)
這篇文章主要介紹了Springboot處理跨域的實(shí)現(xiàn)方式(附Demo),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
淺談Java動(dòng)態(tài)代理的實(shí)現(xiàn)
最近,小組同事做代碼改造時(shí),使用到了動(dòng)態(tài)代理,自己閱讀時(shí),發(fā)現(xiàn)對(duì)代理這種設(shè)計(jì)模式都不怎么清楚,導(dǎo)致理解代碼也很困難 自己唯一能看懂的,大概就是handler中的invoke方法 ,文中作出了非常詳細(xì)的介紹,需要的朋友可以參考下2021-05-05
詳解基于MybatisPlus兩步實(shí)現(xiàn)多租戶方案
這篇文章主要介紹了詳解基于MybatisPlus兩步實(shí)現(xiàn)多租戶方案,本文分兩步,通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Spring boot 整合CXF開發(fā)web service示例
這篇文章主要介紹了Spring boot 整合CXF開發(fā)web service示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
使用SpringBoot+InfluxDB實(shí)現(xiàn)高效數(shù)據(jù)存儲(chǔ)與查詢
InfluxDB 是一個(gè)開源的時(shí)間序列數(shù)據(jù)庫,特別適合處理帶有時(shí)間戳的監(jiān)控?cái)?shù)據(jù)、指標(biāo)數(shù)據(jù)等,下面詳細(xì)介紹如何在 Spring Boot 項(xiàng)目中集成 InfluxDB 實(shí)現(xiàn)高效數(shù)據(jù)存儲(chǔ)與查詢功能,需要的朋友可以參考下2025-08-08
java實(shí)現(xiàn)ssh連接服務(wù)器的方法步驟
本文主要介紹了java實(shí)現(xiàn)ssh連接服務(wù)器的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09

