如何从mysql数据库中检索图像并在其中显示< td>和< img>在HTML中标记? [英] How to retrieve image from mysql db and show it inside <td> and <img> tag in HTML?

查看:205
本文介绍了如何从mysql数据库中检索图像并在其中显示< td>和< img>在HTML中标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从mysql数据库中检索图像并将其显示在HTML中的标签内,并且该img标签应放置在里面?这是我的代码:
它只显示图像。它没有显示除image以外的任何内容。



预先感谢。

 <%@ page language =javacontentType =text / html; charset = ISO-8859-1
pageEncoding =ISO-8859-1%>
<!DOCTYPE html PUBLIC - // W3C // DTD HTML 4.01 Transitional // ENhttp://www.w3.org/TR/html4/loose.dtd\">
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = ISO-8859-1>
< title>在此插入标题< / title>
< / head>
< body>
<%@ page import =java.io。*%>
<%@ page import =java.sql。*%>
<%@ page import =java.util。*%>
<%@ page import =java.text。*%>
<%@ page import =javax.servlet。*%>
<%@ page import =javax.servlet.http。*%>
<%@ page import =javax.servlet.http.HttpSession%>
<%@ page language =java%>
<%@ page session =true%>
<%
尝试{
// PrintWriter out = response.getWriter();

out.println(Retrieve Image Example!);
字符串driverName =com.mysql.jdbc.Driver;
String url =jdbc:mysql:// localhost:3306 /;
String dbName =db;
String userName =root;
String password =root;
连接con = null;
Class.forName(driverName);
con = DriverManager.getConnection(url + dbName,userName,password);
Statement st = con.createStatement();

%>
< table border ='1'>
< tr>
< td>名称:< / td>< td>
< / td>我的名字< / td>
< / tr>
< tr>
< td>图片:< / td>
< td width = 10px;>
<%
PreparedStatement pre1 = con.prepareStatement(select * from image where id =+ 8);
ResultSet rs1 = pre1.executeQuery(); (rs1.next())
{byte [] bytearray1 = new byte [4096];
int size1 = 0;
InputStream sImage1;
sImage1 = rs1.getBinaryStream(2);
response.reset();
response.setContentType(image / jpeg);
response.addHeader(Content-Disposition,filename = logo.jpg); ((size1 = sImage1.read(bytearray1))!= -1)
{
response.getOutputStream()。write(bytearray1,0,size1);
}
response.flushBuffer();
sImage1.close();
rs1.close();
}


out.println(Retrieved Successfully!);
pre.close();
con.close();
}
catch(Exception e){
out.println(e.getMessage());
}
%>
< / td>< / tr>
< / table>
< / body>
< / html>


解决方案

图像作为单独请求加载到加载的请求html。因此,您需要:


  • 建立您在指定 src img 元素。
  • 将一个servlet(或类似的)映射到该URL。该servlet根据返回该映像的URL的参数加载映像。


How to retrieve image from mysql db and show it inside tag in HTML and that img tag should be placed inside ? Here s my code: It displays only the image . Its not showing any content other than image.

Thanks in advance.

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"      "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="javax.servlet.http.HttpSession"%>
<%@ page language="java"%>
<%@ page session="true"%>
<%
  try{
   //PrintWriter out=response.getWriter();

  out.println("Retrieve Image Example!");
  String driverName = "com.mysql.jdbc.Driver";
  String url = "jdbc:mysql://localhost:3306/";
  String dbName = "db";
  String userName = "root";
  String password = "root";
  Connection con = null;
  Class.forName(driverName);
  con = DriverManager.getConnection(url+dbName,userName,password);
  Statement st = con.createStatement();

 %>
  <table border='1'>
 <tr>
 <td>Name:</td><td>
 </td>My Name</td>
 </tr>
 <tr>
 <td>Image:</td>
 <td width=10px;>
 <%
 PreparedStatement pre1 = con.prepareStatement("select * from image where id="+8);
 ResultSet rs1=pre1.executeQuery();
 while(rs1.next())   
 {byte[] bytearray1 = new byte[4096];  
              int size1=0;  
             InputStream sImage1;  
               sImage1 = rs1.getBinaryStream(2);  
               response.reset();  
              response.setContentType("image/jpeg");  
              response.addHeader("Content-Disposition","filename=logo.jpg");  
              while((size1=sImage1.read(bytearray1))!= -1 )  
                {  
                  response.getOutputStream().write(bytearray1,0,size1);  
                }  
              response.flushBuffer(); 
              sImage1.close();  
              rs1.close();  
           }


    out.println("Retrieved Successfully!");
  pre.close();
  con.close();  
  }
  catch (Exception e){
  out.println(e.getMessage());
  }
%>
</td></tr>
 </table>
</body>
  </html>

解决方案

Images are loaded as separate requests to the request that loaded the html. As such you need to:

  • establish a URL encoding scheme that you use when specifying the src of the img element.
  • map a servlet (or similar) to that URL. The servlet loads the image based on the parameters of the URL returning that image in its response.

这篇关于如何从mysql数据库中检索图像并在其中显示&lt; td&gt;和&lt; img&gt;在HTML中标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆