目录[TOC] 依赖12345<dependency> <groupId>com.googlecode.juniversalchardet</groupId> <artifactId>juniversalchardet</artifactId> <version>1.0.3</version></dependency> 工具类123456789101112131415161718192021222324252627282930313233343536import 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; }}