有时候在前台传数据到后台时,回发送莫名的乱码问题,编码转来转去也不行,最后只用了此方法: 先在前台用js将汉字打散成 Ascii 组成的数字,放入hidden字段,在后台得到后使用java的方法再转回来! 具体如下: public static String t1(String s) {// ASCII转换为字符串 if ("".equals(s.trim()) || s == null) { return null; } String[] chars = s.split(" "); String string = ""; try { for (int i = 0; i < chars.length; i++) { string = string + (char) Integer.parseInt(chars[i]); // System.out.println(chars[i] + " " + (char) // Integer.parseInt(chars[i])); } } catch (Exception e) { System.out.println("Error:t1"); e.printStackTrace(); } System.out.println("字符串:" + string); return string; } public static String t2(String s) {// 字符串转换为ASCII码 // String s="新年快乐!";//字符串 if ("".equals(s.trim()) || s == null) { return null; } char[] chars = s.toCharArray(); // 把字符中转换为字符数组 String string = (int) chars[0] + ""; try { for (int i = 1; i < chars.length; i++) {// 输出结果 string = string + " " + (int) chars[i]; // System.out.println(" "+chars[i]+" "+(int)chars[i]); } } catch (Exception e) { System.out.println("Error:t2"); e.printStackTrace(); } System.out.println("ASCII:" + string); return string; } public static void main(String[] args) throws UnsupportedEncodingException { String t2 = t2("郭");// 字符串转换为ASCII码 t1("22899"); } /** *js: string --> ascii * *var dd = trim(myform.real_name.value); var string=dd.charCodeAt(0); for(var i=1;i<dd.length;i++) { string =string+" "+dd.charCodeAt(i); } alert(string); myform.user_cn_name.value=string; * */