用JSP怎样对ORACLE存取图片.

解决方案 »

  1.   

    http://search.csdn.net/expert/topic/61/6101/2002/11/5/1151465.htm
      

  2.   

    也可以用过程.
    http://expert.csdn.net/Expert/topic/2435/2435782.xml?temp=.2710382
      

  3.   

    这是在servlet里的一个方法  public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
        throws javax.servlet.ServletException, java.io.IOException
      {
         performTask(request, response);
      }  public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException
      {    response.reset();
        response.setContentType("image/jpeg");
        OutputStream op = response.getOutputStream();    Blob blob = null;
     
        blob = getImgFromDB(id,type);    InputStream is = null;
        if(blob!=null){
          try{
            is = blob.getBinaryStream();
          }
          catch(Exception ex){
            System.out.println("取得InputStream时产生错误:"+ex.getMessage());
          }
        }    int total = 0;
        int nread = 0;    byte[] buffer = new byte[4 * 1024];
        while ( (nread = is.read(buffer)) != -1) {
          op.write(buffer, 0, nread);
          total += nread;
        }
        op.flush();
        op.close();
      public Blob getImgFromDB(String id,String type){
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        Blob oBlob = null;    std nstd = new std(jndiName);
        String strSql = "SELECT " + idName + " ," + type +
            ",dbms_lob.getlength(" + type + ") FROM " + tableName + " WHERE " +
            idName + "=lower('" + id + "')";
        //System.out.println(strSql);
        try{
          conn = nstd.getConnection(jndiName);
          stmt = conn.createStatement();
          rs = stmt.executeQuery(strSql);
          if (rs.next() && rs.getInt(3) > 0) { //有图片记录,且图片不为空
            oBlob = rs.getBlob(2);
          }
        }
        catch(Exception fe){
          System.out.println("showImage Error :" + fe.getMessage());
        }
        finally{
          try {
            if (rs != null) {rs.close();}
            if (stmt != null) {stmt.close();}
            if (conn != null) {conn.close();}
          }
          catch (Exception e) {
            System.out.print(e.getMessage());
          }
        }    return oBlob;  }}