0

我在这些代码中使用 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>
4

1 回答 1

1

其实并不难。我们将使用两个jsps文件首先显示图像,此页面用于获取图像。

<%@ Page import="java.sql.*" %>
    <%@ Page import="java.io.*" %>

    <html>
    <%
    byte[] imgData = null;
    try 
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con =  
        DriverManager.getConnection("jdbc:mysql://localhost/try","root","root");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select photo from employee where employee_id=" + request.getParameter("empId"));
        while (rs.next()) 
        {
            Blob image = rs.getBlob(1);
            imgData = image.getBytes(1,(int)image.length());
        }
        response.setContentType("image/png");
        OutputStream o = response.getOutputStream();
        o.flush();
        o.close();
        rs.close();
        stmt.close();
        con.close();
    } 
    catch (Exception e) 
    {
        out.println("Unable To Display image");
        out.println("Image Display Error=" + e.getMessage());
        return;
    } 
    %>
    </html>














































and here i getting all ids to get all images






 <%@ page import="java.sql.*"%>
    <%@ page import="java.io.*"%>
    <html>
    <%
    try 
    {
        String EmpFirstName;
        String EmpSurname;
        String EmpId;
        Class.forName("com.mysql.jdbc.Driver");
        Connection con =  DriverManager.getConnection("jdbc:mysql://localhost/try","root","root");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select employee_id,first_name,surname from employee");
        while (rs.next()) 
        {
            EmpFirstName = rs.getString("first_name");
            EmpSurname = rs.getString("surname");
            EmpId = rs.getString("EmpId");
            <DIV><%=EmpFirstName5> <%=Surname%> </DIV>
            <img src="http://localhost/GetImage.jsp?empId=<%=EmpId%>" />
        }
        rs.close();
        stmt.close();
        con.close();
    } 
    catch (Exception e) 
    {
        out.println(e.Message);
        return;
    } 
    %>
    </html>


the code and its explanation  taken from 
http://stackoverflow.com/users/535152/tom%c3%a1s
thank you very much 
于 2017-02-27T12:40:10.513 回答