Encodes.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Copyright (c) 2005-2012 springside.org.cn
  3. */
  4. package com.gxzc.zen.common.plug;
  5. import org.apache.commons.codec.DecoderException;
  6. import org.apache.commons.codec.binary.Base64;
  7. import org.apache.commons.codec.binary.Hex;
  8. import org.apache.commons.lang3.StringEscapeUtils;
  9. import java.io.UnsupportedEncodingException;
  10. import java.net.URLDecoder;
  11. import java.net.URLEncoder;
  12. /**
  13. * 封装各种格式的编码解码工具类.
  14. * 1.Commons-Codec的 hex/base64 编码
  15. * 2.自制的base62 编码
  16. * 3.Commons-Lang的xml/html escape
  17. * 4.JDK提供的URLEncoder
  18. * @author calvin
  19. * @version 2013-01-15
  20. */
  21. public class Encodes {
  22. private static final String DEFAULT_URL_ENCODING = "UTF-8";
  23. private static final char[] BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
  24. /**
  25. * Hex编码.
  26. */
  27. public static String encodeHex(byte[] input) {
  28. return new String(Hex.encodeHex(input));
  29. }
  30. /**
  31. * Hex解码.
  32. */
  33. public static byte[] decodeHex(String input) {
  34. try {
  35. return Hex.decodeHex(input.toCharArray());
  36. } catch (DecoderException e) {
  37. throw new RuntimeException(e);
  38. }
  39. }
  40. /**
  41. * Base64编码.
  42. */
  43. public static String encodeBase64(byte[] input) {
  44. return new String(Base64.encodeBase64(input));
  45. }
  46. /**
  47. * Base64编码.
  48. */
  49. public static String encodeBase64(String input) {
  50. try {
  51. return new String(Base64.encodeBase64(input.getBytes(DEFAULT_URL_ENCODING)));
  52. } catch (UnsupportedEncodingException e) {
  53. return "";
  54. }
  55. }
  56. // /**
  57. // * Base64编码, URL安全(将Base64中的URL非法字符'+'和'/'转为'-'和'_', 见RFC3548).
  58. // */
  59. // public static String encodeUrlSafeBase64(byte[] input) {
  60. // return Base64.encodeBase64URLSafe(input);
  61. // }
  62. /**
  63. * Base64解码.
  64. */
  65. public static byte[] decodeBase64(String input) {
  66. return Base64.decodeBase64(input.getBytes());
  67. }
  68. /**
  69. * Base64解码.
  70. */
  71. public static String decodeBase64String(String input) {
  72. try {
  73. return new String(Base64.decodeBase64(input.getBytes()), DEFAULT_URL_ENCODING);
  74. } catch (UnsupportedEncodingException e) {
  75. return "";
  76. }
  77. }
  78. /**
  79. * Base62编码。
  80. */
  81. public static String encodeBase62(byte[] input) {
  82. char[] chars = new char[input.length];
  83. for (int i = 0; i < input.length; i++) {
  84. chars[i] = BASE62[((input[i] & 0xFF) % BASE62.length)];
  85. }
  86. return new String(chars);
  87. }
  88. /**
  89. * Html 转码.
  90. */
  91. public static String escapeHtml(String html) {
  92. return StringEscapeUtils.escapeHtml4(html);
  93. }
  94. /**
  95. * Html 解码.
  96. */
  97. public static String unescapeHtml(String htmlEscaped) {
  98. return StringEscapeUtils.unescapeHtml4(htmlEscaped);
  99. }
  100. /**
  101. * Xml 转码.
  102. */
  103. public static String escapeXml(String xml) {
  104. return StringEscapeUtils.escapeXml10(xml);
  105. }
  106. /**
  107. * Xml 解码.
  108. */
  109. public static String unescapeXml(String xmlEscaped) {
  110. return StringEscapeUtils.unescapeXml(xmlEscaped);
  111. }
  112. /**
  113. * URL 编码, Encode默认为UTF-8.
  114. */
  115. public static String urlEncode(String part) {
  116. try {
  117. return URLEncoder.encode(part, DEFAULT_URL_ENCODING);
  118. } catch (UnsupportedEncodingException e) {
  119. throw new RuntimeException(e);
  120. }
  121. }
  122. /**
  123. * URL 解码, Encode默认为UTF-8.
  124. */
  125. public static String urlDecode(String part) {
  126. try {
  127. return URLDecoder.decode(part, DEFAULT_URL_ENCODING);
  128. } catch (UnsupportedEncodingException e) {
  129. throw new RuntimeException(e);
  130. }
  131. }
  132. }