亂碼主要出現(xiàn)在" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運營 > 透徹分析和解決一切javaWeb項目亂碼問題

透徹分析和解決一切javaWeb項目亂碼問題

時間:2023-06-29 07:24:01 | 來源:網(wǎng)站運營

時間:2023-06-29 07:24:01 來源:網(wǎng)站運營

透徹分析和解決一切javaWeb項目亂碼問題:

前言

亂碼是我們在程序開發(fā)中經(jīng)常碰到且讓人頭疼的一件事,尤其是我們在做javaweb開發(fā),如果我們沒有清楚亂碼產(chǎn)生的原理,碰到亂碼問題了就容易摸不著頭腦,無從下手。

亂碼主要出現(xiàn)在兩部分,如下:

第一,瀏覽器通過表單提交到后臺,如果表單內(nèi)容有中文,那么后臺收到的數(shù)據(jù)可能會出現(xiàn)亂碼。

第二,后端服務(wù)器需要返回給瀏覽器數(shù)據(jù),如果數(shù)據(jù)中帶有中文,那么瀏覽器上可能會顯示亂碼。

接下來我們逐一分析亂碼產(chǎn)生的原因,以及如何解決亂碼問題。

一、后端收到瀏覽器提交的中文亂碼

這里又分為get請求和post請求。

get請求
get請求,請求參數(shù)中帶有中文,后臺接收會出現(xiàn)亂碼,原因是tomcat默認(rèn)編碼是“ISO-8859-1”,所以tomcat會使用“ISO-8859-1”對中文進(jìn)行編碼,該編碼不支持中文,所以后臺接收到就亂碼了。解決方式有兩種。

  1. param = new String(param.getBytes("ISO-8859-1"),"utf-8");
  2. 修改tomcat編碼為"utf-8",不建議使用這種方式。
post請求
post請求,出現(xiàn)亂碼的原因同get請求,解決方式比較簡單,如下:

request.setCharacterEncoding("utf-8");設(shè)置請求參數(shù)的編碼格式為“utf-8”,這樣就不會有問題了。

二、后端返回中文給瀏覽器發(fā)生亂碼

后端返回數(shù)據(jù)給瀏覽器,一般也有兩種形式,一種是response.getOutputStream(),一種是response.getWriter()。

兩者區(qū)別以及使用規(guī)則
因此,調(diào)用requonse.getWriter()方法時可實現(xiàn)文本字符串?dāng)?shù)據(jù)輸出,調(diào)用response.getOutputStream()方法可現(xiàn)實字節(jié)流數(shù)據(jù)的輸出。所以,如果要輸出圖片等二進(jìn)制數(shù)據(jù)時,需要使用response.getOutputStream。

注意,getOutputStream()和getWriter()不能同時使用,否則會拋出”getWriter() has already been called for this response“異常。

區(qū)別講完了,下面我們主要還是通過實踐分析下亂碼產(chǎn)生的原理。

response.getOutputStream().print()
返回英文數(shù)據(jù)就不說了,沒什么問題,看下返回中文是什么效果;

@RequestMapping("/helloworld.do")public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException { String str = "中國加油,杭州加油"; response.getOutputStream().print(str);}結(jié)果如下:







分析:

OutPutStream是輸出二進(jìn)制數(shù)據(jù)的,所以需要對字符串改成二進(jìn)制輸出,Tomcat使用的是"ISO8859-1"編碼對其進(jìn)行轉(zhuǎn)換,而中文對”ISO859-1“不支持,所以就拋異常了。

response.getOutputStream.write()
同樣的,我們再來看下輸出中文會怎么樣。

@RequestMapping("/helloworld.do")public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException { String str = "中國加油,杭州加油"; response.getOutputStream().write(str.getBytes());}頁面輸出結(jié)果如下:

涓浗鍔犳補(bǔ)錛屾姹夊姞娌?分析:

在java中,String的getBytes()方法是得到一個操作系統(tǒng)默認(rèn)的編碼格式的字節(jié)數(shù)組,我電腦的系統(tǒng)是macos,默認(rèn)編碼格式是utf-8,返回給瀏覽器是utf-8編碼格式的字節(jié)數(shù)組,但是瀏覽器默認(rèn)是"gbk"編碼解析,所以就亂碼了。

既然這樣,那我們換成“gb2312”編碼(gb2312編碼是gbk編碼的一種)試試呢?

@RequestMapping("/helloworld.do")public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException { String str = "中國加油,杭州加油"; response.getOutputStream().write(str.getBytes());}頁面輸出:

中國加油,杭州加油原理我們弄清楚了,但是在項目開發(fā)中,我們需要編碼統(tǒng)一,最常用的就是中文字符編碼"UTF-8",可是按照我們的理解,如果我們直接response.getOutputStream().write(str.getBytes("utf-8"));肯定會亂碼,我們需要用某種方式,告訴瀏覽器,你要用我指定的“utf-8”編碼接受我返回的中文。response.setContentType("text/html;charset=UTF-8")這樣就完事了,看看效果吧。

@RequestMapping("/helloworld.do")public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException { String str = "中國加油,杭州加油"; response.setContentType("text/html;charset=utf-8"); response.getOutputStream().write(str.getBytes("utf-8"));}頁面輸出:

中國加油,杭州加油
response.getWriter()
前面已經(jīng)總結(jié)過了,response.getWriter()跟response.getOutputStream()不一樣,outputStream是輸出二進(jìn)制的,writer是輸出字符串的。response.getWriter()輸出也有兩種方法,一種是print(),一種是write(),其實兩者在處理亂碼這一塊沒有什么區(qū)別,就不分開講述了。

示例:

@RequestMapping("/helloworld.do")public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException { String str = "中國加油,杭州加油"; response.getWriter().print(str);}頁面輸出:

?????????分析:

同樣的,Tomcat默認(rèn)的編碼是ISO 8859-1,當(dāng)我們輸出中文數(shù)據(jù)的時候,Tomcat會依據(jù)ISO 8859-1碼表給我們的數(shù)據(jù)編碼,中文不支持這個碼表呀,所以出現(xiàn)了亂碼。

這個時候response.setContentType("text/html;charset=UTF-8")又派上用場了。

@RequestMapping("/helloworld.do")public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException { String str = "中國加油,杭州加油"; response.setContentType("text/html;charset=utf-8"); response.getWriter().print(str);}頁面輸出:

中國加油,杭州加油在這里,response.setContentType("text/html;charset=UTF-8")做了兩件事,response.setCharacterEncoding("UTF-8");和response.setHeader("Content-Type", "text/html;charset=UTF-8");具體就是,第一,輸出中文”中國加油,杭州加油“的時候,對中文進(jìn)行”utf-8“編碼;第二,告訴瀏覽器,你也要用"utf-8"來顯示我返回的中文

最后

對于springMVC項目,如何解決亂碼問題呢?項目中一般會在web.xml中配置編碼過濾器。配置如下:

<filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>這樣能保證請求的參數(shù)按照指定的編碼格式進(jìn)行編碼,簡單翻看下過濾器源碼如下:

@Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) { request.setCharacterEncoding(this.encoding); if (this.forceEncoding) { response.setCharacterEncoding(this.encoding); } } filterChain.doFilter(request, response); }代碼中有兩處重要的地方值得注意,分別是request.setCharacterEncoding(this.encoding);和response.setCharacterEncoding(this.encoding);前者表示我們對請求過來的參數(shù)使用指定的"utf-8"進(jìn)行編碼,后者便是,返回給瀏覽器時,后端返回字符的編碼是“utf-8”。

好了,經(jīng)過以上分析是不是亂碼也沒有那么可怕了。只要明白其中的緣由,解決起來就是一行代碼或者幾行配置的事兒了,如果大家覺得有幫助,不妨點贊支持一下

關(guān)鍵詞:項目,分析,和解,透徹

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關(guān)閉