Browse Source

优化语音

main
wang 3 weeks ago
parent
commit
f1c080bda4
  1. 22
      src/main/java/com/threecloud/dataserviceyy/service/VaaSyncService.java
  2. 18
      src/main/java/com/threecloud/dataserviceyy/util/VaaHttpUtil.java

22
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("<!doctype") || headerStr.startsWith("<html")) {
logger.warn("【录音】本地文件为HTML页面,重新下载: {}", fileName);
needDownload = true;
Files.delete(localFile);
}
} catch (Exception e) {
logger.warn("【录音】校验本地文件失败,重新下载: {}", fileName);
needDownload = true;
}
}
if (needDownload) {
String fileUrl = "http://" + deviceHost + filePath;
vaaHttpUtil.httpDown(fileUrl, localPath, authToken);
logger.info("【录音】下载完成: {} ({} 字节)", fileName, Files.size(localFile));

18
src/main/java/com/threecloud/dataserviceyy/util/VaaHttpUtil.java

@ -163,12 +163,28 @@ public class VaaHttpUtil {
logger.info("录音文件下载完成: {}, 大小: {} bytes ({} MB)",
savePath, totalBytes, totalBytes / 1024 / 1024);
// 完整性校验:如果服务器返回了Content-Length,校验实际下载大小
// 完整性校验
if (expectedSize > 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("<!doctype") || headerStr.startsWith("<html")) {
saveFile.delete();
throw new RuntimeException("下载内容为HTML页面,非音频文件");
}
} else {
throw new RuntimeException("文件下载失败,HTTP状态码: " + responseCode);
}

Loading…
Cancel
Save