JSP读取Oracle数据库里的图片Blob字段并显示在页面上
2011-12-16 15:56
549 查看
The original website:/article/4172090.html
/**
* 转载请注明作者longdick http://longdick.iteye.com
*
*/
首先定义一个读取Oracle数据库的Blob字段并把字节写入一个输出流的方法:
Java代码
public static void writeImg(OutputStream os) { Connection con; try { con = ConnectionFactory.getConnection(); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select pic from blob_table where b_id=1"); byte[] b = new byte[1024]; if (rs.next()) { Blob blob = rs.getBlob(1); InputStream is = blob.getBinaryStream(); int i = 0; while ((i = is.read(b)) != -1) { os.write(b, 0, i); } os.close(); is.close(); } rs.close(); stmt.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } }
然后做一个servlet,在doGet方法里调用writeImg方法
Java代码
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DBOperator.writeImg(response.getOutputStream()); }
最后只需要在显示的jsp页面里面定义img标签的src属性为servlet的访问地址就可以了:
Html代码
<img src="servlet/newServlet" >
/**
* 转载请注明作者longdick http://longdick.iteye.com
*
*/
首先定义一个读取Oracle数据库的Blob字段并把字节写入一个输出流的方法:
Java代码
public static void writeImg(OutputStream os) { Connection con; try { con = ConnectionFactory.getConnection(); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select pic from blob_table where b_id=1"); byte[] b = new byte[1024]; if (rs.next()) { Blob blob = rs.getBlob(1); InputStream is = blob.getBinaryStream(); int i = 0; while ((i = is.read(b)) != -1) { os.write(b, 0, i); } os.close(); is.close(); } rs.close(); stmt.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } }
public static void writeImg(OutputStream os) { Connection con; try { con = ConnectionFactory.getConnection(); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select pic from blob_table where b_id=1"); byte[] b = new byte[1024]; if (rs.next()) { Blob blob = rs.getBlob(1); InputStream is = blob.getBinaryStream(); int i = 0; while ((i = is.read(b)) != -1) { os.write(b, 0, i); } os.close(); is.close(); } rs.close(); stmt.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } }
然后做一个servlet,在doGet方法里调用writeImg方法
Java代码
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DBOperator.writeImg(response.getOutputStream()); }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DBOperator.writeImg(response.getOutputStream()); }
最后只需要在显示的jsp页面里面定义img标签的src属性为servlet的访问地址就可以了:
Html代码
<img src="servlet/newServlet" >
相关文章推荐
- 把图片文件存入oracle数据库 然后读取并显示在jsp页面
- java以Blob形式存储,读取图片并在jsp页面显示图片流
- java以Blob形式存储,读取图片并在jsp页面显示图片流
- 从Oracle中数据库中读取以Blob类型存储的图片显示在jsp页面中
- 把mysql读取的图片显示在jsp 页面上
- (springmvc)从oracle读取blob类型图片并在jsp中显示
- JAVA读取Oracle中的blob图片字段并显示
- Jsp 读取oracle数据库图片,显示出来
- SpringMVC从数据库中读取图片显示到JSP页面上
- 在jsp中显示数据库Blob字段图片实例
- JAVA读取Oracle中的blob图片字段并显示
- (二)上传图片至服务器,写入到数据库Blob字段中,以及从数据库读取Blob信息(iframe父子页面传值)
- java 通过流的方式读取远程图片并显示在jsp页面(类型以jpg、png等结尾的图片)
- struts2从数据库中读取mysql的Blob格式图片和sqlserver中image格式图片显示到jsp上
- JAVA读取Oracle中的blob图片字段并显示
- 救命呀!关于从数据库读取Word文档(BLOB)在JSP页面显示
- JAVA读取Oracle中的blob图片字段并显示
- java 通过流的方式读取本地图片并显示在jsp 页面上(类型以jpg、png等结尾的图片)
- JAVA读取Oracle中的blob图片字段并显示
- JAVA读取Oracle中的blob图片字段并显示