時間:2023-06-29 07:24:01 | 來源:網(wǎng)站運營
時間:2023-06-29 07:24:01 來源:網(wǎng)站運營
透徹分析和解決一切javaWeb項目亂碼問題:get請求get請求,請求參數(shù)中帶有中文,后臺接收會出現(xiàn)亂碼,原因是tomcat默認(rèn)編碼是“ISO-8859-1”,所以tomcat會使用“ISO-8859-1”對中文進(jìn)行編碼,該編碼不支持中文,所以后臺接收到就亂碼了。解決方式有兩種。
post請求post請求,出現(xiàn)亂碼的原因同get請求,解決方式比較簡單,如下:
request.setCharacterEncoding("utf-8");
設(shè)置請求參數(shù)的編碼格式為“utf-8”,這樣就不會有問題了。兩者區(qū)別以及使用規(guī)則
response.getOutputStream().print()返回英文數(shù)據(jù)就不說了,沒什么問題,看下返回中文是什么效果;
@RequestMapping("/helloworld.do")public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException { String str = "中國加油,杭州加油"; response.getOutputStream().print(str);}
結(jié)果如下:response.getOutputStream.write()同樣的,我們再來看下輸出中文會怎么樣。
@RequestMapping("/helloworld.do")public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException { String str = "中國加油,杭州加油"; response.getOutputStream().write(str.getBytes());}
頁面輸出結(jié)果如下:涓浗鍔犳補(bǔ)錛屾姹夊姞娌?
分析:@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);}
頁面輸出:?????????
分析:@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"來顯示我返回的中文。<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”。關(guān)鍵詞:項目,分析,和解,透徹
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。