0

如何从 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>
4

2 回答 2

4

图像作为单独的请求加载到加载 html 的请求中。因此,您需要:

  • 建立在指定元素时使用src的URL 编码方案。img
  • 将 servlet(或类似的)映射到该 URL。servlet 根据在其响应中返回该图像的 URL 的参数加载图像。
于 2013-01-29T14:11:24.130 回答
0

我发现。

r.jsp:-

 <%@ 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>
  <table border=2>
  <tr><th>DISPLAYING IMAGE</th></tr>
  <tr><td>hi</td></tr>
  <tr><td>
 <img src="retrieve.jsp" width=130 height=130>
 </td></tr>
 </table>
</body>
</html>

检索.jsp:-

            <%@ 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">
     </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();
     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();  
        }
  pre.close();
  con.close();  
  }
 catch (Exception e){
         out.println(e.getMessage());
        }
%>
  </body>
    </html>
于 2013-01-31T06:05:03.043 回答