|
|
@ -16,7 +16,6 @@ import java.nio.file.Files;
|
|
|
|
* @description: 自动获取文件的编码
|
|
|
|
* @description: 自动获取文件的编码
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class EncodingDetects {
|
|
|
|
public class EncodingDetects {
|
|
|
|
private static UniversalDetector detector = new UniversalDetector(null);
|
|
|
|
|
|
|
|
private static final int DEFAULT_LENGTH = 4096;
|
|
|
|
private static final int DEFAULT_LENGTH = 4096;
|
|
|
|
private static final int LIMIT = 50;
|
|
|
|
private static final int LIMIT = 50;
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(EncodingDetects.class);
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(EncodingDetects.class);
|
|
|
@ -40,13 +39,10 @@ public class EncodingDetects {
|
|
|
|
if (content != null && content.length <= LIMIT) {
|
|
|
|
if (content != null && content.length <= LIMIT) {
|
|
|
|
return SimpleEncodingDetects.getJavaEncode(content);
|
|
|
|
return SimpleEncodingDetects.getJavaEncode(content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String charsetName;
|
|
|
|
UniversalDetector detector = new UniversalDetector(null);
|
|
|
|
synchronized (EncodingDetects.class) {
|
|
|
|
|
|
|
|
detector.reset();
|
|
|
|
|
|
|
|
detector.handleData(content, 0, content.length);
|
|
|
|
detector.handleData(content, 0, content.length);
|
|
|
|
detector.dataEnd();
|
|
|
|
detector.dataEnd();
|
|
|
|
charsetName = detector.getDetectedCharset();
|
|
|
|
String charsetName = detector.getDetectedCharset();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (charsetName == null) {
|
|
|
|
if (charsetName == null) {
|
|
|
|
charsetName = Charset.defaultCharset().name();
|
|
|
|
charsetName = Charset.defaultCharset().name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|