1
0

Encodes.java 3.7 KB

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