📄 如何用jsp输出存在于oracle数据库blob字段中的jpg图片.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0121)http://www.dbonline.cn/source/oracle/20040217/OTHER_how%20to%20use%20JSP%20export%20the%20based%20BLOB%20JPG%20image.html -->
<HTML><HEAD><TITLE>如何用jsp输出存在于oracle数据库Blob字段中的jpg图片</TITLE><!-- InstanceBegin template="/Templates/model.dwt" codeOutsideHTMLIsLocked="false" -->
<META http-equiv=Content-Type content="text/html; charset=gb2312"><!-- InstanceBeginEditable name="doctitle" --><!-- InstanceEndEditable -->
<SCRIPT language=JavaScript><!--function SymError(){ return true;}window.onerror = SymError;//--></SCRIPT>
<SCRIPT language=JavaScript type=text/JavaScript><!--function MM_reloadPage(init) { //reloads the window if Nav4 resized if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) { document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }} else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();}MM_reloadPage(true);//--></SCRIPT>
<!-- InstanceBeginEditable name="head" -->
<STYLE type=text/css>.style35 {
FONT-SIZE: 12px
}
.style37 {
FONT-WEIGHT: bold; FONT-SIZE: 12px; COLOR: #ff0000
}
</STYLE>
<!-- InstanceEndEditable -->
<STYLE type=text/css>.style33 {
}
A {
COLOR: #ffffff; TEXT-DECORATION: none
}
A:hover {
COLOR: #ff0000; TEXT-DECORATION: underline
}
BODY {
COLOR: #0000ff
}
TD {
COLOR: #0000ff
}
TH {
COLOR: #0000ff
}
.style34 {
COLOR: #ff0000
}
</STYLE>
<META content="MSHTML 6.00.2600.0" name=GENERATOR></HEAD>
<BODY>
<TABLE width="102%">
<TBODY>
<TR>
<TD><IMG height=60 src="如何用jsp输出存在于oracle数据库Blob字段中的jpg图片.files/1.png"
width=454>
<OBJECT
codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0
height=60 width=525 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000><PARAM NAME="movie" VALUE="../../../aa.swf"><PARAM NAME="quality" VALUE="high">
<embed src="../../../aa.swf" quality="high"
pluginspage="http://www.macromedia.com/go/getflashplayer"
type="application/x-shockwave-flash" width="525"
height="60"></embed></OBJECT></TD>
<TD> </TD></TR></TBODY></TABLE>
<TABLE width="100%">
<TBODY>
<TR vAlign=center align=middle bgColor=#330066>
<TD noWrap><FONT size=2><SPAN class=style34><A
href="http://www.dbonline.cn/index.htm">首页</A></SPAN></FONT></TD>
<TD noWrap><FONT size=2><SPAN class=style33><A
href="http://www.dbonline.cn/plsql.htm">PL/SQL</A></SPAN></FONT></TD>
<TD noWrap><FONT size=2><SPAN class=style33><A
href="http://www.dbonline.cn/structure.htm">数据库结构</A></SPAN></FONT></TD>
<TD noWrap><FONT size=2><SPAN class=style33><A
href="http://www.dbonline.cn/backup%20restore.htm">备份与恢复</A></SPAN></FONT></TD>
<TD noWrap><FONT size=2><SPAN class=style33><A
href="http://www.dbonline.cn/performance%20tunning.htm">性能优化</A></SPAN></FONT></TD>
<TD noWrap><FONT size=2><SPAN class=style33><A
href="http://www.dbonline.cn/ocp.htm">OCP认证</A></SPAN></FONT></TD>
<TD noWrap><FONT size=2><SPAN class=style33><A
href="http://www.dbonline.cn/database%20theory.htm">数据库理论</A></SPAN></FONT></TD>
<TD noWrap><FONT size=2><SPAN class=style33><A
href="http://www.dbonline.cn/other%20database.htm">其它数据库</A></SPAN></FONT></TD>
<TD noWrap><FONT size=2><SPAN class=style33><A
href="http://www.dbonline.cn/UNIX%20base.htm">UNIX基础</A></SPAN></FONT></TD>
<TD noWrap><FONT size=2><SPAN class=style33><A
href="http://www.dbonline.cn/soft.htm">常用软件</A></SPAN></FONT></TD>
<TD noWrap><FONT size=2><SPAN class=style33><A
href="http://www.dbonline.cn/ebook.htm">电子书籍</A></SPAN></FONT></TD></TR></TBODY></TABLE>
<TABLE width="100%">
<TBODY>
<TR>
<TD noWrap width="24%"><FONT size=2><B>
<SCRIPT language=JavaScript> <!--// var today = new Date(); var strDate = "<font color=red>" + (today.getYear() + "</font>" + "年" + "<font color=red>" + (today.getMonth() + 1) + "</font>" + "月" + "<font color=red>" + today.getDate() + "</font>" + "日" + " 星期"); if (today.getDay()==0) {strDate = strDate + "日";} else if (today.getDay()==1) {strDate = strDate + "一";} else if (today.getDay()==2) {strDate = strDate + "二";} else if (today.getDay()==3) {strDate = strDate + "三";} else if (today.getDay()==4) {strDate = strDate + "四";} else if (today.getDay()==5) {strDate = strDate + "五";} else if (today.getDay()==6) {strDate = strDate + "六";} strDate="<font color=000000>"+strDate+"</font>" document.write(strDate); //--> </SCRIPT>
</B></FONT></TD>
<TD noWrap align=right width="63%"> </TD>
<TD noWrap width="13%"> </TD></TR></TBODY></TABLE>
<HR>
<TABLE class=style34 width="88%" align=center>
<TBODY>
<TR>
<TD vAlign=bottom bgColor=#ffffff height=41><!-- InstanceBeginEditable name="EditRegion2" -->
<TABLE cellSpacing=0 cellPadding=0>
<TBODY>
<TR>
<TD>
<DIV align=center><SPAN
class=style37>如何用jsp输出存在于oracle数据库Blob字段中的jpg图片 </SPAN></DIV></TD>
<TR>
<TD>
<P class=style35 align=center><STRONG>作者: </STRONG>Jagie</P></TD></TR>
<TR>
<TD></TD></TR>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=0>
<TBODY>
<TR>
<TD><SPAN class=style35>关键字:jsp,Blob
<BR><BR><BR>在web-oa系统中,档案管理好象不可或缺。其中员工照片常常做成这样:用户通过浏览器上传员工相片,服务端程序接收图片文件,保存到数据库中的某个表的Blob字段里(关于文件上传和blob字段的更新,请参考http://www.javaresearch.org/article/showarticle.jsp?column=106&thread=11003和http://forum.hibernate.org.cn/viewtopic.php?t=254)。通过(jdbc/jdo)和(jsp/servlet),服务器再把保存在Blob字段中的图片文件展现给用户。我这里给出一个关于展现的简单例子。
<BR><BR>1.在数据库中建表,并且表要包含Blob字段。 <BR>2.用plsql
developer这种客户端数据库工具,修改某条记录的Blob字段,存入一个jpg文件。
<BR>3.用jdbc或者hibernate获取某条记录的Blob字段数据。(请参考参考资料2)
<BR>4.写一个jsp文件,来输出图片。如下所示
<BR><BR>//////////////////////cwry_pic.jsp/////////////////////////
<BR><%@page import="java.io.*"%> <BR><%@page
import="com.jagie.business.profile.*" %> <BR><%@page
import="java.sql.Blob"%> <BR><% <BR>response.reset();
<BR>//这个设置很重要,否则客户端浏览器不能识别输出内容,导致弹出下载的对话框。
<BR>response.setContentType("image/jpeg");
<BR>ServletOutputStream sos = response.getOutputStream();
<BR>//这里是用hibernate来根据id装载对象,你可以用别的方式如jdbc来组装对象,附文中有
<BR>//BaseInfo这个javabean的示意性代码. <BR>BaseInfo bif=
ProFileOperator.loadProFile(Long.valueOf("1")); <BR>//输出图片
<BR>if(bif!=null&&bif.getPic()!=null){ <BR>Blob
blob=(Blob)bif.getPic(); <BR>InputStream pi =
blob.getBinaryStream(); <BR>int blobsize = (int)blob.length();
<BR>byte[] blobbytes = new byte[blobsize]; <BR>int bytesRead =
0; <BR>while ((bytesRead = pi.read(blobbytes)) != -1) {
<BR>sos.write(blobbytes, 0, bytesRead); <BR>} <BR>pi.close();
<BR>sos.flush(); <BR>} <BR>%>
<BR><BR>发布这个jsp到你的web服务器,再用浏览器访问这个jsp页面,你就可以看到保存在Blob字段中的图片了。
<BR><BR><BR>附文:
<BR><BR>///////////////////BaseInfo.java示意性代码///////////////////////
<BR>package com.jagie.business.profile; <BR>public class
BaseInfo{ <BR>//pk <BR>private Long id; <BR>//相片 <BR>private
java.sql.Blob pic; <BR>public Long getId() { <BR>return id;
<BR>} <BR>public void setId(Long id) { <BR>this.id = id; <BR>}
<BR><BR>public java.sql.Blob getPic() { <BR>return pic; <BR>}
<BR>public void setPic(java.sql.Blob pic) { <BR>this.pic =
pic; <BR>} <BR>} <BR><BR>最后,希望这篇文章能对你有所启发,如有错误,敬请批评指正!
<BR><BR><BR>参考资料:
<BR>1:http://www-900.ibm.com/developerWorks/cn/java/l-imgtxt/index.shtml
<BR>2:http://forum.hibernate.org.cn/viewtopic.php?t=254
<BR></SPAN></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><!-- InstanceEndEditable --></TD></TR></TBODY></TABLE>
<HR>
<TABLE height=24 width=439 align=center><!--DWLayoutTable-->
<TBODY>
<TR>
<TD noWrap width=66 height=20><FONT color=#0000ff size=2><A
href="http://www.dbonline.cn/about%20sites.htm" target=_blank><FONT
color=#0000ff>关于本站</FONT></A> |</FONT></TD>
<TD noWrap width=66><FONT color=#0000ff size=2><A
href="mailto:webmaster@dbonline.cn" target=_blank><FONT
color=#0000ff>联络站长</FONT></A><FONT color=#333333> </FONT>|</FONT></TD>
<TD noWrap width=79><FONT color=#0000ff size=2><A
href="http://www.dbonline.cn/source/gbook/index.php" target=_blank><FONT
color=#0000ff>意见、建议</FONT></A><FONT color=#330099> </FONT>|</FONT></TD>
<TD noWrap width=66><FONT color=#0000ff size=2><A
href="http://www.dbonline.cn/operation%20cooperate.htm"
target=_blank><FONT color=#0000ff>业务合作</FONT></A> |</FONT></TD>
<TD noWrap width=66><FONT color=#0000ff size=2><A
href="http://www.dbonline.cn/site%20navigation.htm" target=_blank><FONT
color=#0000ff>本站地图</FONT></A><FONT color=#330099> </FONT>|</FONT></TD>
<TD noWrap width=68><FONT color=#0000ff size=2><A
href="http://www.dbonline.cn/adcolumn.htm" target=_blank><FONT
color=#0000ff>广告服务</FONT></A> |</FONT></TD></TR></TBODY></TABLE>
<TABLE width="53%" align=center>
<TBODY>
<TR>
<TD noWrap>
<DIV align=center><FONT color=#ff0000 size=2><STRONG>Copyright (C)
2003-2004 Oracle数据库在线 版权所有</STRONG> </FONT><BR></DIV></TD></TR>
<TR>
<TD noWrap>
<DIV align=center><FONT color=#0000ff size=2>E-Mail:<A
href="mailto:zzy93421@sohu.com" target=_blank><FONT
color=#0000ff>webmaster@dbonline.cn</FONT></A></FONT></DIV></TD></TR>
<TR>
<TD noWrap height=15>
<DIV align=center><FONT color=#330099 size=2><A
href="http://www.orahouse.net/" target=_blank><FONT
color=#0000ff>http://www.dbonline.cn</FONT></A></FONT></DIV></TD></TR></TBODY></TABLE><!-- InstanceEnd --></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -