Sie sind auf Seite 1von 3

A simple way to display database blob stored image in a jsp - Francois Degrelle's blog

Francois Degrelle's
Vous aimerez aussi…
J'aime ce blog
blog about some Oracle technologies
Rechercher ok

Information is not knowledge, Knowledge is not wisdom, Wisdom is not truth, Truth is not beauty, Beauty is not love, Love is not music, Music is THE

BEST!

Articles Mardi 11 octobre 2005


Recent articles
English articles Forms : LongBridge
French articles Consulting's
A simple way to display database blob stored image in a jsp Image/Document
Want to step on the Java JavaBeans NEW Version
stage? 2.2
One of my recent assignments was to build a web page that needed to show images that
Oracle Forms Look and
are stored in a database. Feel version 1.5.3
Oracle Forms Look and
Having a strong PLSQL coding background the solution seemed to be close to my Feel version 1.5.2
fingertips and I created a PLSQL function that I called through the PLSQL Gateway. Forms : LongBridge
"FormsImageBean" Java
Bean NEW Version 2.1
Forms LAF : The winner is
CREATE TABLE PHOTOS ... Germany!
Forms PJC/Java Beans :
(
New Discussion Forum
IMAGEID NUMBER(10), Oracle Forms Look and
IMAGE BLOB Feel version 1.5.1
List of all blog entries ) Oracle 11g Streams
Implementer's Guide
Some Forms books / Oracle Forms : How to
simulate a key pressed
Links CREATE OR REPLACE PROCEDURE Display_Image(p_id NUMBER) IS Rodha Scott : I finally
Frank Nimphius managed to see her...
Photo BLOB
Grant Ronald liste complète
v_amt NUMBER DEFAULT 4096;
Jan Carlin
Duncan Mills new
v_off NUMBER DEFAULT 1; Categories
Duncan Mills v_raw RAW(4096); Oracle Forms (176)
Gerd Volberg BEGIN Oracle PL/SQL (20)
Hafed M Benteftifa J2EE (13)
-- Get the blob image
Mark Striekwold Other (28)
Andreas Weiden SELECT image
Laurent Schneider INTO Photo Calendar
Wilfred van der Deijl FROM PHOTOS
Romain Guy (Swing) Mai 2010
WHERE IMAGEID = p_id;
L M M J V S D
The PJC and Java Beans'
library owa_util.mime_header('images/gif'); 1 2
BEGIN 3 4 5 6 7 8 9
Search LOOP 10 11 12 13 14 15 16

Go -- Read the BLOB 17 18 19 20 21 22 23

dbms_lob.READ(Photo, v_amt, v_off, v_raw); 24 25 26 27 28 29 30

-- Display image 31

htp.prn(utl_raw.cast_to_varchar2(v_raw)); << < > >>

v_off := v_off + v_amt;


v_amt := 4096;
END LOOP;
dbms_lob.CLOSE(Photo);
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
END;
END;
/

The web page could be called with the following URL and did what I was asked to
do

img src=" http://machine:port/pls/myapp/display_image?p_id=12 "


width="115" border="0"

This works like a charm but has a caveat that I discovered when presenting it to
the network department. The network department didn’t like the idea of exposing
the database server to the Internet, which indeed is considerably unsafe.

http://fdegrelle.over-blog.com/article-992927.html[5/20/2010 5:51:33 AM]


A simple way to display database blob stored image in a jsp - Francois Degrelle's blog

Back to the whiteboard, I thought of using Web Service. This approach just didn’t
feel right and appeared to be too complex for this little solution to build.
Eventually I decided to write a JavaServer Page to do the job.

The Java class to stream the image from the database column
package image;
import java.sql.*;
import java.io.*;
import java.util.*;
import oracle.jdbc.*;
import oracle.sql.*;
public class images
{
/*-------------------------
* Get the Blob image
*------------------------*/
public static byte[] getPhoto (OracleConnection conn, int iNumPhoto)
throws Exception, SQLException
{
String req = "" ;
Blob img ;
byte[] imgData = null ;
Statement stmt = conn.createStatement ();

// Query
req = "Select image From IMAGES Where ImageID = " + iNumPhoto ;

ResultSet rset = stmt.executeQuery ( req );

while (rset.next ())


{
img = rset.getBlob(1);
imgData = img.getBytes(1,(int)img.length());
}

rset.close();
stmt.close();

return imgData ;
}

The JavaServer Page includes the bean so its methods can be accessed in the
JSP page using scriplets and “photo” as a named bean reference

<%@ page import = "image.*" %>


<%@ page import = "java.io.*" %>
<%@ page import = "oracle.jdbc.OracleConnection" %>
<jsp:useBean id="photo" class="image.images" scope="session" />
<%

int iNumPhoto ;
oracle.jdbc.driver.OracleConnection conn = null;

if ( request.getParameter("imgID") != null )
{

iNumPhoto = Integer.parseInt(request.getParameter("imgID")) ;

try
{

http://fdegrelle.over-blog.com/article-992927.html[5/20/2010 5:51:33 AM]


A simple way to display database blob stored image in a jsp - Francois Degrelle's blog

conn = …………;
conn.setAutoCommit (false);

// get the image from the database


byte[] imgData = photo.getPhoto( conn, iNumPhoto ) ;
// display the image
response.setContentType("image/gif");
OutputStream o = response.getOutputStream();
o.write(imgData);
o.flush();
o.close();
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
… Close the connexion … ;
}
}
%>

To display the image on the web, I now use the following image URL

img src="image.jsp?imgID=12" width="115" border="0"

Special thank you to Frank Nimphius for its good suggestions on the style ;o)
Francois

Par Francois Degrelle - Publié dans : J2EE


Ecrire un commentaire - Voir les 5 commentaires - Partager

Précédent Retour à l'accueil Suivant

my English articles my French articles

Créer un blog gratuit sur over-blog.com - Contact - C.G.U. - Rémunération en droits d'auteur - Signaler un abus - Articles les plus commentés

http://fdegrelle.over-blog.com/article-992927.html[5/20/2010 5:51:33 AM]

Das könnte Ihnen auch gefallen