oracle中用bolb字段存取图片,多次调用时出错?

丞相 2004-04-13 05:20:38
以下是我从oracle的bolb字段中读取图片用来显示的servlet,运行正常,但如果在一个页面中循环调用这个servlet就会出错,(因为在同一个页面中我想根据不同的id取出几十个图片),提示我
ServletOutputStream 已经关闭,就不能再往里写了,请教各位,怎么解决这个问题?

import java.sql.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DownLoad extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("======DownLoad begin=====");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@10.216.0.2:1521:ORCL","test","test");
Statement stmt=conn.createStatement();
String id=request.getParameter("id");
String sql="SELECT id, name, content FROM test where id='"+id+"'";
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
Blob blob = rs.getBlob("content");
byte[] ab = blob.getBytes(1, (int)blob.length());
response.setContentType("image/jpeg");
ServletOutputStream op = response.getOutputStream();
op.write(ab);
op.flush();
op.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
System.out.println("======DownLoad end=====");
}
}
...全文
57 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Arias 2004-04-13
  • 打赏
  • 举报
回复
好像你的程序是有点问题,在你的数据库操作后没有执行一个关闭的操作,这样的话一旦你多次调用而且你的连接的数量悠闲的话肯定会出现问题。
所以你最最好还是在这个程序中写完有关对数据库的关闭操作。这样你每次去取一副图的就 申请一次连接显示 完后释放连接这样就可以了!
你试一试可否行的通!

81,092

社区成员

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

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