我在 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>
。请协助解决此问题
最佳答案
您需要了解标准 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?
或者
<img src="otherjspreturningimage.jsp" />
关于html - 显示数据库中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18314656/