亂碼是咱們在程序開發中常常碰到且讓人頭疼的一件事,尤爲是咱們在作javaweb開發,若是咱們沒有清楚亂碼產生的原理,碰到亂碼問題了就容易摸不着頭腦,無從下手。html
亂碼主要出如今兩部分,以下:java
第一,瀏覽器經過表單提交到後臺,若是表單內容有中文,那麼後臺收到的數據可能會出現亂碼。web
第二,後端服務器須要返回給瀏覽器數據,若是數據中帶有中文,那麼瀏覽器上可能會顯示亂碼。spring
接下來咱們逐一分析亂碼產生的緣由,以及如何解決亂碼問題。macos
這裏又分爲get請求和post請求。後端
get請求
get請求,請求參數中帶有中文,後臺接收會出現亂碼,緣由是tomcat默認編碼是「ISO-8859-1」,因此tomcat會使用「ISO-8859-1」對中文進行編碼,該編碼不支持中文,因此後臺接收到就亂碼了。解決方式有兩種。數組
param = new String(param.getBytes("ISO-8859-1"),"utf-8");
修改tomcat編碼爲"utf-8",不建議使用這種方式。瀏覽器
post請求
post請求,出現亂碼的緣由同get請求,解決方式比較簡單,以下:tomcat
request.setCharacterEncoding("utf-8");
設置請求參數的編碼格式爲「utf-8」,這樣就不會有問題了。服務器
後端返回數據給瀏覽器,通常也有兩種形式,一種是response.getOutputStream(),一種是response.getWriter()。
二者區別以及使用規則
所以,調用requonse.getWriter()方法時可實現文本字符串數據輸出,調用response.getOutputStream()方法可現實字節流數據的輸出。因此,若是要輸出圖片等二進制數據時,須要使用response.getOutputStream。
注意,getOutputStream()和getWriter()不能同時使用,不然會拋出」getWriter() has already been called for this response「異常。
區別講完了,下面咱們主要仍是經過實踐分析下亂碼產生的原理。
response.getOutputStream().print()
返回英文數據就不說了,沒什麼問題,看下返回中文是什麼效果;
@RequestMapping("/helloworld.do") public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException { String str = "中國加油,武漢加油"; response.getOutputStream().print(str); }
結果以下:
分析:
OutPutStream是輸出二進制數據的,因此須要對字符串改爲二進制輸出,Tomcat使用的是"ISO8859-1"編碼對其進行轉換,而中文對」ISO859-1「不支持,因此就拋異常了。
response.getOutputStream.write()
一樣的,咱們再來看下輸出中文會怎麼樣。
@RequestMapping("/helloworld.do") public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException { String str = "中國加油,武漢加油"; response.getOutputStream().write(str.getBytes()); }
頁面輸出結果以下:
涓浗鍔犳補錛屾奼夊姞娌�
分析:
在java中,String的getBytes()方法是獲得一個操做系統默認的編碼格式的字節數組,我電腦的系統是macos,默認編碼格式是utf-8,返回給瀏覽器是utf-8編碼格式的字節數組,可是瀏覽器默認是"gbk"編碼解析,因此就亂碼了。
既然這樣,那咱們換成「gb2312」編碼(gb2312編碼是gbk編碼的一種)試試呢?
@RequestMapping("/helloworld.do") public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException { String str = "中國加油,武漢加油"; response.getOutputStream().write(str.getBytes()); }
頁面輸出:
中國加油,武漢加油
原理咱們弄清楚了,可是在項目開發中,咱們須要編碼統一,最經常使用的就是中文字符編碼"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()
前面已經總結過了,response.getWriter()跟response.getOutputStream()不同,outputStream是輸出二進制的,writer是輸出字符串的。response.getWriter()輸出也有兩種方法,一種是print(),一種是write(),其實二者在處理亂碼這一塊沒有什麼區別,就不分開講述了。
示例:
@RequestMapping("/helloworld.do") public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException { String str = "中國加油,武漢加油"; response.getWriter().print(str); }
頁面輸出:
?????????
分析:
一樣的,Tomcat默認的編碼是ISO 8859-1,當咱們輸出中文數據的時候,Tomcat會依據ISO 8859-1碼錶給咱們的數據編碼,中文不支持這個碼錶呀,因此出現了亂碼。
這個時候**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");具體就是,第一,輸出中文」中國加油,武漢加油「的時候,對中文進行」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>
這樣能保證請求的參數按照指定的編碼格式進行編碼,簡單翻看下過濾器源碼以下:
@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);前者表示咱們對請求過來的參數使用指定的"utf-8"進行編碼,後者即是,返回給瀏覽器時,後端返回字符的編碼是「utf-8」。
好了,通過以上分析是否是亂碼也沒有那麼可怕了。只要明白其中的原因,解決起來就是一行代碼或者幾行配置的事兒了,若是你們以爲有幫助,不妨點贊支持一下?
若是你們以爲我寫的不錯、清晰易懂的話,能夠關注個人公衆號「灰太狼學爪哇」,不按期分享原創技術文章,與君共勉。
原文出處:https://www.cnblogs.com/xiaoming0601/p/12304418.html