当前位置: 代码迷 >> J2EE >> strut2中文乱码,该怎么处理
  详细解决方案

strut2中文乱码,该怎么处理

热度:124   发布时间:2016-04-22 01:34:12.0
strut2中文乱码
页面设置编码:gb2312
其余所有设置均为:utf-8
tomcat同样设URIEcoding:utf-8
现在要早Action中接受页面传过来的值
无论怎么转都是乱码:我试过的方法有
Java code
HttpServletRequest request = ServletActionContext.getRequest();        request.setCharacterEncoding("gb2312");        String str = request.getParameter("msgContent");        System.out.println(str);        System.out                .println(new String(this.getMsgContent().getBytes(), "utf-8"));        System.out                .println(new String(this.getMsgContent().getBytes(), "gb2312"));        System.out.println(new String(this.getMsgContent().getBytes(), "gbk"));        System.out.println("可以正常么1:" + this.getMsgContent());// 1        System.out.println("可以正常么2:" + this.getMsgContent().getBytes());// 2        System.out.println("可以正常么3:" + this.getMsgContent().getBytes("GB2312"));// 3        System.out.println("可以正常么4:"                + this.getMsgContent().getBytes("ISO-8859-1"));// 4        System.out.println("可以正常么5:"                + new String(this.getMsgContent().getBytes()));// 5        System.out.println("可以正常么6:"                + new String(this.getMsgContent().getBytes(), "GB2312"));// 6        System.out.println("可以正常么7:"                + new String(this.getMsgContent().getBytes(), "ISO-8859-1"));// 7        System.out.println("可以正常么8:"                + new String(this.getMsgContent().getBytes("GB2312")));// 8        System.out                .println("可以正常么9:"                        + new String(this.getMsgContent().getBytes("GB2312"),                                "GB2312"));// 9        System.out.println("可以正常么10:"                + new String(this.getMsgContent().getBytes("GB2312"),                        "ISO-8859-1"));// 10        System.out.println("可以正常么11:"                + new String(this.getMsgContent().getBytes("ISO-8859-1")));// 11        System.out                .println("====================================12=====================");        System.out.println("可以正常么12:"                + new String(this.getMsgContent().getBytes("ISO-8859-1"),                        "GB2312"));// 12        System.out                .println("====================================13=====================");        System.out.println("可以正常么1:"                + new String(this.getMsgContent().getBytes("ISO-8859-1"),                        "ISO-8859-1"));// 13        String stra = URLDecoder.decode(this.getMsgContent(), "utf-8");//        String stra1 = URLDecoder.decode(this.getMsgContent(), "gbk");//        String stra2 = URLDecoder.decode(this.getMsgContent(), "gb2312");//        System.out.println(stra);        System.out.println(stra1);        System.out.println(stra2);//运行结果如下:????????????????????????????????????????????????????可以正常么1:?????????????可以正常么2:[B@1cf806b可以正常么3:[B@11272ec可以正常么4:[B@36fc18可以正常么5:?????????????可以正常么6:?????????????可以正常么7:?????????????可以正常么8:?????????????可以正常么9:?????????????可以正常么10:?????????????可以正常么11:?????????????====================================12=====================可以正常么12:?????????????====================================13=====================可以正常么1:????????????????????????????????????????????????????


------解决方案--------------------
因为你页面设置了编码为gb2312, 所以你传递到后台的数据相当于用UTF-8 解析gb2312编码的数据,当然就会出现乱码,建议你在页面设置为UTF-8,这样对以后有好处,不会出现乱码。或者你把Action中的编码格式改为gb2312也可以。
------解决方案--------------------
试试这个

new String(this.getMsgContent().getBytes("UTF-8"), "GB2312")