为什么我的图像在jsp中显示默认图标?下面有一张图片

时间:2013-07-19 09:13:14

标签: jsp servlets web-applications blob

* 这是JSP *

<% try {
                String connectionURL = "jdbc:mysql://localhost:3306/mydb";
                Connection connection = null;
                Statement statement = null;
                ResultSet rs = null;
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                connection = DriverManager.getConnection(connectionURL, "root", "alienware");
                statement = connection.createStatement();
                String QueryString = "SELECT Warehouse_Stock.name ,Warehouse_Stock.photo from Warehouse_Stock";
                rs = statement.executeQuery(QueryString);




        %> 
        <table class = "hovered" id="info" cellpadding="15" border="2">
            <thead>
            <tr>
                 <td>Photo</td>
                <td>Product Name</td>
                <!--<td>Contact Number</td>
                <td>Remarks</td>
                <td>Email Address</td>-->

            </tr>
                </thead>
            <%
                while (rs.next()) {
            %>
            <TR>
                 <td><img src="getImageDetails.jsp?your_id=12"  /></td>
                <td><%=rs.getString(1)%></td> 

                 <%--<td><%=rs.getBlob(1)%></td>--%>
                <%-- <td><%=rs.getInt(2)%></td>
                 <td><%=rs.getString(3)%></td>
                 <td><%=rs.getString(4)%></td>--%>


            </TR>

这是servlet

    response.setContentType("image/jpeg");
    PrintWriter out = response.getWriter();

    int img_id = Integer.parseInt(request.getParameter("product_code"));
    DBConnectionImp db = new DBConnectionImp();
    Connection con = db.getConnection();
            ResultSet rs = null;
    PreparedStatement pstmt = null;
    OutputStream oImage;
    try {
        pstmt = con.prepareStatement("SELECT Warehouse_Stock.photo from Warehouse_Stock");
        pstmt.setInt(1, img_id);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            //byte barray[] = rs.getBytes(1);
            //byte barray[] = rs.getBytes(1);
            //response.setContentType("image/jpeg");
            ////oImage = response.getOutputStream();
            //oImage.write(barray);
           // oImage.flush();
            //oImage.close();

            Blob blob = rs.getBlob(1);

            //response.setContentType("image/jpeg");
            oImage = response.getOutputStream();
            oImage.write(blob.getBytes(1, (int) blob.length()));
            oImage.flush();
            oImage.close();



        }
    } catch (Exception ex) {
        //ex.printStackTrace();
    } finally {
        try {
            if (con != null) {
                con.close();
            }
        } catch (Exception ex) {
            // ex.printStackTrace();
        }
    }

你看到有图像显示,但这些图像都是一样的。这些图像不是真正的图像,而是代表破损图像的图标,就像加载不支持闪存的浏览器一样。您在浏览器不支持闪存时看到的是我正在谈论的那个。但它不是“f”,而是代表破碎图像的图标。

1 个答案:

答案 0 :(得分:1)

所有图片都是从网址

加载的
getImageDetails.jsp?your_id=12

因此,此URL不指向servlet,而是指向JSP。即使它指向您的servlet,servlet也希望在参数product_code中找到要加载的图像的ID,但是您传递参数your_id