以HTML和JSP代码显示图像 [英] Displaying image in HTML and JSP code
问题描述
如何用HTML和JSP代码显示数据库中的图像?我用JSP编写了这段代码.
How can I display image from database in HTML and JSP code? I wrote this code in JSP.
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MindDotEditor posted Data</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="noindex, nofollow" />
<link href="../sample.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="../fckeditor.gif" type="image/x-icon" />
</head>
<body>
<%
String url = "jdbc:mysql://localhost:3306/sample";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String id = (String) session.getAttribute("id");
int j = Integer.parseInt(id);
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url,"root","root");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT Image_File FROM Images WHERE Image_id = '3' ");
int i = 1;
if(rs.next()) {
Blob len1 = rs.getBlob("Image_File");
int len = (int)len1.length();
byte[] b = new byte[len];
InputStream readImg = rs.getBinaryStream(1);
int index = readImg.read(b, 0, len);
System.out.println("index" +index);
stmt.close();
response.reset();
response.setContentType("image/jpg");
response.getOutputStream().write(b,0,len);
response.getOutputStream().flush();
}
} catch(Exception ex) {
out.println(ex);
} finally {
rs.close();
stmt.close();
con.close();
}
%>
<br>
<center><input type="button" value="Print" onclick="window.print();return false;" /></center>
</body>
</html>
推荐答案
HTML中的图像应使用<img>
元素表示. <img>
元素具有src
属性,该属性应指向返回图像的URL.在特定情况下,该URL可以指向Servlet
,该Servlet
从某个数据源(例如,使用FileInputStream
的本地磁盘文件系统,或者使用ResultSet#getBinaryStream()
的DB)获取图像的InputStream
,并将其写入响应OutputStream
的 Java IO 方式
Images in HTML are to be represented using the <img>
element. The <img>
element has a src
attribute which should point to an URL which returns an image. That URL can in this particular case just point to a Servlet
which gets an InputStream
of the image from some datasource (e.g. from local disk file system using FileInputStream
, or from the DB using ResultSet#getBinaryStream()
) and writes it to the OutputStream
of the response the usual Java IO way.
因此,您基本上需要更改HTML以包括以下内容:
Thus, you basically need to change your HTML to include the following:
<img src="images?id=1">
然后创建一个Servlet
,该Servlet
映射到/images
的url-pattern
上并实现doGet()
,大致如下:
Then create a Servlet
which is mapped on an url-pattern
of /images
and implement the doGet()
roughly like follows:
Image image = imageDAO.find(Long.valueOf(request.getParameter("id")));
response.setContentType(image.getContentType());
response.setContentLength(image.getContentLength());
response.setHeader("Content-Disposition", "inline; filename=\"" + image.getFileName() + "\"");
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(image.getInputStream());
output = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[8192];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
}
应该的.
我当然假设数据库中的image
表已经具有(自描述)列content_type
,content_length
和file_name
,否则您需要将第一个替换为"image"
,保留第二个(如果有问题的数据库支持则替换为Blob#length()
),并为第三个硬编码(随机)名称.
I of course assume that the image
table in your database already has the (self-descriptive) columns content_type
, content_length
and file_name
, otherwise you'll need to substitute the first with "image"
, leave the second away (or replace with Blob#length()
if the database in question supports it) and hardcode a (random) name for the third.
可以在 查看全文