How to Implement HTTP byte-range requests in Spring MVC

前端 未结 7 1775
余生分开走
余生分开走 2020-11-30 21:30

I have the problem with video rewind on my site.

I figure out that problem with http headers.

My current controller method which returns video:



        
7条回答
  •  时光说笑
    2020-11-30 22:15

    it's 2020 here.

    Partial ranges are implemented in Spring by default (I'm using Spring Boot 2.2.4). My controller code looks similar to the one below and it works just fine with range headers.

    return ResponseEntity.ok().body(new FileSystemResource(file))
    

    Notes (See The Almighty Source Code for details):

    • Spring requires the response to be set to 200 and internally (if range headers are present on the request) replaces it with 206 Partial Content.
    • Spring also requires the response to be a Resource instance except InputStreamResource for which it fails. For files FileSystemResource seems to be the best choice.

提交回复
热议问题