xinhao_
你们都是坏人
采纳率33.3%
2012-08-31 11:28 阅读 181
已采纳

从oralce中读取blob多图,只显示一张图的问题

[code="java"]<%@ page language="java" pageEncoding="UTF-8" contentType="image/jpeg" import="java.awt.image.*,java.sql.*,com.sun.image.codec.jpeg.*,java.util.*,javax.imageio.*,java.io.*"%>

<%

Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
conn=sql.getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);  

BufferedInputStream inputimage=null;
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try
{
int i=0;
rs = stmt.executeQuery(" SELECT Pic from V_USER WHERE USER='MAX'");

while (rs.next())
{
i++;
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("Pic");
inputimage = new BufferedInputStream(blob.getBinaryStream());
}rs.close();
}
catch (Exception ex)
{
System.out.println("blobRead()'s exception"+ex);
conn.rollback();
throw ex;
}
conn.setAutoCommit(defaultCommit);
// Send back image
BufferedImage image = null;
try
{
image = ImageIO.read(inputimage);
}
catch(IOException e)
{
System.out.println(e);
}
ServletOutputStream sos = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
encoder.encode(image);
inputimage.close();
%> [/code]

SELECT Pic from V_USER WHERE USER='MAX' 的值有多笔资料,
在页面上显示时,只显示最后一张图,请问是何原因啊!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    jinnianshilongnian jinnianshilongnian 2012-08-31 12:57

    问题1:
    你是通过jsp页面输出图像的,因此一个页面仅能输出一个图片;

    问题2:
    如下语句 将从oracle获取最后一个blob数据给inputimage,因此只输出最后一次的数据
    while (rs.next())

    {

    i++;

    oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("Pic");

    inputimage = new BufferedInputStream(blob.getBinaryStream());

    }rs.close();

    }

    解决方案:

    1、在要显示图片的网页使用:
    for 图片id 图片idList {

    }

    2、请求JSP时带上图片的id
    如?id=123

    3、jsp根据id查一个图片写出

    点赞 评论 复制链接分享
  • iteye_5246 iteye_5246 2012-08-31 11:37

    //原因是:while循环重复给一个 inputimage 赋值导致,在循环到下一条记录会把上一条记录覆盖的原因
    //解决方法:用集合保存
    [code="html"]
    <%@ page language="java" pageEncoding="UTF-8" contentType="image/jpeg" import="java.awt.image.*,java.sql.*,com.sun.image.codec.jpeg.*,java.util.*,javax.imageio.*,java.io.*"%>

    <%

    Connection conn=null;
    Statement stmt=null;
    ResultSet rs=null;
    conn=sql.getConnection();
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);  
    

    BufferedInputStream inputimage=null;
    boolean defaultCommit = conn.getAutoCommit();
    conn.setAutoCommit(false);
    ArrayList imagestList=new ArrayList();

    ArrayList imageList=new ArrayList();
    try
    {
    int i=0;
    rs = stmt.executeQuery(" SELECT Pic from V_USER WHERE USER='MAX'");

    while (rs.next())
    {
    i++;
    oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("Pic");
    inputimage = new BufferedInputStream(blob.getBinaryStream());
    //原因是:while循环重复给一个 inputimage 赋值导致,在循环到下一条记录会把上一条记录覆盖的原因
    //解决方法:用集合保存
    imagestList.add(inputimage);
    }rs.close();
    }
    catch (Exception ex)
    {
    System.out.println("blobRead()'s exception"+ex);
    conn.rollback();
    throw ex;
    }
    conn.setAutoCommit(defaultCommit);
    // Send back image
    BufferedImage image = null;
    try
    {
    for(int i=0;i<imagestList.size();i++)
    {
    image = ImageIO.read(inputimage);
    imageList.add(image);//存放所有图片
    }

    }
    catch(IOException e)
    {
    System.out.println(e);
    }
    ServletOutputStream sos = response.getOutputStream();
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
    encoder.encode(image);
    inputimage.close();
    %>

    [/code]

    点赞 10 评论 复制链接分享

相关推荐