1,用google的工具包,配置maven:
<!-- https://mvnrepository.com/artifact/com.googlecode.juniversalchardet/juniversalchardet --> <dependency> <groupId>com.googlecode.juniversalchardet</groupId> <artifactId>juniversalchardet</artifactId> <version>1.0.3</version> </dependency>
2,定义一个公共方法:
public static String guessEncoding(byte[] bytes) { UniversalDetector detector = new UniversalDetector(null); detector.handleData(bytes, 0, bytes.length); detector.dataEnd(); String encoding = detector.getDetectedCharset(); detector.reset(); return encoding; }