Monday, December 24, 2012

Jersey Server Binary File

To serve a binary file such as a ZIP file with a REST service in Jersey, use the following:


@GET
@Produces("application/zip")
@Path("{itemId}")
public Response downloadItem(@PathParam("itemId") int itemId){
   String fileName = "item" + itemId + ".zip";
   StreamingOutput streamingOutput = new StreamingOutput(){
       public void write(OutputStream output) throws IOException, WebApplicationException {
           // write data to output stream here
       }        
   };
   
   return Response.ok(streamingOutput, "application/zip").header("content-disposition","attachment; filename = "+fileName).build();
}

No comments:

Post a Comment