简体   繁体   中英

How do I embed images from database into web pages

One possible way of storing images in a database is as a stream of bits. This works as a storage mechanism, but I can't figure out how to embed this data into a webpage as an image once I extract it to fulfill a client request.

I'm working with Servlets in Java. Can anyone give me some guidance?

You probably want to write a servlet that reads the BLOB from the database and copies directly into HttpServletRequest.getOutputStream() , remembering to set the content type to the appropriate format (image/png for example).

The database id or key or whatever can be encoded in the path (/image/foo), or passed as a query parameter (/image?id=foo). This path is what you use in your <img> tag.

This is probably best written in actual Java, rather than JSP or similar presentation technologies.

Let me share what I am doing with image files and dynamic HTML generation and why - I believe this is a non-standard approach, feel free to comment or use such a system if it works for you. :)

I have several parameterized html, style sheets and image files that go into the html jarred up and saved as [clientid].jar file on the server side. The UI is applet based. At a lean time, the applet requests the file (and associated logic) from the servlet, after due authentication. The servlet wraps up the entire jar file as byte array, encapsulates its contents (from predefined directory in the unix FS) and the business logic (from the database) in a FileXfer object and sends it out on an ObjectOutputStream. The applet extracts the bytes and saves the jar as a tmp file in tmpdir, with a deleteOnExit flag.

During execution, when the html is required to be displayed, the applet extracts the necessary files from the archive and saves them in the same directory, filtering the html as required by the business logic. Image files (jpeg, png, etc) are not filtered. All such files are deleteOnExit, so there is no footprint once the application exits. Next it opens the html with a browser tab, and everything I need to display is there in the right format. The applet has the logic of file extraction - eg, do not extract "logo.png" if it was extracted 15 seconds back to display another piece of HTML, etc.

The advantages I see are:

  1. I get an automatic compression of the bytes I need to transfer from the server to the client, speeding up the transfer by about 3x, (jar uses zip-compression)

  2. The client (applet) picks up the load of filtering the html, thereby relieving the server of the same job

  3. No blob storage of image files on the DB (I read somewhere that blobs are not exactly efficient for DB operations)

  4. The html can be edited independently using standard img tags assuming the image file is in $cwd

  5. [clientid].jar file content is not included in the jar containing the applet class, allowing the applet to load and start up faster.

TIA for your comments, - MS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM