2014-01-31 57 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> 
相关问题