3

Have a look at the following code snippet which I use to retrieve images from a database:

response.setContentType("image/gif");
String url="jdbc:oracle:thin:@localhost:1521:xe";
String username="xyz";
String password="abc";

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection(url,username,password);
String sql="Select name,description,image from pictures";
PreparedStatement stmt=conn.prepareStatement(sql);
ResultSet resultSet=stmt.executeQuery();
ServletOutputStream sos=response.getOutputStream();

while(resultSet.next()) {
    byte[] buffer=new byte[1];

    InputStream is=resultSet.getBinaryStream(3);
    while(is.read(buffer)>0){
      sos.write(buffer);
}
sos.println();
sos.flush();
}

sos.close();
conn.close();

I am trying this code to display images which are retrieved from the database. This code is supposed to retrieve multiple images from multiple rows which are stored in the database. But this code displays a single image.

Richard
  • 56,349
  • 34
  • 180
  • 251
DEEPMALA
  • 53
  • 1
  • 1
  • 7

3 Answers3

4

Why would it display multiple images ? You're setting the content type of the response to image/gif, and that means the browser will expect a single image. However you're streaming multiple images into the response stream.

So I suspect the browser is taking just the first image. It could just as well reject the whole response as being corrupted (since we have multiple images streamed together).

You need to identify which image you retrieve for each request, and modify the above to extract a single image (by amending your SQL appropriately).

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • So how should I set the response type then so that it displays multiple images in a single page??It would be much better if I am able to display the name of that image as text.Is it possible?? – DEEPMALA Oct 02 '10 at 09:35
  • 1
    You need to (say) build an HTML page containing a list of references to your images (number 1,2,3) etc. So you'll need to have a JSP that requests the list of images and their name, and embeds links to those images. It's a 2-stage process. – Brian Agnew Oct 02 '10 at 09:41
2

You can't return multiple images at once. You can think of the access to the servlet as an access to a file.

Here you created a "file" which contains multiple images. Most of the image-renderers will only see the first image stored in the "file".

If you want to have more than one image, you have to do more than one request (or open more than one file).

Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
  • Well you could... if you put them all in one large image and then use CSS to show only the required part of it. But that's far from easy. – Gimby Jan 10 '13 at 12:15
2

As already pointed out by Brian and Colin you can't have multiple images , all being accessed simultaneously. It seems your task is to have many images in the same page. then one of the way as suggested by Brian (using JSP that calls for the images). The other way is to divide your page into multiple frame sets which themselves can call the images in the same way JSP does.

Ashish Yadav
  • 1,667
  • 6
  • 20
  • 26