@RequestMapping("play")
public void play(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.reset();
File file = new File("url");
long fileLength = file.length();
// 随机读取文件
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
// 请求头获取从哪开始读
String rangeStr = request.getHeader("Range");
long range = 0;
if (rangeStr != null) {
range = Long.valueOf(rangeStr.substring(rangeStr.indexOf("=") + 1, rangeStr.indexOf("-")));
}
System.err.println(range);
ServletOutputStream outputStream = response.getOutputStream();
response.setHeader("Content-Type", "video/mp4");
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
randomAccessFile.seek(range);
byte[] bytes = new byte[1024 * 1024];
int len = randomAccessFile.read(bytes);
response.setContentLength(len);
response.setHeader("Content-Range", "bytes "+range+"-"+(fileLength-1)+"/"+fileLength);
outputStream.write(bytes, 0, len);
outputStream.close();
randomAccessFile.close();
}
03-20
304
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-12
367
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-31
182
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-08
2208
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-25
1038
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)