java 读取 utf-8 bom 出现乱码
private static String ReadUTF8WithBOMToString(String filePath) throws IOException { String defaultEncoding = "UTF-8"; InputStream inputStream = new FileInputStream(filePath); StringBuffer fileData = new StringBuffer(); try { BOMInputStream bOMInputStream = new BOMInputStream(inputStream); ByteOrderMark bom = bOMInputStream.getBOM(); String charsetName = bom == null ? defaultEncoding : bom.getCharsetName(); InputStreamReader isr = new InputStreamReader(new BufferedInputStream(bOMInputStream), charsetName); BufferedReader reader = new BufferedReader(isr); char[] buf = new char[1024]; int numRead = 0; while((numRead=reader.read(buf)) != -1) { String readData = String.valueOf(buf, 0, numRead); fileData.append(readData); } reader.close(); return fileData.toString(); } finally { inputStream.close(); } }
用到了一个jar包 Apache Commons IO, https://img2020.cnblogs.com/blog/65667/202008/65667-20200812160049507-807397496.jpg,将图片链接下载后改名为 commons-io-2.7.jar
桂棹兮兰桨,击空明兮溯流光。