SpringBoot 中检测文件编码格式
引入相关依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>com.googlecode.juniversalchardet</groupId>
<artifactId>juniversalchardet</artifactId>
<version>1.0.3</version>
</dependency>
工具类
import org.apache.commons.lang3.StringUtils;
import org.mozilla.universalchardet.UniversalDetector;
import java.io.*;
import java.nio.file.Files;
public class FileUtils {
/**
* 自动检测文件的编码格式
*
* @param file 需要检测的文件
* @return encoding 编码格式,默认为 UTF-8
* @throws IOException
*/
public static String detectFileEncoding(File file) throws IOException {
String encoding = null;
try (BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
UniversalDetector detector = new UniversalDetector(null);
byte[] buf = new byte[4096];
int len;
while ((len = bis.read(buf)) != -1 && !detector.isDone()) {
detector.handleData(buf, 0, len);
}
detector.dataEnd();
encoding = detector.getDetectedCharset();
detector.reset();
}
return !StringUtils.isBlank(encoding) ? encoding : "UTF-8";
}
}