@GetMapping("/play/{name}")
public ResponseEntity<ResourceRegion> play(@PathVariable("name") String name,@RequestHeader HttpHeaders httpHeaders) throws IOException {
UrlResource video =new UrlResource("file:"+ARCHIVE_DIR_STR+"/"+name+".mp4");
ResourceRegion resourceRegion = resourceRegion(video, httpHeaders);
return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT)
.contentType(MediaTypeFactory
.getMediaType("video/mp4")
.orElse(MediaType.APPLICATION_OCTET_STREAM))
.body(resourceRegion);
}
private ResourceRegion resourceRegion(UrlResource video, HttpHeaders headers) throws IOException {
long contentLength = video.contentLength();
HttpRange range = headers.getRange().get(0);
if (range != null) {
long start = range.getRangeStart(contentLength);
long end = range.getRangeEnd(contentLength);
long rangeLength = Math.min(1 * 1024 * 1024, end - start + 1);
return new ResourceRegion(video, start, rangeLength);
} else {
long rangeLength = Math.min(1 * 1024 * 1024, contentLength);
return new ResourceRegion(video, 0, rangeLength);
}
}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PlayPage</title>
</head>
<body>
<video autoplay="autoplay" controls>
<source src="/play/name" type="video/mp4">
</video>
</body>
</html>