只要添加编码方式就可以解决POST中的乱码问题

 //1、解决POST乱码问题
        request.setCharacterEncoding("UTF-8");


        //2、获取username
        String username = request.getParameter("username");
        System.out.println(username);

使用GET却不能使用这种方法解决

复制代码
 String username = request.getParameter("username");
        //System.out.println(username);
        System.out.println("解决乱码问题前"+username);
        //3、GET,获取参数的方式:getQueryString
        //乱码原因:tomcat对url解码时使用的是默认字符集ISO-8859-1
        //3.1 先对乱码数据进行编码:转为字节数组
        byte[] bytes = username.getBytes(StandardCharsets.ISO_8859_1);
        //3.2字节数组解码
        username = new String(bytes,StandardCharsets.UTF_8);
        System.out.println("解决乱码问题后"+username);
复制代码

POST和GET方式都可以用这种方式提交

 

posted on   na2co3-  阅读(106)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix



点击右上角即可分享
微信分享提示