您的位置 首页 java

java中判断字符串是否乱码的方法

在IE中GET方式提交会产生乱码 但有两个入口都会走这同一段代码

固不能直接转码,所以要进行判断传过来的该值是不是乱码

可用以下方式验证:

java.nio.charset.Charset.forName("GBK").newEncoder().canEncode("测试") //判断是不是GBK编码 即是否乱码
//使用request.getQueryString()获取通过URL传过来的值 有可能是乱码 如:q=éæ¥String s = request.getQueryString();   if(s!=null){	if(!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(s))){		try {			s = new String(request.getQueryString().getBytes("ISO-8859-1"),"UTF-8");		} catch (UnsupportedEncodingException e) {			e.printStackTrace();		}		}	}

文章来源:智云一二三科技

文章标题:java中判断字符串是否乱码的方法

文章地址:https://www.zhihuclub.com/15444.shtml

关于作者: 智云科技

热门文章

网站地图