import java.io.UnsupportedEncodingException; /**
* 判断字符编码
*
* @author guyinyihun
*/
public class CharacterCodingUtil { private final static String ENCODE = "GBK"; /**
* 判断是否为ISO-8859-1
*
* @return
*/
public static boolean checkISO(String str) {
boolean flag = java.nio.charset.Charset.forName("ISO-8859-1").newEncoder().canEncode(str);
return flag;
} /**
* 判断是否为UTF-8
*
* @return
*/
public static boolean checkUTF(String str) { boolean flag = java.nio.charset.Charset.forName("UTF-8").newEncoder().canEncode(str);
return flag;
} public static boolean checkUnicode(String str) { boolean flag = java.nio.charset.Charset.forName("unicode").newEncoder().canEncode(str);
return flag;
} /**
* <p>
* Title: getEncoding
* </p>
* <p>
* Description: 判断字符编码
* </p>
*
* @param str
* @return
*/
public static String getEncoding(String str) {
String encode = "unicode";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s = encode;
return s;
}
} catch (Exception exception) {
}
encode = "ISO-8859-1";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s1 = encode;
return s1;
}
} catch (Exception exception1) {
}
encode = "UTF-8";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s2 = encode;
return s2;
}
} catch (Exception exception2) {
}
encode = "GBK";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s3 = encode;
return s3;
}
} catch (Exception exception3) {
}
return "";
} /**
* <p>
* Title: isoToutf8
* </p>
* <p>
* Description: ISO-8859-1 编码 转 UTF-8
* </p>
*
* @param str
* @return
*/
public static String isoToutf8(String str) {
try {
return new String(str.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return str;
}
} /**
* <p>
* Title: utf8Toiso
* </p>
* <p>
* Description: UTF-8 编码 转 ISO-8859-1
* </p>
*
* @param str
* @return
*/
public static String utf8Toiso(String str) {
try {
return new String(str.getBytes("utf-8"), "iso-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return str;
}
} /**
* <p>Title: unicodeToCn</p>
* <p>Description: unicode 转 中文</p>
*
* @param unicode
* @return
*/
public static String unicodeToCn(String unicode) {
/** 以 \ u 分割,因为java注释也能识别unicode,因此中间加了一个空格 */
String[] strs = unicode.split("\\\\u");
String returnStr = "";
// 由于unicode字符串以 \ u 开头,因此分割出的第一个字符是""。
for (int i = 1; i < strs.length; i++) {
returnStr += (char) Integer.valueOf(strs[i], 16).intValue();
}
return returnStr;
} /**
* <p>Title: cnToUnicode</p>
* <p>Description: 中文转 unicode</p>
*
* @param cn
* @return
*/
public static String cnToUnicode(String cn) {
char[] chars = cn.toCharArray();
String returnStr = "";
for (int i = 0; i < chars.length; i++) {
returnStr += "\\u" + Integer.toString(chars[i], 16);
}
return returnStr;
} /**
* URL 解码
*
* @return String
* @author lifq
* @date 2015-3-17 下午04:09:51
*/
public static String getURLDecoderString(String str) {
String result = "";
if (null == str) {
return "";
}
try {
result = java.net.URLDecoder.decode(str, ENCODE);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
} /**
* URL 转码
*
* @return String
* @author lifq
* @date 2015-3-17 下午04:10:28
*/
public static String getURLEncoderString(String str) {
String result = "";
if (null == str) {
return "";
}
try {
result = java.net.URLEncoder.encode(str, ENCODE);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
} }

最新文章

  1. Nutz中那些好用的工具类
  2. redis-string-统计
  3. [转]C++学习–基础篇(书籍推荐及分享)
  4. 专题——web.xml 中 url-pattern
  5. jpg图片转eps 用于LaTeX
  6. 书评&lt;&lt;剑指offer 名企面试官精讲典型编程题&gt;&gt;
  7. 【转】Java的接口和抽象类的区别
  8. Multi-Device Hybrid Apps for Visual Studio CTP2.0
  9. 感知器Perceptron
  10. python 函数基础 定义
  11. npm killed有可能是内存不够, 为Ubuntu增加swap
  12. android采用SurfaceView实现文字滚动效果
  13. LVM 移除PV步骤
  14. 告别回调,拥抱async await
  15. python文件派生
  16. Swig--模板引擎
  17. 【Web】Sublime Text 3 安装+注册+汉化
  18. Android studio jni
  19. FastDFS部署安装全过程
  20. 优云老王的心路历程(二):下一站Web体验监控产品

热门文章

  1. ios 获取手机信息(UIDevice、NSBundle、NSLocale)
  2. iterm恢复默认设置
  3. 报错 关于python的x和y不等长
  4. app 自动化测试 Appium+python可以运行的代码
  5. ACCESS数据库改名asp或asa
  6. JQ的核心原理以及扩展等
  7. css函数——calc()和attr()
  8. Java基础学习总结(50)——Java事务处理总结
  9. 【LCS】POJ1458Common Subsequence
  10. IIS服务器设置http自动跳转https方法