diff --git a/src/main/java/com/threecloud/dataserviceyy/service/VaaSyncService.java b/src/main/java/com/threecloud/dataserviceyy/service/VaaSyncService.java index 8de79eb..ffc7fc9 100644 --- a/src/main/java/com/threecloud/dataserviceyy/service/VaaSyncService.java +++ b/src/main/java/com/threecloud/dataserviceyy/service/VaaSyncService.java @@ -277,7 +277,27 @@ public class VaaSyncService { (int) (endTime - begTime), channelPhone, phone, isOutgoing, isAnswered); // 下载录音文件(无重试) - if (!Files.exists(localFile) || Files.size(localFile) == 0) { + // 文件不存在、大小为0、或小于1KB(可能损坏)时重新下载 + boolean needDownload = !Files.exists(localFile) || Files.size(localFile) < 1024; + // 额外校验:已存在文件也可能是HTML错误页面,检查文件头 + if (!needDownload) { + try { + byte[] header = new byte[16]; + try (java.io.RandomAccessFile raf = new java.io.RandomAccessFile(localFile.toFile(), "r")) { + raf.readFully(header); + } + String headerStr = new String(header, "ASCII").trim().toLowerCase(); + if (headerStr.startsWith(" 0 && totalBytes != expectedSize) { saveFile.delete(); throw new RuntimeException(String.format( "文件下载不完整: 期望 %d bytes, 实际 %d bytes", expectedSize, totalBytes)); } + // 录音文件至少应有1KB,过小说明下载异常 + if (totalBytes < 1024) { + saveFile.delete(); + throw new RuntimeException(String.format( + "文件过小,可能下载异常: 仅 %d bytes", totalBytes)); + } + // 校验文件头,确保不是HTML错误页面 + byte[] header = new byte[(int) Math.min(totalBytes, 16)]; + try (java.io.RandomAccessFile raf = new java.io.RandomAccessFile(saveFile, "r")) { + raf.readFully(header); + } + String headerStr = new String(header, "ASCII").trim().toLowerCase(); + if (headerStr.startsWith("