• 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>
  • 相关阅读:
    四则运算
    软工与我
    四则运算结对作业
    《构建之法》读第四、十七章收获
    2016012088四则运算
    构建之法第一、二、十六章
    我的软件工程之路
    小学四则运算结对项目报告【GUI】
    构建之法4,17章读书笔记
    2016012095+小学四则远算练习软件项目报告
  • 原文地址:https://www.cnblogs.com/GIScore/p/7999339.html
Copyright © 2020-2023  润新知