1

I have a JSP page and it should get all the images from database and should to display on one table. My resultset object 'rs' is pointing to images. My code is like this:

String query = "select image from stock";
rst = stmt.executeQuery(query);
while(rst.next())
<%
<td><img height="89" src=<%rst.getString(1)%></td>
%>
  }

I know, getString will not work for BLOB type. I was even used getBinaryStream(), but not succeed. Any idea?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
janasainik
  • 811
  • 5
  • 20
  • 40

4 Answers4

4

User the following piece of code to convert Blob to byte[]:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
InputStream in = blob.getBinaryStream();
int n = 0;
while ((n=in.read(buf))>=0)
{
   baos.write(buf, 0, n);
}
in.close();
byte[] bytes = baos.toByteArray(); 

Use Servlet to write your image:

if (bytes != null && bytes.length > 0) {
 response.setContentType("image/jpg");
 response.getOutputStream().write(bytes);
 response.getOutputStream().flush();
 response.getOutputStream().close();
}

Use retrive your image using servlet request url in jsp:

<img src="imageDisplayProcess.do?pKey=imageId" width="117" height="160"
 onError="loadImage()" onAbort="loadImage()" />

imageDisplayProcess.do?pKey=imageId //should be your image servlet URL
Mohamed Saligh
  • 12,029
  • 19
  • 65
  • 84
  • Conversion blob to `byte[]` is unnecessary. Just write `getBinaryStream()` to `getOutputStream()`. – BalusC Dec 03 '10 at 21:23
2

Here is complete example ,

Note: Consider this example as reference only to understand the way,

jmj
  • 237,923
  • 42
  • 401
  • 438
1
  1. Make a servlet and map it, to, say, /image/*
  2. Use src="image/<%= imageId %>, where imageId is the unique id of each image in the db
  3. in the servlet getBinaryStream() and transfer it (for example using IOUtils.copy(..)) to response.getOutputStream()
  4. Set the Content-Type to image/jpeg or image/png (or whatever the type is)
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • InputStream sImage; byte[] bytearray = new byte[1048576]; int size=0; sImage = rst.getBinaryStream(2); response.reset(); response.setContentType("image/jpeg"); while((size=sImage.read(bytearray))!= -1 ){ response.getOutputStream().write(bytearray,0,size); – janasainik Dec 02 '10 at 08:18
  • like this i written, but i am getting "getOutputStream() has already been called for this response" exception. – janasainik Dec 02 '10 at 08:19
  • @Hermanth - show the whole servlet code by editing the original question. It's not readable here. – Bozho Dec 02 '10 at 08:27
0

Just put response type as "image/jpg" and retrieve column using ResultSet

demo code written bellow---

     if(rs.next()){
       int len = imgLen.length();
       byte [] rb = new byte[len];
       InputStream readImg = rs1.getBinaryStream(1);
       int index=readImg.read(rb, 0, len);
       response.setContentType("image/jpg");
       response.getOutputStream().write(rb,0,len);
       response.getOutputStream().flush();
       }
Kedar1442
  • 217
  • 2
  • 8