业务背景:
客户要求完成音频生成之后,如果退出当前页面,则将后端生成的音频删除;
现象:
前端加载audio标签之后,然后退出,调用后端删除音频接口,此时后端调用file.delete会返回false,删除失败;
问题排查:
由于前端加载audio的时候,会去获取播放的文件流,但是前端audio标签是分成两段加载流,第一段获取音频头文件,第二段才是获取音频内容。所以当加载完音频,但是未播放时,http连接会保持,直到音频主体传送完成
此时问题便发生了:未播放音频,但是退出,删除音频会导致文件无法删除;
/**
* 播放音频
*
* @param request 请求
* @param response 响应
* @param path 音频地址
* @return
*/
@GetMapping("/player")
public void getMusic(HttpServletRequest request, HttpServletResponse response, @RequestParam String path) {
File file = new File(path);
if (!file.exists()) {
throw new RuntimeException("音频文件不存在");
}
try (FileInputStream fis = new FileInputStream(file)) {
response.addHeader("Accept-Ranges", "bytes");
response.addHeader("Content-Type", "audio/x-wav;charset=UTF-8");
response.addHeader("Connection", "close");
ServletOutputStream os = response.getOutputStream();
copy(fis, os);
} catch (Exception e) {
LOG.error(e.getMessage());
e.printStackTrace();
}
}
/**
* 进行字节流拷贝
*
* @param in
* @param out
* @return
* @throws IOException
*/
public static int copy(InputStream in, ServletOutputStream out){
Assert.notNull(in, "No InputStream specified");
Assert.notNull(out, "No OutputStream specified");
int byteCount = 0;
int size = 4096;
byte[] buffer = new byte[size];
int bytesRead = -1;
try {
while ((bytesRead = in.read(buffer)) != -1) {
// LOG.info("开始:{}",byteCount);
out.write(buffer, 0, bytesRead);
byteCount += bytesRead;
// LOG.info("结束:{}",byteCount);
}
out.flush();
LOG.info("总字节:{}",byteCount);
} catch (Exception e) {
/**
* 捕获io异常,outputstream抛出异常断开问题,捕获处理
*/
LOG.error(e.getMessage());
}
return byteCount;
}
解决办法:
前端删除文件前主动关闭audio标签流连接

6075

被折叠的 条评论
为什么被折叠?



