使用jsp-servlet从数据库检索多个图像 [英] retrieval of multiple images from the database using jsp-servlet

查看:158
本文介绍了使用jsp-servlet从数据库检索多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看下面的代码片段,用于从数据库中检索图片:

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.

推荐答案

为什么会显示多张图片?您正在将响应的内容类型设置为image / gif,这意味着浏览器需要一张单个图片。但是,您正在将多个图像流式传输到响应流中。

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).

您需要识别哪个图片您可以针对每个请求进行检索,并修改上述内容以提取单个图片(通过适当地修改SQL)。

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).

这篇关于使用jsp-servlet从数据库检索多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆