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")
[解决办法]
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>
[解决办法]
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);
}
[解决办法]
我增减遇到过在jsp页面传中文到struts中,jsp页面的编码是UTF-8的,java是GBK的,然后在jsp页面用
- Java code
URLEncoder.encode(String 字段名,String 编码类型)