3

I want to stream video file instead of Downloading it. I tried with the below code. But it is downloading.

@GET
@Path("video")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response video() {
File file = new File("/home/lvaddi/Downloads/demoVideoFile.flv");

ResponseBuilder response = Response.ok(file, MediaType.APPLICATION_OCTET_STREAM);
response.header("Content-Disposition",  "filename=videofile.flv");
return response.build();
}
Lokesh Reddy
  • 85
  • 1
  • 11

1 Answers1

3

You should use StreamingOutput to do that not return the file as body (which will send the whole file in bulk), something like:

new StreamingOutput() {
        @Override
        public void write(OutputStream output) throws IOException, WebApplicationException {
            try {
                InputStream input = <get input stream from your file>
                IOUtils.copy(input, output);
                output.flush();
            } finally {
                EntityUtils.consume(response.getEntity());
                IOUtils.closeQuietly(response);
            }
        }
    };

There's also an example here

Community
  • 1
  • 1
danf
  • 2,629
  • 21
  • 28