jias_je 发表于 2013-2-6 10:09:11

jsp显示数据库图片 步骤

ps:这儿使用的数据库是oracle,以下是我实例中的大致代码块,

1,java 部分
public Blob openPic(String id){

String sql = "select..";//select语句略
try {

ps = ConnectDb.getConnect().prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = ps.executeQuery();
while (rs.next()) {
blob = rs.getBlob("tp1");
}

rs.close();
ps.close();


} catch (SQLException e) {
e.printStackTrace();
return null;
}
return blob;
}

2,jsp部分
Blob blob = (Blob)request.getAttribute("blob");
BufferedOutputStream bos = null;
InputStream is =null;
if(blob != null){
response.setContentType("image/gif"); //这句可 以不要,因为是img标签接收
try{
bos = new BufferedOutputStream      (response.getOutputStream());
is = blob.getBinaryStream();
byte[] buf = new byte;
    int i = 0;
    while((i=is.read(buf))!=-1){
   bos.write(buf,0,i);
    }
}catch(final IOException e) {

} finally {
try {
if (is != null)
is.close();
if (bos != null)
            bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
      
    }
}
   out.clear();
   out = pageContext.pushBody();//这句不能少

3,显示部分页面//这儿和2不是同一个页面
<img src={action path}</img>//action略,此处为显示部分,首先该action被触发后会显示第二部分传来的结果
页: [1]
查看完整版本: jsp显示数据库图片 步骤