从oracle数据库中读取图片,在jsp?

servlet:(jsp调用servlet显示图片,servlet如下)
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("image/gif");
InputStream is = null;
QueryPicture q = new QueryPicture();
try{
is = q.getPictureInfo("88888");
OutputStream o = response.getOutputStream();;

byte[] buf = new byte[is.available()];
int nRead = 0;
while( (nRead=is.read(buf)) != -1 )
{

o.write(buf, 0, nRead);
}
o.flush();
}
catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace() ;
}

}


javabean:如下
public void AddPicture() {//增加图片方法如下
Connection con;
byte[] b = null;
FileInputStream filePicture = null;
try {
con = createConnection();
File fl = new File("c:\\windows\\HLPBELL.GIF");
filePicture = new FileInputStream(fl);
if (b != null) b = null;
b = new byte[filePicture.available()];
filePicture.read(b);
PreparedStatement pstmt = con.prepareStatement(
"INSERT INTO map(p_number,pic) valueS(?,?)");

pstmt.setString(1, "88888");
pstmt.setBytes(2,b);
pstmt.executeUpdate();
System.out.println("ok");
pstmt.close();
}
catch (Exception ex) {
System.err.println("sql exception:" + ex.getMessage());
}
}

public InputStream getPictureInfo(String pictureID) {//读取图片代码如下
ResultSet rs = null;
Statement stmt = null;
String sql = null;
InputStream is = null;
InputStream out = null;
int szContent;
byte[] aCatContent;

try {
sql = "select p_number, pic from map where p_number='" +
pictureID.trim() + "'";
System.out.println(sql);
Connection conn = createConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
is = rs.getBinaryStream("pic");
szContent = is.available();
System.out.println(szContent);
aCatContent = new byte[szContent];
is.read(aCatContent);
is.close();
System.out.println(aCatContent.length);
out = new ByteArrayInputStream(aCatContent);
System.out.println(out.available());
}
}
catch (Exception e) {
e.printStackTrace();
}
return out;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值