页面设置编码:gb2312
其余所有设置均为:utf-8
tomcat同样设URIEcoding:utf-8
现在要早Action中接受页面传过来的值
无论怎么转都是乱码:我试过的方法有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:?????????????
?????????????
?????????????
?????????????

解决方案 »

  1.   

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

  2.   

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

  3.   

    下面我我已经试过的方法:HttpServletRequest request = ServletActionContext.getRequest();
    System.out.println(request);
    request.setCharacterEncoding("gb2312");
    String str = new String(request.getParameter("msgContent").getBytes("GB2312"),"UTF-8");
    System.out.println(str);
    System.out.println("可以正常么1:" + this.getMsgContent());// 1
    System.out.println(this.getMsgContent());
    System.out.println("===============================无参数================================");
    System.out.println("可以正常么2:" + this.getMsgContent().getBytes());// 2
    System.out.println("可以正常么3:" + this.getMsgContent().getBytes("GBK"));// 3
    System.out.println("可以正常么3:" + this.getMsgContent().getBytes("UTF-8"));// 3
    System.out.println("可以正常么3:" + this.getMsgContent().getBytes("GB2312"));// 3
    System.out.println("可以正常么3:" + this.getMsgContent().getBytes("ISO-8859-1"));// 3
    System.out.println("===============================无参数================================");
    System.out.println("可以正常么8:"+ new String(this.getMsgContent().getBytes()));// 8
    System.out.println("可以正常么9:"+ new String(this.getMsgContent().getBytes(),"GBK"));// 9
    System.out.println("可以正常么9:"+ new String(this.getMsgContent().getBytes(),"UTF-8"));// 9
    System.out.println("可以正常么9:"+ new String(this.getMsgContent().getBytes(),"GB2312"));// 9
    System.out.println("可以正常么10:"+ new String(this.getMsgContent().getBytes(),"ISO-8859-1"));// 10
    System.out.println("===============================GB2312================================");
    System.out.println("可以正常么8:"+ new String(this.getMsgContent().getBytes("GB2312")));// 8
    System.out.println("可以正常么9:"+ new String(this.getMsgContent().getBytes("GB2312"),"GBK"));// 9
    System.out.println("可以正常么9:"+ new String(this.getMsgContent().getBytes("GB2312"),"UTF-8"));// 9
    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("=============================ISO-8859-1================================");
    System.out.println("可以正常么11:"+ new String(this.getMsgContent().getBytes("ISO-8859-1")));// 11
    System.out.println("可以正常么11:"+ new String(this.getMsgContent().getBytes("ISO-8859-1"),"GBK"));// 11
    System.out.println("可以正常么5:" + new String(this.getMsgContent().getBytes("ISO-8859-1"),"UTF-8"));// 5
    System.out.println("可以正常么12:"+ new String(this.getMsgContent().getBytes("ISO-8859-1"),"GB2312"));// 12
    System.out.println("可以正常么1:" + new String(this.getMsgContent().getBytes("ISO-8859-1"),"ISO-8859-1"));// 13
    System.out.println("=============================GBK================================");
    System.out.println("可以正常么11:"+ new String(this.getMsgContent().getBytes("GBK")));// 11
    System.out.println("可以正常么11:"+ new String(this.getMsgContent().getBytes("GBK"),"GBK"));// 11
    System.out.println("可以正常么5:" + new String(this.getMsgContent().getBytes("GBK"),"UTF-8"));// 5
    System.out.println("可以正常么12:"+ new String(this.getMsgContent().getBytes("GBK"),"GB2312"));// 12
    System.out.println("可以正常么1:" + new String(this.getMsgContent().getBytes("GBK"),"ISO-8859-1"));// 13
    System.out.println("=============================UTF-8================================");
    System.out.println("可以正常么11:"+ new String(this.getMsgContent().getBytes("UTF-8")));// 11
    System.out.println("可以正常么11:"+ new String(this.getMsgContent().getBytes("UTF-8"),"GBK"));// 11
    System.out.println("可以正常么5:" + new String(this.getMsgContent().getBytes("UTF-8"),"UTF-8"));// 5
    System.out.println("可以正常么12:"+ new String(this.getMsgContent().getBytes("UTF-8"),"GB2312"));// 12
    System.out.println("可以正常么1:" + new String(this.getMsgContent().getBytes("UTF-8"),"ISO-8859-1"));// 13
    System.out.println("=============================UTF-8================================");
    System.out.println("可以正常么11:"+ new String(this.getMsgContent().getBytes("GB18030")));// 11
    System.out.println("可以正常么11:"+ new String(this.getMsgContent().getBytes("GB18030"),"GBK"));// 11
    System.out.println("可以正常么5:" + new String(this.getMsgContent().getBytes("GB18030"),"UTF-8"));// 5
    System.out.println("可以正常么12:"+ new String(this.getMsgContent().getBytes("GB18030"),"GB2312"));// 12
    System.out.println("可以正常么1:" + new String(this.getMsgContent().getBytes("GB18030"),"ISO-8859-1"));// 13
    System.out.println("可以正常么1:" + new String(this.getMsgContent().getBytes("GB18030"),"GB18030"));// 13
    String stra = URLDecoder.decode(this.getMsgContent(), "utf-8");//
    String stra1 = URLDecoder.decode(this.getMsgContent(), "gbk");//
    String stra2 = URLDecoder.decode(this.getMsgContent(), "gb2312");//
    String stra3 = URLDecoder.decode(this.getMsgContent(), "iso-8859-1");//
    System.out.println(stra);
    System.out.println(stra1);
    System.out.println(stra2);
    System.out.println(stra3);
      

  4.   

    页面上也用utf-8呀,编码是要统一的
      

  5.   

    LZ可以再web.xml中试试这样的配置
    <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <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>
      

  6.   

    编码要统一。
    试试在struts.xml 中写<constant name="struts.i18n.encoding" value="gb2312"></constant> 
      

  7.   

    编码统一,不行了写个interceptor加上去。
      

  8.   

    public static byte[] decodeHex(char[] data) throws DecoderException {
    int len = data.length; if ((len & 0x1) != 0) {
    throw new DecoderException("Odd number of characters.");
    } byte[] out = new byte[len >> 1]; int i = 0;
    for (int j = 0; j < len; ++i) {
    int f = toDigit(data[j], j) << 4;
    ++j;
    f |= toDigit(data[j], j);
    ++j;
    out[i] = (byte) (f & 0xFF);
    } return out;
    } protected static int toDigit(char ch, int index) throws DecoderException {
    int digit = Character.digit(ch, 16);
    if (digit == -1) {
    throw new DecoderException("Illegal hexadecimal charcter " + ch
    + " at index " + index);
    }
    return digit;
    } public static void main(String[] args) throws DecoderException {
    String soruce = "xxx";
    byte[] a = decodeHex(soruce.toCharArray());
    soruce = new String(a);
    System.out.println("==="+soruce);
    }
      

  9.   

    在整个程序中还是使用同一种编码的好!UTF-8
      

  10.   

    我增减遇到过在jsp页面传中文到struts中,jsp页面的编码是UTF-8的,java是GBK的,然后在jsp页面用URLEncoder.encode(String 字段名,String 编码类型)即可
      

  11.   

    编码是要求统一的,建议全部使用utf-8就好
      

  12.   

    <constant name="struts.i18n.encoding" value="UTF-8" />
    在Strut2的配置文件中加入这句,我以前就是这么解决的,高版本的是没有这个问题的
      

  13.   

    1:一个工程中首先所有的编码要统一
    2:还有配置中文过滤器
    3:如果可以进行post提交就进行post提交
    如果按照次三步走肯定不会有中文问题。