0

*这里是 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>

这是小服务程序

    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();
        }
    }

您会看到显示的图像,但这些图像都是相同的。这些图像并不是真正的图像,而是代表损坏图像的图标,例如当您加载不支持 flash 的浏览器时。当您的浏览器不支持 Flash 时,您看到的就是我正在谈论的那个。但它不是一个“f”,而是一个代表破损图像的图标。

4

1 回答 1

1

所有图片均从 URL 加载

getImageDetails.jsp?your_id=12

所以这个 URL 并不指向一个 servlet,而是一个 JSP。即使它指向您的 servlet,servlet 也希望在 parameter 中找到要加载的图像的 ID product_code,但您正在传递 parameter your_id

于 2013-07-19T09:27:40.953 回答