Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Servlet/JSP/JSF/JavaFX Script  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
reply to postflat modethreaded modego to previous topicgo to next topicgo to back
话题被移动
该话题已被移动 - why , 2005-08-17 12:52
如果您尚不清楚该话题被移动的原因,请参考论坛规则以及本版公告或者联系本版版主。
作者 用JSP从数据库中读取图片并显示在网页上
vincentlaw





发贴: 7
于 2005-08-17 11:23 user profilesend a private message to usersend email to vincentlawreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
环境mysql+tomcat:

<1>先在mysql下建立如下的table. 并insert图像.

mysql.sql文件如下:


CREATE TABLE photo (
photo_no intDevil unsigned NOT NULL auto_increment,
image blob,
PRIMARY KEY (`photo_no`)
)

<2>把show.jsp放在tomcat的任意目录下. show.jsp作用:从数据库中读出blob,并产生image/jpg.

show.jsp文件如下:

<%@ page contentType="text/html; charset=gbk" %>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*, javax.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.math.*"%>

<%
String photo_no = request.getParameter("photo_no");

//mysql连接
Class.forName("com.mysql.jdbc.Driver").newInstance();
String URL="jdbc:mysql://localhost:3306/job?user=root&password=111111";
Connection con = DriverManager.getConnection(URL);

//oracle连接
//String URL="jdbc:oracle:thin@localhost:1521:orcl2";
//user="system";
//password="manager";
//Connection con = DriverManager.getConnection(URL,user,password);

try{
// 准备语句执行对象
Statement stmt = con.createStatement();

String sql = " SELECT * FROM PHOTO WHERE photo_no = "+ photo_no;
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
Blob b = rs.getBlob("photo_image");
long size = b.length();
//out.print(size);
byte[] bs = b.getBytes(1, (int)size);
response.setContentType("image/jpeg");
OutputStream outs = response.getOutputStream();
outs.write(bs);
outs.flush();
rs.close();
}
else {
rs.close();
response.sendRedirect("./images/error.gif");
}
}
finally{
con.close();
}
%>

<3>把如下文件放在show.jsp的同一目录下.

index.html文件如下:

<HTML>
<HEAD>
<TITLE> 图像测试 </TITLE>

</HEAD>

<BODY>
<TABLE>
<TR>
<TD>图像测试</TD>
</TR>
<TR>
<TD><img src="show.jsp?photo_no=2"></TD>
</TR>
</TABLE>

</BODY>
</HTML>


YuLimin edited on 2005-08-19 15:39


话题树型展开
人气 标题 作者 字数 发贴时间
1991 用JSP从数据库中读取图片并显示在网页上 vincentlaw 1728 2005-08-17 11:23
1335 Re:用JSP从数据库中读取图片并显示在网页上 chengbd 21 2005-08-19 02:22

reply to postflat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923