在网页上显示图片出来

xinry 2002-05-06 04:40:04
数据表drawing中已经有若干条图片类型的数据记录了。
现在想在网页上显示图片出来,用了如下代码,但是不对

<%@ page language="java" import="java.sql.*" %>
<%
java.sql.Connection conn;
java.sql.Statement stmt;
java.sql.ResultSet rs1;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:dababasename");///数据源
stmt=conn.createStatement();

rs1=stmt.executeQuery("SELECT * FROM drawing WHERE id=1");
if (rs1.next())
{
byte [] blocco = rs1.getBytes("drawing1");
response.setContentType("image/jpeg");
ServletOutputStream op = response.getOutputStream();
for(int i=0;i<blocco.length;i++)
{
op.write(blocco[i]);
}
}
rs1.close();
%>
...全文
134 点赞 收藏 3
写回复
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
byfan 2002-05-22
我用“路人甲”的方法也不能正确显示oracle中BLOB字段中的图片,不知是什么原因。程序如下:

<%@ page contentType="image/jpeg;charset=gb2312 " %>
<%@page language="java" import="java.sql.*, java.util.*, oracle.jdbc.driver.*, oracle.sql.*,java.io.*, javax.servlet.*"%>
<% int length = 0;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@172.16.99.201:1521:scdy", "scdy", "scdy");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM TFILES WHERE ID =12");
if (rs.next())
{
BLOB my_blob = (BLOB)rs.getObject("FILE");
length = (int)my_blob.length();
byte [] byte_array = my_blob.getBytes(1, length);
response.setContentType("image/gif");
ServletOutputStream sos = response.getOutputStream();
int i;
for(i=0;i<byte_array.length;i++)
{
sos.write(byte_array[i]);
}
rs.close();
stmt.close();
con.close();
} }
catch(Exception e){
out.println("An error occurs:"+e.toString());
}%>
回复
xinry 2002-05-06
提示的错误:
null
java.lang.IllegalStateException
at allaire.jrun.servlet.JRunResponse.getOutputStream(JRunResponse.java:294)
at jrun__images2ejspb._jspService(jrun__images2ejspb.java:91)
at allaire.jrun.jsp.HttpJSPServlet.service(HttpJSPServlet.java:40)
at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1013)
at allaire.jrun.servlet.JRunSE.runServlet(JRunSE.java:925)
at allaire.jrun.servlet.JRunNamedDispatcher.forward(JRunNamedDispatcher.java:34)
at allaire.jrun.jsp.JSPServlet.service(JSPServlet.java:175)
at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1013)
at allaire.jrun.servlet.JRunSE.runServlet(JRunSE.java:925)
at allaire.jrun.servlet.JRunRequestDispatcher.forward(JRunRequestDispatcher.java:88)
at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1131)
at allaire.jrun.servlet.JvmContext.dispatch(JvmContext.java:330)
at allaire.jrun.http.WebEndpoint.run(WebEndpoint.java:107)
at allaire.jrun.ThreadPool.run(ThreadPool.java:272)
at allaire.jrun.WorkerThread.run(WorkerThread.java:75)
回复
可乐加水 2002-05-06
out.print(Object);不可以吗
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告