There are two ways to do this.
1st - returns stream StreamingOutput.
@Produces(MediaType.APPLICATION_OCTET_STREAM) public Response download() { InputStream is = getYourInputStream(); StreamingOutput stream = new StreamingOutput() { public void write(OutputStream output) throws IOException, WebApplicationException { try { output.write(IOUtils.toByteArray(is)); } catch (Exception e) { throw new WebApplicationException(e); } } }; return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM).header("content-disposition", "attachment; filename=\"temp.csv\"").build(); }
You can return the file size by adding the Content-Length header, in the following example:
return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM).header("content-disposition", "attachment; filename=\"temp.csv\"").header("Content-Length", getFileSize()).build();
But if you do not want to return an instance of StreamingOutput, there is another option.
2nd - Define the input stream as the response of the object.
@Produces(MediaType.APPLICATION_OCTET_STREAM) public Response download() { InputStream is = getYourInputStream(); return Response.code(200).entity(is).build(); }
PeDiVo
source share