O
O
ogr2011-07-29 14:40:15
Images
ogr, 2011-07-29 14:40:15

Issuing images from the database by a servlet

There was a problem: the image is stored in the database in binary form. The servlet renders the page. How to add an image to this page? Those. so form the path to the image?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Kirill Mamaev, 2011-08-01
@ogr

Have you got hooked on GAE, and are you pulling pictures from the datastore? :)
I suggest this:
In
And on /images/picture.gif - a separate servlet is mapped, which gives only the picture in binary form.

A
Anatoly, 2011-07-29
@taliban

I don’t know how your servlets work, but in general there are two ways to display images from the database, simple but with a load and complex but with reduced resources:
1. we send the image through the server, using a separate url that generates the image title and spits out the data in binary form, the browser will do everything itself, the address will look like: mysite.com/get/image/1234/
2. Before output, save the image to a separate folder, and write the path to it on the page, you will get a normal address: mysite.com/images/1234.jpeg
Naturally, in the second approach, you need to take into account that there may not be an image, and with 404, do a redirect to the address that will create the image on disk.

A
asm0dey, 2011-07-29
@asm0dey

there is another option. pull out the image in binary form - base64 encode - img src via data url

A
Alexey Pomogaev, 2011-07-29
@Foror

You make a separate servlet that on doGet (or whatever the method is called for processing a client's request) reads the name / id of the image from the URL. Then he looks (in Map, for example) there is an image already saved to disk. If there is, it opens this file for reading. If not, then it opens the database for reading binary data and copies them to disk. Then it also opens the file on disk for reading.
Well, then, on the response we set the necessary header (image / jpeg, png, etc.), see details in mime. And we read data from disk and write to response.
Something like this :) Better yet, develop a more complex caching system and add a specific file name on disk. And then, with nginx, you can give this static.
location ~ ^/(img)/ {
root /usr/share/tomcat5.5/webapps/myproject/ROOT;
}
Where there is an img directory in the ROOT and, accordingly, when /img/some.jpg is requested, nginx will look for the some.jpg file in the ROOT/img folder and return it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question