通过HttpServletResponse直接响应到浏览器听录音
controll控制层
@RequestMapping(value = "/listenRecord", method = RequestMethod.GET)
public void listenRecord(@RequestParam("id") String id, HttpServletRequest request, HttpServletResponse response) {
baiYingCallRegister.listenRecord(callId ,request, response);
}
server业务层
public void listenRecord(String id, HttpServletRequest request, HttpServletResponse response) {
String SAVE_DIR = "/opt/luYin/records";
//这里的id可以是个录音的绝对路径,
try {
File path = new File(SAVE_DIR);
//可以是.mp3 .ogg .wav的音乐后缀都可以
File localFile = new File(path, "/" + id + ".wav");
this.player(request, response, new FileInputStream(localFile), localFile.length());
} catch (Exception e) {
logger.error("提供给平台听录音异常", e);
}
}
public void player(HttpServletRequest request, HttpServletResponse response, InputStream ins,
long fileLength, String... type) {
try (BufferedInputStream bis = new BufferedInputStream(ins);
OutputStream out = response.getOutputStream()) {
long p = 0L;
long toLength = 0L;
long contentLength = 0L;
// 0,从头开始的全文下载;1,从某字节开始的下载(bytes=27000-);2,从某字节开始到某字节结束的下载(bytes=27000-39000)
int rangeSwitch = 0;
String rangBytes = "";
response.reset();
response.setHeader("Accept-Ranges", "bytes");
String range = request.getHeader("Range");
if (range != null && range.trim().length() > 0 && !"null".equals(range)) {
response.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);
rangBytes = range.replaceAll("bytes=", "");
if (rangBytes.endsWith("-")) { // bytes=270000-
rangeSwitch = 1;
p = Long.parseLong(rangBytes.substring(0, rangBytes.indexOf("-")));
contentLength = fileLength - p; // 客户端请求的是270000之后的字节(包括bytes下标索引为270000的字节)
} else { // bytes=270000-320000
rangeSwitch = 2;
String temp1 = rangBytes.substring(0, rangBytes.indexOf("-"));
String temp2 = rangBytes.substring(rangBytes.indexOf("-") + 1, rangBytes.length());
p = Long.parseLong(temp1);
toLength = Long.parseLong(temp2);
contentLength = toLength - p + 1; // 客户端请求的是 270000-320000 之间的字节
}
} else {
contentLength = fileLength;
}
// 如果设设置了Content-Length,则客户端会自动进行多线程下载。如果不希望支持多线程,则不要设置这个参数。
// Content-Length: [文件的总大小] - [客户端请求的下载的文件块的开始字节]
response.setHeader("Content-Length", Long.toString(contentLength));
// Content-Range: bytes [文件块的开始字节]-[文件的总大小 - 1]/[文件的总大小]
if (rangeSwitch == 1) {
String contentRange = "bytes " + p + "-" + (fileLength - 1) + "/" + fileLength;
response.setHeader("Content-Range", contentRange);
bis.skip(p);
} else if (rangeSwitch == 2) {
String contentRange = range.replace("=", " ") + "/" + new Long(fileLength).toString();
response.setHeader("Content-Range", contentRange);
bis.skip(p);
} else {
String contentRange = new StringBuffer("bytes ").append("0-").append(fileLength - 1).append("/")
.append(fileLength).toString();
response.setHeader("Content-Range", contentRange);
}
if (type.length > 0) {
response.setHeader("Content-Type", type[0]);
} else {
response.setHeader("Content-Type", "audio/" + "wav" + ";charset=UTF-8");
}
int n = 0;
long readLength = 0;
int bsize = 10240;
byte[] bytes = new byte[bsize];
if (rangeSwitch == 2) {
// 针对 bytes=27000-39000 的请求,从27000开始写数据
while (readLength <= contentLength - bsize) {
n = bis.read(bytes);
readLength += n;
out.write(bytes, 0, n);
out.flush();
}
if (readLength <= contentLength) {
n = bis.read(bytes, 0, (int) (contentLength - readLength));
readLength += n;
out.write(bytes, 0, n);
out.flush();
}
} else {
while ((n = bis.read(bytes)) != -1) {
readLength += n;
out.write(bytes, 0, n);
out.flush();
}
}
} catch (Exception e) {
logger.error("提供给平台听录音异常了", e);
}
}