CharsetUtil.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * FileName:CharsetUtil.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 info.monitorenter.cpdetector.io.*;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.File;
  23. import java.io.InputStream;
  24. import java.net.URL;
  25. import java.nio.charset.Charset;
  26. /**
  27. * 功能:
  28. * @author xiongliang
  29. *
  30. * mobile enterprise application platform
  31. * Version 0.1
  32. */
  33. public class CharsetUtil {
  34. /**
  35. * 获得JDK默认编码
  36. * @return
  37. */
  38. public static String getDefaultCharset(){
  39. return Charset.defaultCharset().toString();
  40. }
  41. /**
  42. * 获得远程URL文件的编码格式
  43. * @param url
  44. * @return
  45. */
  46. public static String getReomoteURLFileEncode(URL url) {
  47. CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
  48. detector.add(new ParsingDetector(false));
  49. detector.add(JChardetFacade.getInstance());
  50. detector.add(ASCIIDetector.getInstance());
  51. detector.add(UnicodeDetector.getInstance());
  52. Charset charset = null;
  53. try {
  54. charset = detector.detectCodepage(url);
  55. } catch (Exception ex) {
  56. ex.printStackTrace();
  57. }
  58. if (charset != null) {
  59. return charset.name();
  60. } else {
  61. return ConvertUtil.UTF_8;
  62. }
  63. }
  64. /**
  65. * 获得文件流的编码格式
  66. * @param is
  67. * @return
  68. */
  69. public static String getInputStreamEncode(InputStream is) {
  70. CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
  71. detector.add(new ParsingDetector(false));
  72. detector.add(JChardetFacade.getInstance());
  73. detector.add(ASCIIDetector.getInstance());
  74. detector.add(UnicodeDetector.getInstance());
  75. Charset charset = null;
  76. try {
  77. charset = detector.detectCodepage(is, 0);
  78. } catch (Exception ex) {
  79. ex.printStackTrace();
  80. }
  81. if (charset != null) {
  82. return charset.name();
  83. } else {
  84. return ConvertUtil.UTF_8;
  85. }
  86. }
  87. /**
  88. * 获得本地文件的编码格式
  89. * @param file
  90. * @return
  91. */
  92. public static String getLocalteFileEncode(File file) {
  93. CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
  94. detector.add(new ParsingDetector(false));
  95. detector.add(JChardetFacade.getInstance());
  96. detector.add(ASCIIDetector.getInstance());
  97. detector.add(UnicodeDetector.getInstance());
  98. Charset charset = null;
  99. try {
  100. charset = detector.detectCodepage(file.toURI().toURL());
  101. } catch (Exception ex) {
  102. ex.printStackTrace();
  103. }
  104. if (charset != null) {
  105. return charset.name();
  106. } else {
  107. return ConvertUtil.UTF_8;
  108. }
  109. }
  110. /**
  111. * 获得字符串的编码格式
  112. * @param str
  113. * @return
  114. */
  115. public static String getStringEncode(String str) {
  116. return getEncode(str.getBytes());
  117. }
  118. /**
  119. * 获得字符串的编码格式
  120. * @param _byte
  121. * @return
  122. */
  123. public static String getEncode(byte[] _byte) {
  124. CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
  125. detector.add(new ParsingDetector(false));
  126. detector.add(JChardetFacade.getInstance());
  127. detector.add(ASCIIDetector.getInstance());
  128. detector.add(UnicodeDetector.getInstance());
  129. Charset charset = null;
  130. InputStream myIn=new ByteArrayInputStream(_byte);
  131. try {
  132. charset = detector.detectCodepage(myIn,8);
  133. } catch (Exception ex) {
  134. ex.printStackTrace();
  135. }
  136. if (charset != null) {
  137. return charset.name();
  138. } else {
  139. return ConvertUtil.UTF_8;
  140. }
  141. }
  142. }