java显示图片 保存,图片是保存在数据库里的,请教怎么在页面上显示?多谢

Java codepackage com.learn.util;

import java.io.BufferedInputStream;

import java.io.IOException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletResponse;

import com.learn.db.ConnectionFactory; // 自己实现一个连接工厂类

public class LoadImage {

/*

if exists (select * from sysobjects where id = object_id('ImageContainer') and

OBJECTPROPERTY(id, 'IsUserTable') = 1) drop table obj

CREATE TABLE [ImageContainer] (

[rid] [int] IDENTITY (1, 1) NOT NULL ,

[rname] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,

[rpath] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,

[rfilesize] [int] NULL ,

[rbinary] [image] NULL

) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

*/

public void getFileFromDatabaseByRid(int rid,

HttpServletResponse response) {

String strSQL = null;

Connection conn = null;

PreparedStatement pstmt = null;

ResultSet rst = null;

ServletOutputStream sos = null;

BufferedInputStream bis = null;

try {

strSQL = "SELECT * FROM ImageContainer WHERE rid = ?";

conn = ConnectionFactory.getConnection(); // 取得Connection,代码略

pstmt = conn.prepareStatement(strSQL);

pstmt.setInt(1, rid);

rst = pstmt.executeQuery();

if (rst.next()) {

bis = new BufferedInputStream(rst.getBinaryStream("rbinary"));

sos = response.getOutputStream();

byte[] b = new byte[10 * 1024];

int len = 0;

while ((len = bis.read(b)) != -1) {

sos.write(b, 0, len);

}

bis.close();

sos.flush();

sos.close();

} else {

System.out.println("picture dose not exist!");

}

} catch (IOException ioe) {

ioe.printStackTrace();

} catch (SQLException sqle) {

sqle.printStackTrace();

} finally{

ConnectionFactory.close(rst);

ConnectionFactory.close(pstmt);

ConnectionFactory.close(conn);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值