簡體   English   中英

ResultSet.next()不迭代

[英]ResultSet.next() not iterating

我的代碼:

public class DisplayImage extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String connectionURL = "jdbc:mysql://localhost:3306/ycards";
        java.sql.Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(connectionURL, "root", "letmein");
            PreparedStatement st1 = con.prepareStatement("select image from pictures");
            ResultSet rs1 = st1.executeQuery();
            String imgLen = "";
            while (rs1.next()) {
                imgLen = rs1.getString(1);
                int len = imgLen.length();
                byte[] rb = new byte[len];
                InputStream readImg = rs1.getBinaryStream(1);
                readImg.read(rb, 0, len);
                response.setContentType("image/png");
                response.getOutputStream().write(rb, 0, len);
                response.getOutputStream().flush();
            }
            st1.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

輸出: 在此處輸入圖片說明

它僅顯示第一行,而不顯示其余行,為什么要這樣做? 注意:就本例而言,密碼和用戶名不是真實的。

您正在ResultSet上進行迭代,因為next()將光標從其當前位置向前移動一行,並在讀取最后一個位置時返回false

問題是您在每次迭代時都刷新httpResponse.outputstream
並調用flush()PrintWriter提交的HTTP響應。
因此,第一次迭代后您將無法編寫任何內容。

我不認為你可以發送倍數圖像與圖像內容類型:

response.setContentType("image/png");

或者,如果您想使用此內容類型,則可以創建一個將所有內容組合在一起的大圖像。
但這真的有意義嗎?

我懷疑您不需要使用:

response.setContentType("image/png");

但是您應該使用應該首先生成的圖像URL在客戶端頁面中顯示圖像。
然后,您可以使用JSTL或scriplet對URL圖像進行迭代,以將其呈現為HTML img元素。

暫無
暫無

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

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