How to Implement HTTP byte-range requests in Spring MVC

前端 未结 7 1730
余生分开走
余生分开走 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

    Try this simple code .

    @GET
    @Produces({"application/octet-stream"})
    public Response getFile() {
      File file = ... 
      return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
          .header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"" ) .build();
    }
    

    Update :

    Its wrapping the Spring Jersey jersey.java.net RESTful implementation with spring MVC.

    For pure Spring mvc use below code.

    @RequestMapping("/video") public ResponseEntity<byte[]> getvideo() throws IOException { 
        InputStream in = ... 
        final HttpHeaders headers = new HttpHeaders(); 
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); 
        return new ResponseEntity<byte[]>IOUtils.toByteArray(in),headers,HttpStatus.CREATED); 
    }
    

    IOUtils is from apache common jar

    0 讨论(0)
提交回复
热议问题