数据库里的图像显示问题

ymblue_boy 2001-10-09 03:51:33
jsp中,怎样将数据库里的图像显示在页面上?图像字段为Picture 属性:image
...全文
92 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ymblue_boy 2001-10-25
  • 打赏
  • 举报
回复
谢谢你.
czb 2001-10-17
  • 打赏
  • 举报
回复
看下我写的吧,我的字段名为photo.你的分数也真少啊。
//photo.jsp
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>相片</title>
</head>

<body>
<%
String id=request.getParameter("id");
%>
<img src="/homepage/servlet/photo?id=<%=id%>">
</body>

</html>

//photo.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;

public class photo extends HttpServlet
{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
public void init(ServletConfig conf) throws ServletException
{
super.init(conf);

try{
Class.forName("org.gjt.mm.mysql.Driver");
}catch(ClassNotFoundException e){}
String url="jdbc:mysql://localhost:3306/Sample?user=root;password=";
// String user="root",password="";
try{
conn=DriverManager.getConnection(url);
pstmt=conn.prepareStatement("select photo from person where id=?");
}catch(SQLException e){System.err.println("Something is error!");}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
int id=Integer.parseInt(request.getParameter("id"));
int length;
InputStream is=null;
byte[] buffer=new byte[4096];
response.setContentType("images/*");
DataOutputStream os=null;
try{
pstmt.setInt(1,id);
rs=pstmt.executeQuery();
os=new DataOutputStream(response.getOutputStream());
is=rs.getBinaryStream("photo");
}catch(SQLException e){}
while((length=is.read(buffer))!=-1)
{
os.write(buffer,0,length);
os.flush();
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doGet(request,response);
}
public void destroy()
{
try{
rs.close();
pstmt.close();
conn.close();
}catch(SQLException e){}
}
}
ymblue_boy 2001-10-16
  • 打赏
  • 举报
回复
我在网上找了,但资料很少。
redstarstar 2001-10-09
  • 打赏
  • 举报
回复
两种方法:
1.从数据库中用流将图像读出来,写成文件放在服务器端,再在jsp中给出图像的地址。
2.从数据库中用流将图像读出来,通过servlet直接将图像输出到网页(这种方法你查查书)
《PHP5+MySQL 网站开发实例精讲》全面、详细地介绍了基于PHP和MySQL的动态网络开发技术的原理和基础编程知识。全书共分为四篇18章,以“PHP基础知识→MySQL数据库基础知识→PHP高级开发→常用模块编程与综合案例开发”为线索具体展开,不仅包括PHP开发环境的搭建、PHP的基本语法、PHP中的常用函数、在MySQL中创建数据库和数据表,以及对MySQL数据库进行查询、删除、更新和排序等基础开发知识,还包括在PHP中创建图像、使用会话等较深入的开发内容,并在“常用模块编程和综合案例开发”篇中给出了常用模块(如通信录模块、计数器模块、文件上传模块、图片浏览模块和聊天室模块等)的开发方法,以及文件管理系统、投票系统、影碟管理系统、新闻发布系统和网上购物商城5个综合案例的开发全过程。  全书内容由浅入深,充分考虑了PHP学习者的特点,并在配套光盘中提供了书中实例的全部源代码,以方便读者举一反三,编写出适合自己的程序。 《PHP5+MySQL 网站开发实例精讲》不仅合适PHP技术的初学者,还能够帮助有一定编程经验的PHP开发人员解决开发过程中遇到的实际问题。《PHP5+MySQL 网站开发实例精讲》可作为广大PHP学习者的自学用书,或高等院校相关专业的教材和辅导用书。 显示更多 显示更少 --------------------------------------------------------------------------------

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧