MySQL 的 latin1 ≠ 標準 ISO-8859-1 (IANA) ≠ Windows CP1252。
官方文檔確認:latin1 is the same as the Windows cp1252 character set ... except ... MySQL translates 0x81 to Unicode 0x0081, 0x8d to 0x008d, 0x8f to 0x008f, 0x90 to 0x0090, and 0x9d to 0x009d.
// 錯誤:使用標準 CP1252 或 ISO-8859-1 解碼
String s = new String(bytes, "cp1252"); // 0x81/0x8D/0x8F/0x90/0x9D 會丟失或錯誤映射
private String convertMySQLLatin1(String s) {
if (s == null) return null;
try {
int len = s.length();
byte[] buf = new byte[len];
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
switch (c) {
case 0x0081: buf[i] = (byte)0x81; break;
case 0x008D: buf[i] = (byte)0x8D; break;
case 0x008F: buf[i] = (byte)0x8F; break;
case 0x0090: buf[i] = (byte)0x90; break;
case 0x009D: buf[i] = (byte)0x9D; break;
default:
buf[i] = Character.toString(c).getBytes("cp1252")[0];
}
}
return new String(buf, "UTF-8"); // 或目標編碼
} catch (UnsupportedEncodingException e) {
log.error("charset convert error", e);
return null;
}
}
utf8mb4,從庫、表、連接、應用全鏈路一致。