簡體   English   中英

未從jsp頁面的數據庫中加載圖像

[英]image is not loaded from database in jsp page

我正在嘗試從數據庫顯示表數據到jsp頁面,所有數據都已獲取,但圖像未加載到圖像選項卡中。 我在表中將圖像存儲為BLOB。

我的jsp頁面如下:

<body>
    <table border="1">  

    <%@ page import="com.*;"%>
    <%
        MySql myobj = new MySql();
        myobj.connect();

        String query="select contact_id,first_name,last_name,photo from contacts";

        ResultSet rs = myobj.getdata(query);

        while(rs.next())
        {               

        %>
            <tr>
                <td>
                    <%= rs.getInt(1) %>             
                </td>

                <td>
                    <%= rs.getString(2).toString() %>
                </td>
                <td>
                    <%= rs.getString(3).toString() %>
                </td>

                <td>
                    <img src="<%= rs.getString(4) %>"  width="500" height="300" alt="data"></img>                   
                </td>

            </tr>
        <% 

        }
    %>


    </table>
</body>

當單擊項目名稱或第二個div的數據時,我還必須執行下載,然后應下載相關數據。

誰能盡快解決我的這個問題?

如何在servlets中實現下載作為后端?

我已經為imageservlet編寫了以下代碼:

protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {   
        String id = request.getParameter("contact_id");     
        MySql myobj = new MySql();
        myobj.connect();
        PrintWriter out=response.getWriter();       
        try
        {
            String query="select photo from contacts where contact_id='"+id+"'";

            ResultSet rs = myobj.getdata(query);

            out.println(id);

            Blob image=null;
            byte[] imgData = null;
            image= rs.getBlob(1);
            imgData = image.getBytes(1, (int) image.length());
            response.setContentType("image/gif");
            OutputStream o = response.getOutputStream();
            o.write(imgData);
            o.flush();
            o.close(); 
            response.sendRedirect("Message.jsp");
        }
        catch (Exception e)
        {
            e.getMessage();
        }       
    }

您應該使用servlet通過適當的content-type標頭來提供圖像

<img src="img-servlet?contact_id=<%= rs.getInt(1) %>"  width="500" height="300" alt="data">

並且,在您的servlet中獲取contact_id查詢參數並獲取圖像內容

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM