天天看點

GET url傳參中文亂碼問題的解決【好用】

GET url傳參中文亂碼問題的解決

百度了很多種方式,最後的解決辦法終于嘗試出來了:

方法一

get方式送出的參數編碼,隻支援iso8859-1編碼。是以,如果裡面有中文。在背景就需要轉換編碼:

String userName = request.getParameter("userName");
userName = new String(userName.getBytes("iso8859-1"), "UTF-8");
      

注:如果你的頁面編碼集為utf-8,則按照此方法設定,如果是gbk,則将utf-8改成gbk即可(該方法我已經自己嘗試過,可行)

方法二

修改tomcat的server.xml檔案:

在如下代碼中增加兩個配置(URIEncoding="UTF-8" useBodyEncodingForURI="true"):

修改前:

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="200000" redirectPort="8443" />
      

修改後:

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="200000" redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>
      

(嘻嘻,該方法待驗證,不過可以一試,萬一你的可以呢)

繼續閱讀