上篇文章我们已经完成了图片文件上传到数据库,现在我们需要将图片文件读出并显示到网页中指定的位置。
一、新建一个JSP用于显示图片。名称了displayImg.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="voucher.basic.readImg" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
response.setContentType("image/jpeg");
byte[] data = null;
String strid = request.getParameter("zpid");
//String strid = "8";
readImg reimg = new readImg();
data = reimg.getImgdata(strid);
//获取图片的byte数据
//data = QueryPhoto.GetImgByteById(strid);
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(data, 0, data.length);
outputStream.close();
outputStream = null;
response.flushBuffer();
//清除输出流,防止释放时被捕获异常
out.clear();
out = pageContext.pushBody();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'delUser.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body></body>
</html>
二、新建一个类,用于读取指定的图片
package voucher.basic;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class readImg {
public byte[] getImgdata(String imgId){
byte[] data = null;
DBHelper dbc = new DBHelper();
Connection dbcon = dbc.openDataBase();
try {
Statement stat = dbcon.createStatement();
String mySql = "select IMG from TESTIMG where IMGID = '" + imgId + "'";
ResultSet rs = stat.executeQuery(mySql);
if(rs.next()){
java.sql.Blob blob = rs.getBlob("IMG");
InputStream inStream = blob.getBinaryStream();
int nSize = (int)blob.length();
data = new byte[nSize];
try {
inStream.read(data);
inStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
}
三、在网页中需要显示图片的位置插入图像占位符,使其SRC指向displayImg.jsp?imgId=图片ID。见下例
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="voucher.basic.DBHelper" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.InputStream" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'readimg.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<img name="img" src="displayimg.jsp?imgId=9" width="687" height="366"> //这里的imgId可以使用<%=imgId%>完成变化
</body>
</html>
需要说明的是,这里给出的最后一个应用网页,只是静态地显示了一幅图片,如果需要根据选择显示图片,可以在选择图片ID的select控件的处加οnclick="selectImg(this)",同时在网页中加入以下javascript
<script javascript>
function selectImg(obj){
document.getElementById("img").src = "displayImg.jsp?imgId=" + obj.value;
}