cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sergey Beryozkin" <>
Subject RE: Return an Image from a REST webservice api
Date Tue, 03 Nov 2009 23:20:28 GMT
Sounds like a really good advice. By the way, CXF servlets will support
the redirection shortly, so, as far as serving the static resources is
concerned, one would have the option to redirect to a default servlet
(Tomcat/Jetty/etc) or indeed to some other servlet capable of streaming
static resources back to the client

Cheers, Sergey

-----Original Message-----
From: Ron Grimes [] 
Sent: 03 November 2009 18:27
Subject: RE: Return an Image from a REST webservice api


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

Ron Grimes

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


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.


public ImagesResource {
    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));



hope it helps, Sergey

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

View raw message