2013-02-09 30 views
0

我在这些代码中使用jsp和mysql。我一次只得到一个图像,但所有数据检索...你能告诉我如何在这个代码中显示多个图像? 我的数据:imagename varchar(),描述varchar(),imageid varchar(),category varchar图像blob()。 index.jsp:如何在jsp中显示来自mysql的多个图像和数据

<%@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> 
+0

[显示在HTML和JSP代码图像]的可能重复(http://stackoverflow.com/questions/1232591/displaying-image-in-html-and-jsp-code) – 2013-02-09 10:19:08

回答

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 
相关问题