页面设置编码: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:?????????????
?????????????
?????????????
?????????????
其余所有设置均为: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:?????????????
?????????????
?????????????
?????????????
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);
<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>
试试在struts.xml 中写<constant name="struts.i18n.encoding" value="gb2312"></constant>
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);
}
在Strut2的配置文件中加入这句,我以前就是这么解决的,高版本的是没有这个问题的
2:还有配置中文过滤器
3:如果可以进行post提交就进行post提交
如果按照次三步走肯定不会有中文问题。