2013-08-19 101 views
0

我在JSP中创建了一个程序来获取图像并将其显示在网页上。程序正常工作图像显示但其他内容不显示。下面是代码显示数据库中的图像

<% 
     byte[] imgData = null ; 
     Class.forName("com.mysql.jdbc.Driver"); 
     Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/buysell","root","root"); 
     Statement stmt = con.createStatement();  
ResultSet resultset =stmt.executeQuery("select * from imagemain where id=1;") ; 
    while(resultset.next()) 
    { 
     Blob bl = resultset.getBlob(2); 
byte[] pict = bl.getBytes(1,(int)bl.length()); 
response.setContentType("image/jpg"); 
OutputStream o = response.getOutputStream(); 
%> 
<img src="<%o.write(pict);%>" width="10" height="10"> 
<h1>Vishal</h1> 
<% 
out.print("1"); 
o.flush(); 
o.close(); 
    } 
     %> 

程序不显示<h1>Vishal</h1>。请帮助这个

+0

调试?查询是否工作 - >在数据库客户端(MySQL管理员,SQLYOG或phpmyadmin)中运行它以查看。 while循环运行?结果集可能是NULL? –

+0

感谢Raymond为您的回应...图像显示正确,但其他HTML内容像“

Vishal

”无法正确显示。 – vishal

+0

您正在返回一张图片。要显示文本和图像,您需要两次http访问或使用B64编码创建数据URI。在这两种情况下,你需要开始与文本/ html – mplungjan

回答

1

您需要在http如何标准访问读了作品

现在尝试

response.setContentType("text/html"); 
OutputStream o = response.getOutputStream(); 
%><img src="data:image/jpg;base64, <%o.write(Base64.encode(pict));%>" width="10" height="10"> 
    <h1>Vishal</h1> 

此处了解详情:How to display an image which is in bytes to JSP page using HTML tags?

OR

<img src="otherjspreturningimage.jsp" /> 
+0

我收到一个错误“Base64找不到符号” – vishal

+0

您需要为您的项目添加一个b64编码器。试试谷歌! https://www.google.com/search?q=jsp+b64+encoder – mplungjan