spring get方法 中文(UTF-8)乱码
问题:
前端用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>
posted on 2017-12-07 15:36 Geography爱好者 阅读(2011) 评论(0) 编辑 收藏 举报