I have images stored in a database byte{] field and can display them with
StreamResource stream = new StreamResource(source,person.getId() + ".jpg", app);
String filename = stream.getFilename();
Embedded theimage = new Embedded(filename, stream);
where source is returned by a class that implements the StreamSource Interface with the getStream method:
...
InputStream getStream() {
return new ByteArrayInputStream(image);
}
This works fine so far, but what i want to do later is generate a generic html output where i use the html img src tag instead of the embedded component.
For this i need the complete virtual filename. With the above embedded, the image in the browser has an URL like this for example:
that is, a virtual filename generated by vaadin with a changing path to make it unique.
BUT when i call the stream.getFilename(), it only returns "
31.jpg ".without the vaadin-generated path. Ths is of course not enough for my “img src” html tag that should be
in this case something like [code]
[/code]
My question is, how can i get the virtual filename including its virtual (and for each image changing) path for each of the StreamResource instances?
There is unfortunately no way of doing that right now when using StreamResource. You could however instead use a RequestHandler for serving images from the database as that gives you full control over the paths.
There is no StreamResource involved when using a RequestHandler - the handler just gets a request with parameters and can write to the outputStream of the response if it wants to, or just ignore the request if that is more appropriate.
This means that you can generate URLs like /imageFromDatabase?id=123 and then make the RequestHandler handle requests for /imageFromDatabase (based on VaadinRequest.getRequestPathInfo()) by getting the id parameter, fetching the image from the database and writing it directly to the output stream of the response (VaadinResponse.getOutputStream()) while ignoring requests that are to other paths.