問題:
前端用Get方法進行如下請求:
在瀏覽器中輸入:http://localhost:8080/dmaList/ExportBySQL?sql=&names=分區級別&size=10¤tpage=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>