显示数据库中的图像

时间:2013-08-19 13:10:32

标签: html mysql jsp

我在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>。请协助这个

1 个答案:

答案 0 :(得分: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" />