ConvertUtil.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * FileName:ConvertUtil.java
  3. * <p>
  4. * Copyright (c) 2017-2020, <a href="http://www.webcsn.com">hermit (794890569@qq.com)</a>.
  5. * <p>
  6. * Licensed under the GNU General Public License, Version 3 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. * <p>
  10. * http://www.gnu.org/licenses/gpl-3.0.html
  11. * <p>
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. package cn.com.lzt.common.util;
  20. import cn.com.lzt.common.config.Configuration;
  21. import org.apache.commons.codec.DecoderException;
  22. import org.apache.commons.codec.binary.Base64;
  23. import org.apache.commons.codec.binary.Hex;
  24. import java.io.*;
  25. /**
  26. * 功能:转换编码
  27. * @author xiongliang
  28. *
  29. * mobile enterprise application platform
  30. * Version 0.1
  31. */
  32. public class ConvertUtil {
  33. private static final char[] BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
  34. /**
  35. * 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁块
  36. * */
  37. public static final String US_ASCII = "US-ASCII";
  38. /**
  39. * ISO 拉丁字母表 No.1,也叫作 ISO-LATIN-1
  40. * */
  41. public static final String ISO_8859_1 = "ISO-8859-1";
  42. /**
  43. * 8 位 UCS 转换格式
  44. * */
  45. public static final String UTF_8 = "UTF-8";
  46. /**
  47. * 16 位 UCS 转换格式,Big Endian(最低地址存放高位字节)字节顺序
  48. * */
  49. public static final String UTF_16BE = "UTF-16BE";
  50. /**
  51. * 16 位 UCS 转换格式,Little-endian(最高地址存放低位字节)字节顺序
  52. * */
  53. public static final String UTF_16LE = "UTF-16LE";
  54. /**
  55. * 16 位 UCS 转换格式,字节顺序由可选的字节顺序标记来标识
  56. * */
  57. public static final String UTF_16 = "UTF-16";
  58. /**
  59. * 中文超大字符集
  60. * */
  61. public static final String GBK = "GBK";
  62. /**
  63. * 繁体中文
  64. * */
  65. public static final String BIG_5 = "BIG5";
  66. /**
  67. * 字符串编码转换的实现方法
  68. * @param strIn
  69. * @param sourceCharset
  70. * @param targetCharset
  71. * @return
  72. */
  73. public static String convertCharset(String strIn, String sourceCharset, String targetCharset){
  74. if(ValidateUtil.isNull(strIn))
  75. return strIn;
  76. String strOut = null;
  77. try{
  78. byte[] c = strIn.getBytes(sourceCharset);
  79. strOut = new String(c, targetCharset);
  80. }catch(UnsupportedEncodingException e){
  81. e.printStackTrace();
  82. }
  83. return strOut;
  84. }
  85. /**
  86. * 获得UTF8编码的字符串
  87. * @param gbkStr
  88. * @return
  89. */
  90. public static String convertGbkToUft8(String gbkStr) {
  91. try {
  92. return new String(convertGbkToUtf8Bytes(gbkStr), UTF_8);
  93. } catch (UnsupportedEncodingException e) {
  94. throw new InternalError();
  95. }
  96. }
  97. /**
  98. * GBK转UTF-8编码
  99. * @param text
  100. * @return
  101. */
  102. /*public static String convertGbkToUft8(String text){
  103. String utf = null;
  104. try {
  105. String iso = new String(text.getBytes(UTF_8),ISO_8859_1);
  106. utf = new String(iso.getBytes(ISO_8859_1),UTF_8);
  107. } catch (UnsupportedEncodingException e) {
  108. e.printStackTrace();
  109. }
  110. return utf;
  111. }*/
  112. /**
  113. * 获得UTF8编码的字节数组
  114. * @param gbkStr
  115. * @return
  116. */
  117. public static byte[] convertGbkToUtf8Bytes(String gbkStr) {
  118. int n = gbkStr.length();
  119. byte[] utfBytes = new byte[3 * n];
  120. int k = 0;
  121. for (int i = 0; i < n; i++) {
  122. int m = gbkStr.charAt(i);
  123. if (m < 128 && m >= 0) {
  124. utfBytes[k++] = (byte) m;
  125. continue;
  126. }
  127. utfBytes[k++] = (byte) (0xe0 | (m >> 12));
  128. utfBytes[k++] = (byte) (0x80 | ((m >> 6) & 0x3f));
  129. utfBytes[k++] = (byte) (0x80 | (m & 0x3f));
  130. }
  131. if (k < utfBytes.length) {
  132. byte[] tmp = new byte[k];
  133. System.arraycopy(utfBytes, 0, tmp, 0, k);
  134. return tmp;
  135. }
  136. return utfBytes;
  137. }
  138. /**
  139. * 输入流转字符串
  140. * @param is
  141. * @return
  142. */
  143. public static String convertStreamToString(InputStream is){
  144. BufferedReader reader = new BufferedReader( new InputStreamReader(is));
  145. StringBuilder sb = new StringBuilder();
  146. String line = null ;
  147. try{
  148. while ((line = reader.readLine()) != null ) {
  149. sb.append(line + Configuration.LINE_SEPARATOR );
  150. }
  151. } catch (IOException e) {
  152. e.printStackTrace();
  153. } finally {
  154. try {
  155. is.close();
  156. }catch(IOException e) {
  157. e.printStackTrace();
  158. }
  159. }
  160. return sb.toString();
  161. }
  162. /**
  163. * 半角转换成全角
  164. * @param input
  165. * @return
  166. */
  167. public static String convertSBC(String input){
  168. // 半角转全角
  169. char[] c = input.toCharArray();
  170. for (int i = 0; i < c.length; i++) {
  171. if (c[i] == 32) {
  172. c[i] = (char) 12288;
  173. continue;
  174. }
  175. if (c[i] < 127)
  176. c[i] = (char) (c[i] + 65248);
  177. }
  178. return new String(c);
  179. }
  180. /**
  181. * 全角转换成半角
  182. * @param input
  183. * @return
  184. */
  185. public static String convertDBC(String input){
  186. // 全角转 半角
  187. char[] c = input.toCharArray();
  188. for (int i = 0; i < c.length; i++) {
  189. if (c[i] == 12288) {
  190. c[i] = (char) 32;
  191. continue;
  192. }
  193. if (c[i] > 65280 && c[i] < 65375)
  194. c[i] = (char) (c[i] - 65248);
  195. }
  196. return new String(c);
  197. }
  198. /**
  199. * Hex编码.
  200. */
  201. public static String convertEncodeHex(byte[] input) {
  202. return Hex.encodeHexString(input);
  203. }
  204. /**
  205. * Hex解码.
  206. */
  207. public static byte[] convertDecodeHex(String input) {
  208. try {
  209. return Hex.decodeHex(input.toCharArray());
  210. } catch (DecoderException e) {
  211. throw new RuntimeException(e);
  212. }
  213. }
  214. /**
  215. * Base64编码.
  216. */
  217. public static String convertEncodeBase64(byte[] input) {
  218. return Base64.encodeBase64String(input);
  219. }
  220. /**
  221. * Base64编码, URL安全(将Base64中的URL非法字符'+'和'/'转为'-'和'_', 见RFC3548).
  222. */
  223. public static String convertEncodeUrlSafeBase64(byte[] input) {
  224. return Base64.encodeBase64URLSafeString(input);
  225. }
  226. /**
  227. * Base64解码.
  228. */
  229. public static byte[] convertDecodeBase64(String input) {
  230. return Base64.decodeBase64(input);
  231. }
  232. /**
  233. * Base62编码。
  234. */
  235. public static String convertEncodeBase62(byte[] input) {
  236. char[] chars = new char[input.length];
  237. for (int i = 0; i < input.length; i++) {
  238. chars[i] = BASE62[((input[i] & 0xFF) % BASE62.length)];
  239. }
  240. return new String(chars);
  241. }
  242. }