我在这些代码中使用 jsp 和 mysql。我一次只得到一张图像,但检索到所有数据......你能告诉我如何在这段代码中显示多张图像吗?我的数据:imagename varchar()、description varchar()、imageid varchar()、category varchar image blob()。index.jsp:
<%@page import="java.io.InputStream"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.io.OutputStream"%>``
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.
getConnection("jdbc:mysql://localhost:3306/rich","root","");
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("select *from publishers");
while(rs.next())
{
String imgLen=rs.getString(5);
int len = imgLen.length();
byte [] rb = new byte[len];
InputStream readImg = rs.getBinaryStream(5);
int index=readImg.read(rb, 0, len);
System.out.println("index"+index);
stmt.close();
response.reset();
response.setContentType("image/jpg");
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();
}
%>
</body>
</html>
**basic.jsp:**
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.io.OutputStream"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
< form action="index.jsp" method="post">
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/rich","root","");
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("select *from publishers");
while(rs.next())
{
%>
<table border="1">
<tr> <td>
<img src="index.jsp?" /> </td> </tr>
<tr>
<td> <%=rs.getString(1)%> </td>
<td> <%=rs.getString(2)%> </td>
<td> <%=rs.getString(3)%> </td>
<td> <%=rs.getString(4)%> </td>
</tr>
</table>
<%
}
%>
</form>
</body>
</html>