JAVA怎么把图片从数据库中调用出来

JAVA怎么把图片从数据库中调用出来,第1张

1 一半图片都是把路径存放在数据库的 到时候取出路径就可以了

2 在数据库有blob格式可以存放图片 以二进制流的方式取出来

<% String zjbm = CheckParam(request.getParameter("zjbm"),"") String zpSql = "select zp from tjjryxxx where sfzh = '"+zjbm+"'" out.clear() response.setContentType("image/jpeg") response.setHeader("Content-Transfer-Encoding","base64") Connection connection = null PreparedStatement ps = null ResultSet rs = null Blob blob =null byte[] data = null try{ connection =getConn() ps = connection.prepareStatement(zpSql) rs = ps.executeQuery() while(rs.next()){blob = (Blob)rs.getBlob("zp") long nlen = blob.length() int nsize = (int) nlen data = blob.getBytes(1,nsize) OutputStream out1 = response.getOutputStream() BufferedOutputStream bos =null bos = new BufferedOutputStream(out1) bos.write(data,0,data.length) bos.close() rs.close() } }catch(Exception e){ e.printStackTrace()} %>

这是用Struts做的

<img src="<bean:write name="item" property="page"/>" width="100" height="100">

图片文件夹必须在服务器里

如果要存数据库的话,数据库存图片字段用blob形式的(照片:zp为例)。

而且不能直接存,在存之前zp字段先插入一个empty.BLOB(),

然后select ZP from 表 for update。再用输入流的形式写进去。

// 先检索出来字段,必须使用oracle的类:oracle.sql.BLOB

oracle.sql.BLOB blob = null

if (rs.next())

{

blob = (oracle.sql.BLOB) rs.getBlob("ZP")

// 到数据库的输出流


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10035460.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存