目录

[TOC]

依赖

1
2
3
4
5
<dependency>
<groupId>com.googlecode.juniversalchardet</groupId>
<artifactId>juniversalchardet</artifactId>
<version>1.0.3</version>
</dependency>

工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import org.mozilla.universalchardet.UniversalDetector;

import java.io.IOException;
import java.io.InputStream;

/**
* 文件编码工具类
*/
public class FileEncodingUtils {


/**
* 检测文件编码
*
* @return 文件编码
*/
public static String detectFileEncoding(InputStream inputStream) throws IOException {
UniversalDetector detector = new UniversalDetector(null);

byte[] buffer = new byte[1024];
int nread;
while ((nread = inputStream.read(buffer)) > 0 && !detector.isDone()) {
detector.handleData(buffer, 0, nread);
}
detector.dataEnd();

String detectedCharset = detector.getDetectedCharset();
if (detectedCharset == null) {
// 如果检测不到编码,则使用默认编码
detectedCharset = "UTF-8";
}

detector.reset();
return detectedCharset;
}
}