Stream / provide image via url e.g. as webservice?

Hi,

I have a vaadin application where you can upload and display images. The images are uploaded to folder at the server outside the vaadin webapp folder. E.g. /myfolder/myimage.jpg.

The images are displayed with

public void attach() {
	super.attach();
  Embedded image = new Embedded();
  File imgFile = new File("/mypath/myimage.jpg");
  final FileResource imageResource = new FileResource(imgFile, getApplication());
  image.setSource(imageResource);
  this.addComponent(image);
}

Now the image(s) should be provided as URL for other pages, Android Apps aso. E.g.
http://mypage.com/showimage/232

Is there a good solution to do this in vaadin? One idea was to create a webservice. Can anyone give me a clue?

Greetings and thanks a lot
Hartmut

Hi Hartmut,

in Vaadin 7 beta 5 something like this works with a request handler.

see https://vaadin.com/forum/-/message_boards/view_message/2009671 the helpful answers from Leif Astrand

heres a little test code:

public class RequestTest implements RequestHandler {

	public RequestTest() {
		
	}

	@Override
	public boolean handleRequest(VaadinServiceSession session,
			VaadinRequest request, VaadinResponse response) throws IOException {

		String s = request.getRequestPathInfo();
		
		if (s.contains("/img/")){
		    BufferedImage bi = new BufferedImage(300, 200, BufferedImage.TYPE_3BYTE_BGR);
                    bi.getGraphics().drawString(s, 10, 10);
                    response.setContentType("image/png");
                    response.setCacheTime(-1);
                    ImageIO.write(bi, "png", response.getOutputStream());
		    return true; // in case this method actually  handled the respective request, otherwise false leaves it to other handlers
		}
        return false;
	}

You can then add this handler with getSession().addRequestHandler(new RequestTest()) in your UI init.

Have a nice day,
Thomas

Thank you for your help !

Are there pros and cons between the VaadinRequest.getRequestPathInfo() solution and the solution with a webservice? I found a very short solution, doing it with a webservice.
http://www.mkyong.com/webservices/jax-rs/download-image-file-from-jax-rs/

What would be the better solution. The only con for webservice is, that i have to create a new DAOFactory Object for every request. Is that right?

Btw. i have to do it with Vaadin 6.x.x, not with version 7. There is no getSession().addRequestHandler() function at version 6, right?

For Vaadin 6, see e.g. Window.addURIHandler().

@Hartmut, IMO there is no need to use a separate webservice here for the pure vaadin solution scales and performs well and is painless to implement.

Thank you all for your answers and help. I will try both solutions to see which one works better for my application.