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


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() {
  Embedded image = new Embedded();
  File imgFile = new File("/mypath/myimage.jpg");
  final FileResource imageResource = new FileResource(imgFile, getApplication());

Now the image(s) should be provided as URL for other pages, Android Apps aso. E.g.

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

Hi Hartmut,

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

see the helpful answers from Leif Astrand

heres a little test code:

public class RequestTest implements RequestHandler {

	public RequestTest() {

	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);
                    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,

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.

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.