'How can I stream video with Spring Boot
I'm trying to streaming videos which uploaded with MultipartFile and saved video path to database. When I execute a get request to this method "http://localhost:8080/video/{id}" video is just downloading but not showing something, also I tried to watch video with html but no action.
public ResponseEntity<InputStreamResource> getVideoById(Long id) throws FileNotFoundException {
Video video = this.videoRepository.findById(id).get();
java.io.File file = new java.io.File(video.getFile().getUrl());
InputStream fis = new FileInputStream(file);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.valueOf(video.getFile().getContentType()));
headers.set("Accept-Ranges", "bytes");
headers.set("Expires", "0");
headers.set("Cache-Control", "no-cache, no-store");
headers.set("Connection", "keep-alive");
headers.set("Content-Transfer-Encoding", "binary");
return new ResponseEntity<>(new InputStreamResource(fis), headers, HttpStatus.OK);
}
Solution 1:[1]
I dont know about InputStreamResource but I got this working long time ago with a ResponseEntity wrapping byte[]. Try out returning a Response with these headers with data being a byte[] obtained by the video file:
ResponseEntity.status(HttpStatus.PARTIAL_CONTENT)
.header("Content-Type", "video/"+ yourFileType)
.header("Accept-Ranges", "bytes")
.header("Content-Length", yourContentLength)
.header("Content-Range", "bytes " + rangeStart + "-" +
rangeEnd + "/" + fileSize)
.body(data);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | finisinfinitatis |
