Hi,

In addition to what Sergey has presented, which is undoubtedly more directly to 
the point of what you asked, I wanted to suggest the following. Perhaps this 
doesn't fit your situation, but I have never found it actually necessary to 
waste the servlet framework resources to return a streamed image. Instead, I 
prefer to:

1) Have the servlet/web service locate the image - whether that means finding 
it or creating it on the server's hard drive.

2) Pass the name or path of the image back to the client, and 

3) Have the client use the returned image name or path to request the image via 
a standard http static image request.

Since you indicated that you basically wanted it streamed to the browser, 
presumably to simply display it, it sounds like there is no need to tie up Java 
framework resources and connections to accomplish what the container can do 
with simple HTTP static resource retrieval mechanisms. 


Ron Grimes



-----Original Message-----
From: Sergey Beryozkin [mailto:[email protected]] 
Sent: Tuesday, November 03, 2009 10:42 AM
To: [email protected]
Subject: Re: Return an Image from a REST webservice api

Hi

I've looked at BufferedImage JavaDocs ant it appears that indeed, using 
StreamingOutput (which can be simpler than doing a custom 
MessageBodyWriter) can offer the best option, at least for a start.

Ex.

@Path("/images")
public ImagesResource {
    @Path("{id}")
    public StreamingOutput getImage(@PathParam("id") int id) {

          BufferedImage image = retrieveImage(id);
          retrun new StreamingOutputImpl(image);
    }
}

private static class StreamingOutputImpl implements StreamingOutput {


private  BufferedImage image;


public StreamingOutputImplBufferedImage image ) {

this.image = image;

}


public void write(OutputStream output) throws IOException, 
WebApplicationException {

  // just guessing  here :-), the idea is to stream the BufferdImage instance 
to the output

  ImageProducer ip = image.getImageProducer();

  im.addImageConsumer(new ImageConsumerImpl(output));

  ip.startProduction();

}

hope it helps, Sergey

>
> Hello,
>
> I am brand new to CXF and somewhat new to Java.  I have a requirement to
> write a REST webservice that returns a streamed image.  My dynamically
> created image is of type BufferedImage.  I don't want to persist the image
> but simply stream it to the browser.  Can anyone help guide me down the
> right path?
>
> I'm able to write the REST webservice, but I'm unsure what annotations to
> use and what return type to specify on my REST webservice method.  I've
> looked over the forum a bit and it looks like I should attempt to use the
> StreamingOutput class, but the implementation details are a little vague.
>
> Any guidance would be greatly appreciated!
>
> -Colin
> -- 
> View this message in context: 
> http://old.nabble.com/Return-an-Image-from-a-REST-webservice-api-tp26160563p26160563.html
> Sent from the cxf-user mailing list archive at Nabble.com.
> 

Reply via email to