Oracle数据库保存、下载图片

存储图片class:
test表结构( id name image)

public class Test{
    PreparedStatement pst = null;
    Result rs = null;
    Connection conn = ConnectionManager.getConnection();
    String sql = "insert into test(id,name,image) values(?,?,?)";
    try {
          pst = conn.prepareStatement(sql);
          pst.setInt(0,1);
          pst.setString(1,"testData");
          //事先插入空对象empty_blob()  
          pst.setBlob(2,BLOB.empty_lob()); 
          pst.executeUpdate();

          OutputStream os = null;
          // for update 锁定数据行进行更新
          String q_sql = "select image from test_img where id = ? for update"; 
          pst = conn.prepareStatement(q_sql);
          pst.setInt(1,1);
          rs = pst.executeQuery();
          if (rs.next()) {
              oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("image");
              os = blob.getBinaryOutputStream();
              InputStream is = new FileInputStream("C:\\image.jpg");
              int i = 0;
              while ((i = is.read()) != -1) {
                  os.write(i);
              }
          }
          is.close();
          os.flush();
          os.close();
          ConnectionManager.closeAll(rs,pst,conn); // 关闭资源

      } catch (sqlException e) {
            e.printstacktrace();
      } catch (FileNotFoundException e) {
            e.printstacktrace();
      } catch (IOException e) {
            e.printstacktrace();
      }
    }
}

根据id获取图片byte数据

public class Query{
 public byte[] GetImgByteById(Int id,Connection conn){   
    byte[] data = null;  
    try {  
            String sql = "select image from test where id = ?";
            PreparedStatement pst = null;  
            pst = conn.prepareStatement(sql);
            pst.setInt(0,id);
            rs = pst.executeQuery();

            StringBuffer myStringBuffer = new StringBuffer();  
            if (rs.next()) {  
                java.sql.Blob blob = rs.getBlob("image");  
                InputStream in = blob.getBinaryStream();  
                try {  
                    long nLen = blob.length();  
                    int nSize = (int) nLen;  
                    data = new byte[nSize];  
                    in.read(data);  
                    in.close();  
                } catch (IOException e) {  
                    System.out.println("获取图片数据失败,原因:" + e.getMessage());  
                }  

            }  
            System.out.println(myStringBuffer.toString());  
            conn.commit();  
            conn.close();  
        } catch (sqlException ex) {  
            System.out.println(ex.getMessage());  
        }  
        return data;  
    }
}

处理byte数据:

//获取图片的byte数据  
        id = 1;
        data = Query.GetImgByteById(id); 
        ServletoutputStream op = response.getoutputStream();         
       op.write(data,data.length); 
       op.close()

获取conn省略。

相关文章

Java Oracle 结果集是Java语言中处理数据库查询结果的一种方...
Java AES和Oracle AES是现代加密技术中最常使用的两种AES加密...
Java是一种广泛应用的编程语言,具备可靠性、安全性、跨平台...
随着移动互联网的发展,抽奖活动成为了营销活动中不可或缺的...
Java和Oracle都是在计算机领域应用非常广泛的技术,他们经常...
Java 是一门非常流行的编程语言,它可以运行于各种操作系统上...