oracle 数据库显示图片(Servlet)

本文介绍了一种从Oracle数据库中根据图片ID查询图片,并将其作为JPEG图像响应返回的方法。涉及的技术包括使用PreparedStatement执行SQL查询、处理BLOB类型数据以及设置HTTP响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  DataBase db = new DataBase();
  Connection con=null;
  String mobileid=request.getParameter("mobileid");
  try {
   con = db.getConnection();
  } catch (SQLException e) {
   // TODO 锟皆讹拷锟斤拷锟?catch 锟斤拷
   e.printStackTrace();
  }
    byte a[]=null;//字节码
    java.io.FileInputStream fin=null;   
    InputStream in =null;
    oracle.jdbc.OracleResultSet ors=null;//这个非常重要
    oracle.jdbc.driver.OraclePreparedStatement opst=null;
    
    try {
   con.setAutoCommit(false);
  } catch (SQLException e) {
   
   e.printStackTrace();
  }                        
  
         try {
  
  
          String mysql="select picture from bs_mobile where mobileid=?";

          //根据图片的ID查询图片
          opst=(oracle.jdbc.driver.OraclePreparedStatement)con.prepareStatement(mysql);
          opst.setString(1,mobileid);
          ors=(oracle.jdbc.OracleResultSet)opst.executeQuery();
          if (ors.next())
          {
              oracle.sql.BLOB blob2=ors.getBLOB(1);
              in = ors.getBinaryStream(1);
         
           //System.out.println("blob2 length:"+blob2.length());
            int lth= (int) blob2.length();
            response.reset();
            response.setContentType("image/jpg");           
            con.commit();
            OutputStream toClient = response.getOutputStream();
            byte[] P_Buf = new byte[lth];
            int i;
            while ((i = in.read(P_Buf)) != -1) {
             toClient.write(P_Buf, 0, i);
             }
            in.close();
            toClient.flush();          
            toClient.close();
         
          } 
         } catch (SQLException e) {
    
    e.printStackTrace();
   }catch(NullPointerException npe){
    
     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值