Spring MVC的Post請求參數中文亂碼的原因&處理


一、項目配置:

  1. Spring 4.4.1-RELEASE
  2. Jetty 9.3.5
  3. JDK 1.8
  4. Servlet 3.1.0
  5. web.xml文件中沒有配置編解碼Filter

二、實際遇到的問題:
客戶端(比如java)發送post請求訪問接口,數據放在body里面,每個參數utf-8編碼。
從body里面取出的中文參數是亂碼。


下面是發送請求的代碼和服務端接收請求的代碼。

  • 客戶端代碼。
    這是一個真實的第三方訪問API的案例,這段代碼請求到PHP系統正常,請求到java系統就會出現亂碼。
    但是中文參數放到URL中解碼正常,放到請求體中就是亂碼。
    通過httpclient4.1發送Post請求如下:

    public static void postData(String sign, String timestamp) { // 創建默認的httpClient實例. CloseableHttpClient httpclient=null; String result=""; try { httpclient = HttpClients.createDefault(); String url = "http://example/api/entry"; HttpPost httpPost = new HttpPost(url); //設置請求和傳輸超時時間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(6000).setConnectTimeout(6000).build(); httpPost.setConfig(requestConfig); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("app_id", new StringBody("c5eb3ba8c0e7326559", Charset.forName("utf-8"))); entity.addPart("method", new StringBody("kdt.item.add", Charset.forName("utf-8"))); entity.addPart("timestamp", new StringBody(timestamp)); entity.addPart("format", new StringBody("json", Charset.forName("utf-8"))); entity.addPart("v", new StringBody("1.0", Charset.forName("utf-8"))); entity.addPart("sign", new StringBody(sign, Charset.forName("utf-8"))); entity.addPart("sign_method", new StringBody("md5", Charset.forName("utf-8"))); entity.addPart("cid", new StringBody("5000000", Charset.forName("utf-8"))); entity.addPart("tag_ids", new StringBody("0", Charset.forName("utf-8"))); entity.addPart("price", new StringBody("0.01", Charset.forName("utf-8"))); entity.addPart("title", new StringBody("測試", Charset.forName("utf-8"))); entity.addPart("desc", new StringBody("test1", Charset.forName("utf-8"))); //是否是虛擬商品。0為否,1為是。目前不支持虛擬商品 entity.addPart("is_virtual", new StringBody("0", Charset.forName("utf-8"))); entity.addPart("post_fee", new StringBody("0.0", Charset.forName("utf-8"))); //Sku的屬性串。格式:pText:vText;pText:vText,多個sku之間用逗號分隔,如:顏色:黃色;尺寸:M,顏色:黃色;尺寸:S。pText和vText文本中不可以存在冒號和分號以及逗號 entity.addPart("sku_properties", new StringBody("color:white", Charset.forName("utf-8"))); entity.addPart("sku_quantities", new StringBody("998,999", Charset.forName("utf-8"))); entity.addPart("sku_prices", new StringBody("0.01,0.02", Charset.forName("utf-8"))); entity.addPart("sku_outer_ids", new StringBody("null,null", Charset.forName("utf-8"))); //該商品的外部購買地址。當用戶購買環境不支持微信或微博支付時會跳轉到此地址 entity.addPart("buy_url", new StringBody("http://img.cdn.sb.hongware.com/1461836641703511.gif", Charset.forName("utf-8"))); entity.addPart("quantity", new StringBody("1998", Charset.forName("utf-8"))); //寶貝修改的時候需要這個參數 httpPost.setEntity(entity); CloseableHttpResponse response = httpclient.execute(httpPost); try { HttpEntity httpEntity = response.getEntity(); System.out.println(httpEntity.getContent()); InputStream content = httpEntity.getContent(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(content)); String line; while ( (line=bufferedReader.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } finally { response.close(); } } catch (Exception e) { e.printStackTrace(); } finally { try { httpclient.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
  • 服務端代碼如下
    為了簡化演示,我把參數提取代碼Map<String, String[]> parameterMap = request.getParameterMap()冗余在這個入口函數中,以便說明問題:

    @RequestMapping(value = "/api/entry", produces = "application/json;charset=utf-8") public DeferredResult<Object> sign(HttpServletRequest request,HttpServletResponse response) { DeferredResult<Object> deferredResult = new DeferredResult<>(); Map<String, String[]> parameterMap = request.getParameterMap(); String method = request.getParameter("method"); if (StringUtils.isEmpty(method)) { ResponseEntity<String> responseEntity = new ResponseEntity<String>( String.format(Constants.ERROR_RESPONSE, 50000, "service or method is null"), HttpStatus.valueOf(200)); deferredResult.setResult(responseEntity); return deferredResult; } int lastIndex = method.lastIndexOf("."); String service = method.substring(0, lastIndex); method = method.substring(lastIndex + 1); event.setService(service); event.setMethod(method); event.setResult(deferredResult); proxy.doAction(request,response,event); return deferredResult; }

服務端通過Map<String, String[]> parameterMap = request.getParameterMap()取出所有參數,傳進來title參數是亂碼!!

三、根本原因
Servlet 3.0規范中有關請求數據編碼的解釋如下:

當前很多瀏覽器並不發送帶Content-Type頭部的字符編碼標識符,它會把字符編碼的決定留在讀取HTTP請求的時候。如果客戶端沒有指明編碼,容器用來創建請求讀和解析POST數據的默認編碼必須是"ISO-8859-1"。然而,為了提示開發者客戶端沒有成功發送一個字符編碼,容器中getCharacterEncoding方法會返回null。
如果客戶端沒有設置字符編碼,並且請求數據使用了不同編碼而不是上述的默認編碼,程序將會出現中斷。為了糾正這種狀態,一個新的方法setCharacterEncoding(String enc) 被添加到ServletRequest接口。開發者調用這個方法能重寫容器提供的字符編碼。這個方法必須在解析request中任何post數據或者讀任何輸入之前調用。一旦數據已經被讀取,調用這個方法不會影響它的編碼。

另外一種相同的解釋:


網絡上同樣含義的解釋

四、3種解決方法

  1. 在web.xml中配置編解碼Filter
     <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> <async-supported>true</async-supported> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
    關於這段配置需要強調兩點:
    • web.xml中,這段配置要放在所有filter的最前面,否則會不生效,根本原因請見上述第三點的解釋。
    • 兩個初始化參數的作用,其實看這個Filter的源碼就一目了然,這兩個參數是用來決定是否要設置request和response中的編碼。源碼很簡潔:
       public class CharacterEncodingFilter extends OncePerRequestFilter { private String encoding; private boolean forceEncoding = false; public CharacterEncodingFilter() { } public void setEncoding(String encoding) { this.encoding = encoding; } public void setForceEncoding(boolean forceEncoding) { this.forceEncoding = forceEncoding; } 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); } }
  2. 設置Content-Type
    如果post請求方式是x-www-form-urlencoded,那么設置如下:
    Content-Type=application/x-www-form-urlencoded;charset=utf-8
    這樣通過request對象取body體里面的中文是正常的。
    這種方式有一點需要注意: 如果請求方式是multipart/form-data,如上設置會導致request取不到參數。Content-Type要與傳遞數據匹配(本文data)
  3. 手動編解碼
    比如參數title="測試",這樣取出來就是"測試"。
     String str = new String(request.getParameter("title").getBytes("iso-8859-1"), "utf-8");

綜上所有,最優雅的方式是第一種解決方案--通過框架的Filter去處理。
你僅專注於業務代碼就好。

參考資料

    1. ajax post data獲取不到數據
    2. Servlet 3.0規范
    3. HTTP Content-Type常用對照表
    4. Spring官網--Consumable Media Types章節
    5. ISO-8859-1
    6. ISO-8859-1為何能顯示中文
    7. 字符編碼
    8. Media Type


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM