spring get方法 中文(UTF-8)亂碼


問題:

前端用Get方法進行如下請求:

在瀏覽器中輸入:http://localhost:8080/dmaList/ExportBySQL?sql=&names=分區級別&size=10&currentpage=1

后端方法接收代碼如下:

    @RequestMapping(value="/ExportSQL",produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public Boolean  ExportMonitorStationInfoBySQL(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="sql") String sql,@RequestParam(value="size") int size,@RequestParam(value="names") String names,@RequestParam(value="currentpage") int currentPage)
    {
        try {
            names = new String(names.getBytes("ISO-8859-1"),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
        int offset=(currentPage-1)*size;
        int totalCount=dmaServcie.getDmaCountByAdvance(sql);
        List<WLCS_DMAExt>data= dmaServcie.getDmaByPageByAdvance(sql, size, offset);
        Page<WLCS_DMAExt> result=new Page<WLCS_DMAExt>(currentPage, size, totalCount, data);
        return dmaServcie.ExportMonitorStationInfo(response, names, data);
    }

結果參數names亂碼,如下圖所示。

解決辦法:

使用names = new String(names.getBytes("ISO-8859-1"),"UTF-8");

上面的代碼已經把解決辦法寫出來了,轉換后的結果如下圖所示:

PS:

項目中使用了如下方法(在web.xml中添加)解決亂碼問題,結果不行:

  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <async-supported>true</async-supported>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/</url-pattern>
  </filter-mapping>
  <filter>

 


免責聲明!

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



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