Building RESTful web service for reading images

The following method reads the binary representation of an image from the database and streams the content by using the javax.ws.rs.core.StreamingOutput class. The method is annotated with @Produces(MediaType.APPLICATION_OCTET_STREAM) to indicate that the response is in a binary form:

//Other imports are omitted for brevity 
import javax.ws.rs.core.StreamingOutput; 
 
@GET 
@Path("/{empId}/image") 
@Produces(MediaType.APPLICATION_OCTET_STREAM) 
public Response getImage(final @PathParam("empId") Integer id) throws Exception { 
 
  StreamingOutput outputImage = new StreamingOutput() { 
     @Override 
     Public void write(OutputStream output)  
         throws IOException { 
    EmployeeImage empImg = findEmployeeImageEntity(id); 
    byte[] buf = empImg.getImage(); 
    output.write(buf); 
    output.flush(); 
     } 
  }; 

ResponseBuilder response = Response.ok((Object) outputImage);
response.header("Content-Disposition", "inline;filename=image.jpeg");
return response.build(); }

You can use the preceding RESTful web API directly with the <img> component present in the HTML page, as shown in the following HTML snippet. The employees/100/image URI points to the RESTful web API that we built for reading the image content:

<img alt="Employee image" src="employees/100/image" id="id"/> 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.176.166