2014-01-31 56 views
0

我想從mysql表中獲取多個圖像並將其顯示在桌面上。從mysql中獲取多個圖像並在桌面上顯示

我的代碼有什麼問題? 這裏是我的代碼

<%@ page import="java.sql.*"%> 

<%@ page import="java.io.*"%> 

<% 
response.setContentType("image/gif"); 
OutputStream o = response.getOutputStream(); 

Blob image = null; 
//long imgLen; 
Connection con = null; 

byte[ ] imgData = null ; 

Statement stmt = null; 

ResultSet rs = null; 

try 
{ 

Class.forName("com.mysql.jdbc.Driver"); 

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/document","root","root"); 

stmt = con.createStatement(); 

rs = stmt.executeQuery("select * from document_upload"); 
%> 
<html> 
<body> 
<table border=2> 
<tr> 
<td>img 
</td> 

<% 
while(rs.next()) 
{ 
    %> 
    <td> 

    <% 
    image = rs.getBlob(1); 
    imgData = image.getBytes(1,(int)image.length()); 
    o.write(imgData);%></td></tr></table> 
    <% 
} 

o.close(); 

} catch (Exception e) 
{ 

out.println("Unable To Display image"); 

out.println("Image Display Error=" + e.getMessage()); 



} 


%> 

我有它顯示了在一個時刻只能有一個形象問題。 我試圖在while循環中添加表標籤,但仍然存在問題。 的圖像被顯示在該是一個接一個像層..

回答

0

的圖像是一個接一個類似的層被示出..

問題是與你的基本的HTML結構。再看看你的while循環,每當你循環while循環時,你正在關閉你的行和表。您需要關閉while循環中的行和表。

這應該修復它:

<table border=2> 
    <tr> 
    <td>img</td>   
    <% 
    while(rs.next()){ 
    %> 
    <td>  
    <% 
     image = rs.getBlob(1); 
     imgData = image.getBytes(1,(int)image.length()); 
     o.write(imgData); 
    %> 
    </td> 
    <% 
    }  
    %> 
</tr> 
</table> 
相關問題