在项目中有另外一系统请求我们的系统,我们会根据请求传入的入参数再进一步获取其它的参数,然后才sendRedirect到真正的请求上。在测试时发现,如果获取的其它参数中有中文汉字,再sendRedirect时出错,原因是地址栏上的中文汉字变成了乱码。
在网上看到说通过URLDecoder.decode(request.getParameter("param1"))即可,但由于我这里的参数比较多,且有些参数还是JSON格式的,不能简单的对URL进行URLDecoder.decode处理(因为整个请求中的:、{、}、//都会被编码),正确的做法是只对URL中的中文进行URLDecoder.decode处理。我的处理方法及测试如下:
import java.io.UnsupportedEncodingException; public class Main { public static void main(String[] args) { String str = "欢迎光临我的博客iteye,网址http://bijian1013.iteye.com/"; String resStr = getEncodeStr(str); System.out.println(resStr); } private static String getEncodeStr(String str) { StringBuffer resultStrBuf = new StringBuffer(); StringBuffer chineseStrBuf = new StringBuffer(); try { for (int i = 0; i < str.length(); i++) { if ((str.charAt(i) + "").getBytes().length > 1) { chineseStrBuf.append(str.charAt(i)); } else { if (chineseStrBuf.length() > 0) { resultStrBuf.append(java.net.URLEncoder.encode(chineseStrBuf.toString(), "UTF-8")); chineseStrBuf.delete(0, chineseStrBuf.length()); } resultStrBuf.append(str.charAt(i)); } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return resultStrBuf.toString(); } }
运行结果:
%E6%AC%A2%E8%BF%8E%E5%85%89%E4%B8%B4%E6%88%91%E7%9A%84%E5%8D%9A%E5%AE%A2iteye,%E7%BD%91%E5%9D%80http://bijian1013.iteye.com/
附一:Java如何从字符串中取出中文、Java如何从字符串中取出中文和数字,去掉其他字符
public class Main { public static void main(String[] args) { //Java如何从字符串中取出中文 System.out.println("深圳2015SH".replaceAll("[\\p{ASCII}]","")); System.out.println("深圳2015SH".replaceAll("[\\x00-\\x7F]","")); //Java如何从字符串中取出中文 String str = "欢迎光临我的博客iteye,网址http://bijian1013.iteye.com/"; StringBuffer sb = new StringBuffer(); for (int i = 0; i < str.length(); i++) { if ((str.charAt(i)+"").getBytes().length>1) { sb.append(str.charAt(i)); } } System.out.println(sb); //Java如何从字符串中取出中文 System.out.println("欢迎光临我的博客iteye,网址http://bijian1013.iteye.com/".replaceAll("[^\\u4E00-\\u9FA5]", "")); System.out.println("欢迎光临我的博客iteye,网址http://bijian1013.iteye.com/".replaceAll("[\\w]", "").replaceAll("\\p{Punct}", "")); //Java如何从字符串中取出中文和数字,去掉其他字符 String str1 = "^2^3da42b3中文sae34科e技b2字符2`~!@#$%^&*()_+-=[]{};':\",.<>/?\\93啊"; str1 = str1.replaceAll("[^0-9\\u4e00-\\u9fa5]", ""); System.out.println(str1); } }
运行结果:
深圳 深圳 欢迎光临我的博客,网址 欢迎光临我的博客网址 欢迎光临我的博客,网址 23423中文34科技2字符293啊
附二:检查输入的字符串中是否包含中文
import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 检查输入的字符串中是否包含中文 */ public class Main { static String regEx = "[\u4e00-\u9fa5]"; static Pattern pat = Pattern.compile(regEx); public static void main(String[] args) { String input = "Hell world!"; System.out.println(isContainsChinese(input));//false input = "hello world"; System.out.println(isContainsChinese(input));//false input = ",http://bijian1013.iteye.com/"; System.out.println(isContainsChinese(input));//false input = ",http://bijian1013.iteye.com/"; System.out.println(isContainsChinese(input));//false input = "[]%http://bijian1013.iteye.com/"; System.out.println(isContainsChinese(input));//false input = "【】%http://bijian1013.iteye.com/"; System.out.println(isContainsChinese(input));//false input = "中国http://bijian1013.iteye.com/"; System.out.println(isContainsChinese(input));//true } public static boolean isContainsChinese(String str) { Matcher matcher = pat.matcher(str); boolean flg = false; if (matcher.find()) { flg = true; } return flg; } }
运行结果:
false false false false false false true