如何将oracle数据库中的blob显示为jsp的图片,JSP读取Oracle数据库里的图片Blob字段并显示在页面上...

The original website:http://longdick.iteye.com/blog/315279

/**

*  转载请注明作者longdick    http://longdick.iteye.com

*

*/

首先定义一个读取Oracle数据库的Blob字段并把字节写入一个输出流的方法:

Java代码

publicstaticvoidwriteImg(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 =newbyte[1024];

if(rs.next()) {

Blob blob = rs.getBlob(1);

InputStream is = blob.getBinaryStream();

inti =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代码

publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)

throwsServletException, IOException {

DBOperator.writeImg(response.getOutputStream());

}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

DBOperator.writeImg(response.getOutputStream());

}

最后只需要在显示的jsp页面里面定义img标签的src属性为servlet的访问地址就可以了:

Html代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 JSP 页面读取数据库BLOB 类型的值并打印图片,可以使用以下步骤: 1. 在 JSP 页面引入数据库连接的 Java 类,并创建一个连接对象。 2. 写一个 SQL 查询语句,查询包含 BLOB 类型的字段的数据。 3. 执行查询语句,并使用 ResultSet 对象获取查询结果。 4. 使用 ResultSet 对象的 getBinaryStream 方法获取 BLOB 类型字段的二进制流。 5. 将二进制流输出到 JSP 页面,同时设置输出的内容类型为图片格式。 以下是一个简单的示例代码: ```java <%@ page import="java.sql.*" %> <% Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { // 创建连接对象 Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; conn = DriverManager.getConnection(url, user, password); // 查询BLOB类型的字段 String sql = "SELECT blob_field FROM my_table WHERE id = ?"; pstmt = conn.prepareStatement(sql); pstmt.setInt(1, 1); rs = pstmt.executeQuery(); // 获取BLOB类型字段的二进制流并输出 if (rs.next()) { InputStream is = rs.getBinaryStream("blob_field"); response.setContentType("image/jpeg"); OutputStream os = response.getOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) != -1) { os.write(buffer, 0, length); } os.close(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } %> ``` 注意,这只是一个简单的示例,实际应用需要根据具体情况进行修改。同时,需要确保数据库存储的 BLOB 类型数据是图片类型的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值