您的位置:首页 > 数据库

在jsp中直接显示从数据库中存储的图像

2006-01-11 12:32 471 查看
在jbuilder2005中测试通过1、创建jsp文件 <%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
int table_num=4; //指定表格每行显示记录的条数
out.println("<table width='75%' border='1'align='center'>");
out.println("<tr>");
for (int i=1;i<5;i++)
{
String pic01=String.valueOf(i);
pic01=""+pic01+"";
String employeename="name"+pic01;
//加入表格控制
out.println("<td>");
out.println("<div align=/"center/">");
out.println("<p>");
out.println("<a href=/"getphoto?photoid="+pic01+"/">");
out.println("<img border=/"0/"src=/"getphoto?photoid="+pic01+"&ts=AAAAA/" width=/"90/" height=/"120/" align=/"absmiddle/">");
out.println("</a>");
out.println("</p>");
out.println("<p>");
out.println("<a href=/"inf_ employee _details.jsp?inf_employee_id=pic01/">");
out.println(employeename);
out.println("</a>");
out.println("</p>");
out.println("</div>");
out.println("</td>" );
}
out.println(" </table>");
%>
</body>
</html>
</html>
2、创建sevlet getphoto.java, package lp;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;public class getphoto extends HttpServlet {
private static final String CONTENT_TYPE = "image/jpeg";
//Initialize global variables
private String driver_class = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String connect_string ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test_db";
String user="sa";
String psw="123"; Connection conn = null;
/******************************************** *
定义应用变量
******************************************/
private String SQLString = ""; //定义查询语句 //Initialize global variables
public void init() throws ServletException {
try { Class.forName(driver_class);}
catch (java.lang.ClassNotFoundException e1) {System.out.println(e1.getMessage() );}
try{
conn = DriverManager.getConnection(connect_string,user,psw );
}
catch(SQLException e3){System.out.println(e3.getMessage() );}
} //Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE); //在数据库中照片的ID
String PHOTOID = null;
try {
PHOTOID = request.getParameter("photoid");
}
catch(Exception e) {
e.printStackTrace();
} byte [] buf=null;
//扩展名可以从数据库得到,这里直接指定为JPEG
String photoname="jpeg";
try{
//根据ID查找照片
String searchSql="SELECT pic_img FROM testimg WHERE pic_id="+PHOTOID;
Statement stmt = conn.createStatement();
ResultSet RS_photo = stmt.executeQuery(searchSql);
//将图片数据读入缓冲区
if (RS_photo.next()){ buf = RS_photo.getBytes(1);
}else
{
buf = new byte[0];
}
}catch (Exception e){
//throw e;
}
//response.setContentType(CONTENT_TYPE);
//告诉浏览器输出的是图片
response.setContentType("image/"+photoname);
//图片输出的输出流
OutputStream out = response.getOutputStream();
//将缓冲区的输入输出到页面
out.write(buf);
//输入完毕,清楚缓冲
out.flush(); } //Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response); } //Clean up resources
public void destroy() { if(conn!=null)//关闭数据库连接 try {
conn.close();
}
catch (SQLException ex) {
}

}
}web.xml自己配置吧!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: