2013-07-19 28 views
0

* 下面的圖像的JSP *爲什麼我的圖像在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(); 
     } 
    } 

你看到有顯示圖像,但這些圖像都是相同的。這些圖像不是真正的圖像,而是像加載不支持Flash的瀏覽器時表示圖像破碎的圖標。你瀏覽器不支持Flash時看到的是我正在談論的那個。但它不是一個「f」,而是一個表示破碎圖像的圖標。

回答

1

所有圖像從URL

getImageDetails.jsp?your_id=12 

加載所以這個URL並不指向一個servlet,而是一個JSP。即使它指向您的servlet,servlet也希望在參數product_code中找到要加載的映像的ID,但您傳遞的參數是your_id