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中遍歷集合的并發(fā)修改異常解決方案實(shí)例代碼
當(dāng)你遍歷集合的同時(shí),又往集合中添加或者刪除元素,就可能報(bào)并發(fā)修改異常,下面這篇文章主要給大家介紹了關(guān)于Java中遍歷集合的并發(fā)修改異常解決方案的相關(guān)資料,需要的朋友可以參考下2022-12-12
JVM內(nèi)存溢出和內(nèi)存泄漏的區(qū)別及說明
這篇文章主要介紹了JVM內(nèi)存溢出和內(nèi)存泄漏的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Java Maven settings.xml中私有倉庫配置詳解
這篇文章主要介紹了詳解Maven settings.xml配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2021-10-10
Java多線程知識(shí)點(diǎn)全面總結(jié)
這篇文章主要介紹了Java多線程知識(shí)點(diǎn)全面總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

