如何从mysql db检索图像并将其显示在HTML的标记内,并且应将img标记置于其中?这是我的代码:它仅显示图像。除了图像以外,没有显示任何其他内容。
提前致谢。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <%@ page import="java.io.*"%> <%@ page import="java.sql.*"%> <%@ page import="java.util.*"%> <%@ page import="java.text.*"%> <%@ page import="javax.servlet.*"%> <%@ page import="javax.servlet.http.*"%> <%@ page import="javax.servlet.http.HttpSession"%> <%@ page language="java"%> <%@ page session="true"%> <% try{ //PrintWriter out=response.getWriter(); out.println("Retrieve Image Example!"); String driverName = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/"; String dbName = "db"; String userName = "root"; String password = "root"; Connection con = null; Class.forName(driverName); con = DriverManager.getConnection(url+dbName,userName,password); Statement st = con.createStatement(); %> <table border='1'> <tr> <td>Name:</td><td> </td>My Name</td> </tr> <tr> <td>Image:</td> <td width=10px;> <% PreparedStatement pre1 = con.prepareStatement("select * from image where id="+8); ResultSet rs1=pre1.executeQuery(); while(rs1.next()) {byte[] bytearray1 = new byte[4096]; int size1=0; InputStream sImage1; sImage1 = rs1.getBinaryStream(2); response.reset(); response.setContentType("image/jpeg"); response.addHeader("Content-Disposition","filename=logo.jpg"); while((size1=sImage1.read(bytearray1))!= -1 ) { response.getOutputStream().write(bytearray1,0,size1); } response.flushBuffer(); sImage1.close(); rs1.close(); } out.println("Retrieved Successfully!"); pre.close(); con.close(); } catch (Exception e){ out.println(e.getMessage()); } %> </td></tr> </table> </body> </html>
图像作为单独的请求加载到加载html的请求中。因此,您需要:
src
img