用JSP从数据库中读取图片并显示在网页上

5 篇文章 0 订阅

环境:mysql+tomcat: 


<1>先在mysql下建立如下的table. 并insert图像. 
mysql.sql文件如下: 
CREATE TABLE photo ( 
photo_no int(6) unsigned NOT NULL auto_increment, 
image blob, 
PRIMARY KEY (`photo_no`) 
) 

<2>把show.jsp放在tomcat的任意目录下. show.jsp作用:从数据库中读出blob,并产生image/jpg. 

show.jsp文件如下: 

<%@ page contentType="text/html; charset=gbk" %> 
<%@ page import="java.io.*"%> 
<%@ page import="java.sql.*, javax.sql.*" %> 
<%@ page import="java.util.*"%> 
<%@ page import="java.math.*"%> 
<% 
String photo_no = request.getParameter("photo_no"); 
//mysql连接 
Class.forName("com.mysql.jdbc.Driver").newInstance(); 
String URL="jdbc:mysql://localhost:3306/job?user=root&password=111111"; 
Connection con = DriverManager.getConnection(URL); 

//oracle连接 
//String URL="jdbc:oracle:thin@localhost:1521:orcl2"; 
//user="system"; 
//password="manager"; 
//Connection con = DriverManager.getConnection(URL,user,password); 


try{ 
// 准备语句执行对象 
Statement stmt = con.createStatement(); 
String sql = " SELECT * FROM PHOTO WHERE photo_no = "+ photo_no; 
ResultSet rs = stmt.executeQuery(sql); 
if (rs.next()) { 
Blob b = rs.getBlob("photo_image"); 
long size = b.length(); 
//out.print(size); 
byte[] bs = b.getBytes(1, (int)size); 
response.setContentType("image/jpeg"); 
OutputStream outs = response.getOutputStream(); 
outs.write(bs); 
outs.flush(); 
rs.close(); 
} 
else { 
rs.close(); 
response.sendRedirect("./images/error.gif"); 
} 
} 
finally{ 
con.close(); 
} 
%> 


<3>把如下文件放在show.jsp的同一目录下. 
index.html文件如下: 

<html>
<head>
<title> 图像测试 </title>
</head>
<body>
<table>
<tr>
	<td>
		图像测试
	</td>
</tr>
<tr>
	<td>
		<img src="show.jsp?photo_no=2">
	</td>
</tr>
</table>
</body>
</html>



  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
您可以使用JSP和Java代码从数据库读取图片并将其显示网页上。以下是一些步骤: 1. 在数据库创建一个BLOB类型的字段,用于存储图片的二进制数据。 2. 将图片转换为字节数组并将其保存到数据库。您可以使用Java的FileInputStream和ByteArrayOutputStream类来完成此操作。 3. 在JSP页面,使用Java代码从数据库检索图像数据。您可以使用JDBC API的ResultSet类来检索数据。 4. 将字节数组转换为图像,并将其显示网页上。您可以使用Java的ImageIO类来完成此操作。 下面是一个简单的示例代码,它演示了如何从数据库读取图像并将其显示JSP页面上: ``` <%@page import="java.sql.*"%> <%@page import="java.io.*"%> <%@page import="javax.imageio.*"%> <%@page import="javax.servlet.http.*"%> <% // 获取图像ID int imageId = Integer.parseInt(request.getParameter("imageId")); // 从数据库检索图像数据 byte[] imageData = null; try { Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase", "myuser", "mypassword"); PreparedStatement stmt = conn.prepareStatement("SELECT image_data FROM images WHERE image_id = ?"); stmt.setInt(1, imageId); ResultSet rs = stmt.executeQuery(); if (rs.next()) { imageData = rs.getBytes("image_data"); } rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } // 将字节数组转换为图像 BufferedImage image = null; try { ByteArrayInputStream bis = new ByteArrayInputStream(imageData); image = ImageIO.read(bis); } catch (IOException e) { e.printStackTrace(); } // 在网页显示图像 response.setContentType("image/jpeg"); OutputStream out = response.getOutputStream(); ImageIO.write(image, "jpg", out); out.close(); %> ``` 在上面的示例,我们从请求参数获取图像ID,并使用JDBC API从数据库检索图像数据。然后,我们将图像数据转换为图像,并将其写入响应流,以便在网页显示。请注意,我们将响应类型设置为“image/jpeg”,因为我们使用的是JPEG格式的图像。如果您使用的是其他格式的图像,请相应地更改响应类型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值