lundi 2 mars 2015

Spring MVC video download ranged

I am building a video streaming web application. It uses an HTML5 video player. In order to prevent loading the entire file into memory, I am using the following code



@RequestMapping(value = "/video/{videoId}" )
public void downloadVideo(
@PathVariable(value = "videoId") long videoId,
HttpServletResponse response) {

Video video = getVideo(videoId);
response.setContentLength((int) video.getSize());
response.setContentType(video.getMimeType());

Iterable<byte[]> fileContents = video.getContents();

ServletOutputStream outputStream = response.getOutputStream();
for (byte[] bytes : fileContents) {
outputStream.write(bytes);
}
}


video.getContents() returns a custom buffered object which can be treated as an iterable array of bytes (could also be a BufferedInputStream). The video stream works great when you start playing from the beginning.


However when you skip forward or rewind in the video, the browser sends a range header Range: bytes=23432-. When this happens, it fails with the exception java.lang.IllegalStateException: getOutputStream() has already been called for this response.


How do I modify this to work for ranged queries.


Aucun commentaire:

Enregistrer un commentaire