0

我尝试使用此分页代码从 mysql 数据库中获取图像(blob 类型),并显示在我的 jsp 页面上。我想让它像 Yotube 之类的网站一样发生,当用户搜索视频文件时,用户可以以分页方式查看缩略图格式的视频列表。我想做这样的事情。为此,我尝试了以下代码,但仅显示数据库中的一张图像。

        <%@page import="java.io.InputStream"%>
        <%@page import="java.io.OutputStream"%>
        <%@page contentType="text/html" pageEncoding="UTF-8"%>
        <%@page import="java.util.*,xugglerPack.bean"%>
        <%@page import="java.sql.*"%>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
         "http://www.w3.org/TR/html4/loose.dtd">

       <%
          String myfile = session.getAttribute("myfile").toString();
       %>

       <html lang="en-US" xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
       <head>

       <meta http-equiv="Content-type" content="text/html; charset=utf-8" />

       </head>
       <body>
       <div>




       <div>
            <ul>
                <li>
                    <%String user = (String) session.getAttribute("thisuser");%>
                    <%=user%>
                </li>

                <li>
                    <div>
                        <a href="logout.jsp"> Logout </a>
                    </div>

                </li>


            </ul>


        </div>

        <div>&nbsp;</div>
      </div>
      <div>

        <%!
            public int nullIntconvert(String str) {
                int num = 0;
                if (str == null) {
                    str = "0";
                } else if ((str.trim()).equals("null")) {
                    str = "0";
                } else if (str.equals("")) {
                    str = "0";
                }
                try {
                    num = Integer.parseInt(str);
                } catch (Exception e) {
                }
                return num;
            }
        %>
        <%

                    Blob image = null;



                    byte[] imgData = null;

                    Statement stmt = null;

                    ResultSet rs = null;


                    Connection conn = null;
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                    conn =  

           DriverManager.getConnection("jdbc:mysql://localhost:3306/lakhya","root",    
           "pass");
                    ResultSet rs1 = null;
                    ResultSet rs2 = null;
                    PreparedStatement ps1 = null;
                    PreparedStatement ps2 = null;

                    int showRows = 2;
                    int totalRecords = 10;
                    int totalRows = nullIntconvert(request.getParameter("totalRows"));
                    int totalPages = 
                    nullIntconvert(request.getParameter("totalPages"));
                    int iPageNo = nullIntconvert(request.getParameter("iPageNo"));
                    int cPageNo = nullIntconvert(request.getParameter("cPageNo"));

                    int startResult = 0;
                    int endResult = 0;
                    if (iPageNo == 0) {
                        iPageNo = 0;
                    } else {
                        iPageNo = Math.abs((iPageNo - 1) * showRows);
                    }
                    String query1 = "SELECT SQL_CALC_FOUND_ROWS thumbnail FROM files 
           where fname like '%" + myfile + "%' limit " + iPageNo + "," + showRows + "";
                    ps1 = conn.prepareStatement(query1);
                    rs1 = ps1.executeQuery();

                    String query2 = "SELECT FOUND_ROWS() as cnt";
                    ps2 = conn.prepareStatement(query2);
                    rs2 = ps2.executeQuery();
                    if (rs2.next()) {
                        totalRows = rs2.getInt("cnt");

                    }
                    if (totalRows == 0) {
        %>
        <table style="margin-left:450px">
            <tr>
                <td><b><font color="whitesmoke">No Results..</font> </b></td>
            </tr>

        </table>


        <%                                                            } else {
        %>
        <form>
            <input type="hidden" name="iPageNo" value="<%=iPageNo%>"/>
            <input type="hidden" name="cPageNo" value="<%=cPageNo%>"/>
            <input type="hidden" name="showRows" value="<%=showRows%>"/>
            <table style="margin-left:450px;margin-top: 15px;"width="50%" 
            cellpadding="0" cellspacing="0">

                <%

                                                String imgLen = "";
                                                if (rs1.next()) {
                                                    imgLen = 
                                                rs1.getString("thumbnail");

                                                    System.out.println("the image    
                                                length is" + imgLen.length());
                                                }
                                                String query11 = "SELECT thumbnail FROM 
                                            files where fname like '%" + myfile + "%'";
                                                ps1 = conn.prepareStatement(query11);
                                                rs1 = ps1.executeQuery();
                                                while (rs1.next()) {
                                                int len = imgLen.length();
                                                byte[] rb = new byte[len];
                                                InputStream readImg = 
                                                  rs1.getBinaryStream(1);
                                                int index = readImg.read(rb, 0, len);
                                                System.out.println("the index is " + 
                                                index);
                                                //stmt.close();
                                                response.reset();
                                                response.setContentType("image/jpg");

                %>
                <tr>
                    <td style="padding-bottom:2em">
                        <%  response.getOutputStream().write(rb, 0, len);


                response.getOutputStream().flush();
                        %>
                    </td>
                </tr> 

                <%
                                }

                            }

                %>



                <%
                            try {
                                if (totalRows < (iPageNo + showRows)) {
                                    endResult = totalRows;
                                } else {
                                    endResult = (iPageNo + showRows);
                                }
                                startResult = (iPageNo + 1);
                                totalPages = ((int) (Math.ceil((double) totalRows / 
                            showRows)));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                %>

            </table>
            <br/>
            <table style="margin-left:450px">
                <tr>
                    <td colspan="1">
                        <div>
                            <%
                                        int i = 0;
                                        int cPage = 0;
                                        if (totalRows != 0) {
                                            cPage = ((int) (Math.ceil((double) 
                                      endResult / (totalRecords * showRows))));

                                            int prePageNo = (cPage * totalRecords) - 

                                            ((totalRecords - 1) + totalRecords);
                                            if ((cPage * totalRecords) - (totalRecords) 
                                       > 0) {
                            %>
                            <a href="add2list.jsp?iPageNo=<%=prePageNo%>&cPageNo=
                            <%=prePageNo%>"> << Previous</a>
                            <%
                                                                            }
                             for (i = ((cPage * totalRecords) - (totalRecords - 1)); 
                             i <= (cPage * totalRecords); i++)
                                {
                             if (i == ((iPageNo / showRows) + 1)) 
                                 {%>
                            <a href="add2list.jsp?iPageNo=<%=i%>" 
                             style="cursor:pointer;color: red"><b><%=i%></b></a>
                            <%


                             } else if (i <= totalPages) {
                            %>
                            <a href="add2list.jsp?iPageNo=<%=i%>"><%=i%></a>
                            <%
                                                                                }
                                                                            }
                                                                            if 
                             (totalPages > totalRecords && i < totalPages) {
                            %>

                            <a href="add2list.jsp?iPageNo=<%=i%>&cPageNo=<%=i%>"> >> 
                             Next</a>
                            <%
                                            }
                                        }
                            %>
                            <font color="whitesmoke"><b>Rows <%=startResult%> - 
                             <%=endResult%> Total Rows <%=totalRows%> </b></font>

                        </div>
                    </td>
                </tr>
            </table>

        </form>

        <%
                    try {
                        if (ps1 != null) {
                            ps1.close();
                        }
                        if (rs1 != null) {
                            rs1.close();
                        }

                        if (ps2 != null) {
                            ps2.close();
                        }
                        if (rs2 != null) {
                            rs2.close();
                        }

                        if (conn != null) {
                            conn.close();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                 %>

            </div>

           </body>
           </html>
4

0 回答 0