zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    Heapsort 堆排序算法详解(Java实现)
    GIve Me A Welcome Hug!
    linux系统救援模式拯救mv libc.so.6文件后无法使用命令的悲剧
    RabbitMQ集群部署
    使用Xshell通过堡垒机登录服务器
    dubbo + zookeeper环境部署
    zookeeper集群部署
    zabbix-3.0.1 添加微信报警
    zabbix-3.0.1结合grafana绘图
    Centos7.2安装zabbix3.0.1简要
  • 原文地址:https://www.cnblogs.com/GIScore/p/7999339.html
Copyright © 2011-2022 走看看