最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java web返回中文亂碼問題及解決

 更新時(shí)間:2023年05月25日 10:48:17   作者:瞌睡的貓00  
這篇文章主要介紹了java web返回中文亂碼問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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 png圖片修改像素rgba值的操作

    Java png圖片修改像素rgba值的操作

    這篇文章主要介紹了Java png圖片修改像素rgba值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • java連接MySQL數(shù)據(jù)庫實(shí)現(xià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)

    這篇文章主要介紹了Springboot處理跨域的實(shí)現(xiàn)方式(附Demo),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • 淺談Java動(dòng)態(tài)代理的實(shí)現(xiàn)

    淺談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
  • IDEA中maven無法下載源碼的解決方法

    IDEA中maven無法下載源碼的解決方法

    這篇文章主要為大家詳細(xì)介紹了當(dāng)IDEA中maven無法下載源碼時(shí)改如何解決,文中通過圖文為大家進(jìn)行了詳細(xì)講解,需要的小伙伴可以參考一下
    2023-08-08
  • 詳解基于MybatisPlus兩步實(shí)現(xiàn)多租戶方案

    詳解基于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示例

    這篇文章主要介紹了Spring boot 整合CXF開發(fā)web service示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • 基于Spring實(shí)現(xiàn)文件上傳功能

    基于Spring實(shí)現(xiàn)文件上傳功能

    這篇文章主要為大家詳細(xì)介紹了Spring實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • 使用SpringBoot+InfluxDB實(shí)現(xiàn)高效數(shù)據(jù)存儲(chǔ)與查詢

    使用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ù)器的方法步驟

    本文主要介紹了java實(shí)現(xiàn)ssh連接服務(wù)器的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-09-09

最新評(píng)論

安仁县| 曲周县| 永泰县| 永胜县| 永清县| 靖江市| 什邡市| 金塔县| 敖汉旗| 衡水市| 沙雅县| 遂宁市| 勃利县| 嘉兴市| 南和县| 台州市| 安泽县| 新民市| 台中市| 济南市| 买车| 教育| 陆丰市| 凌源市| 准格尔旗| 广平县| 廉江市| 桦南县| 东城区| 汕头市| 遵化市| 长武县| 界首市| 峨山| 两当县| 桃源县| 鱼台县| 林甸县| 沙雅县| 马关县| 东丰县|