谁能提供一个完整的从oracle数据库读图片并显示在网页上的例子

Csharp 2004-10-19 02:27:00
我的数据库图片字段是long raw型的,
多谢!我的代码如下,但总是在读取blob字段的时候出现转型异常:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.naming.*;
import oracle.jdbc.driver.*;


public class ImageServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException {
response.setContentType(CONTENT_TYPE);
System.out.println("servlet in!");
try{
Context ctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("ds1");
Connection con = ds.getConnection();
String sql_pic="select pic from rf_map where p_number='"+request.getParameter("p_number")+"'

and pic_no=1";
Statement stmt=con.createStatement();
OutputStream toClient=response.getOutputStream();
java.sql.ResultSet rspic=stmt.executeQuery(sql_pic);
while (rspic.next())
{
System.out.println("t11");
int len =2048;
System.out.println("t22");
oracle.sql.BLOB b=((OracleResultSet)rspic).getBLOB("pic");//这句出现ClassCastException
System.out.println("t33");
InputStream inn = b.getBinaryStream();
int size=b.getBufferSize();
System.out.println("t44");

byte[] buf = new byte[size];//这个size到底是设2048还是b.getBufferSize()?
System.out.println("t55");
int readCount = 0;
System.out.println("t66");
readCount = inn.read(buf);
System.out.println("t77");
toClient.write(buf,0,readCount);
System.out.println("t88");
toClient.close();
inn.close();
}
rspic.close();}
catch(Exception e){
e.printStackTrace();
}
}
//Clean up resources
public void destroy() {
}
}
...全文
25 点赞 收藏 回复
写回复
回复
切换为时间正序
请发表友善的回复…
发表回复

还没有回复,快来抢沙发~

发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

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