oracle中blob和clob文件上传

其中数据库连接使用的是前一篇文章jdbc连接oracle代码。

public void uploadBlobFile() throws SQLException, IOException {

  DbConn dbc = new DbConn();
  Connection conn = dbc.getconn();
  conn.setAutoCommit(false);

  Statement st = conn.createStatement();

  st.executeUpdate("insert into blob_test  values(133,empty_blob())");
  ResultSet rs = st.executeQuery("select file_contet from  blob_test  where  id=133 for update");
  if (rs.next()) {

   // 得到java.sql.Blob对象,然后Cast为oracle.sql.BLOB

   oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);

   // 到数据库的输出流

   OutputStream outStream = blob.getBinaryOutputStream();

   // 这里用一个文件模拟输入流
   InputStream in = new FileInputStream(new File("D://工作文档.doc"));

   byte[] b = new byte[blob.getBufferSize()];

   int len = 0;

   while ((len = in.read(b)) != -1) {

    outStream.write(b, 0, len);

   }
   in.close();
  }

  conn.commit();
  rs.close();
  st.close();
  conn.close();
 }

 public void downBlobFile() throws SQLException, IOException {
  DbConn dbc = new DbConn();
  Connection conn = dbc.getconn();
  conn.setAutoCommit(false);

  Statement st = conn.createStatement();

  conn.commit();
  ResultSet rs = st.executeQuery(

  "select file_contet from  blob_test  where  id=123 ");

  if (rs.next()) {

   java.sql.Blob blob = rs.getBlob(1);

   InputStream ins = blob.getBinaryStream();

   // 用文件模拟输出流

   File file = new File("d://output.doc");

   OutputStream fout = new FileOutputStream(file);

   // 下面将BLOB数据写入文件

   byte[] b = new byte[1024];

   int len = 0;

   while ((len = ins.read(b)) != -1) {

    fout.write(b, 0, len);

   }

   // 依次关闭

   fout.close();

   ins.close();

   
  }
  conn.commit();

  conn.close();
 }
 
 public void uploadClobFile() throws SQLException, IOException {

  DbConn dbc = new DbConn();
  Connection conn = dbc.getconn();
  conn.setAutoCommit(false);

  Statement st = conn.createStatement();

  st.executeUpdate("insert into clob_test  values(133,empty_clob())");
  ResultSet rs = st
    .executeQuery("select file_contet from  clob_test  where  id=133 for update");
  if (rs.next()) {

   // 得到java.sql.Clob对象,然后Cast为oracle.sql.CLOB

   oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob(1);

   // 到数据库的输出流

   Writer outStream = clob.getCharacterOutputStream();

   // 这里用一个文件模拟输入流
   Reader in = new FileReader(new File("D://工作文档.doc"));

   char[] b = new char[clob.getBufferSize()];

   int len = 0;

   while ((len = in.read(b)) != -1) {

    outStream.write(b, 0, len);

   }
   in.close();
  }

  conn.commit();
  rs.close();
  st.close();
  conn.close();
 }
 
 public void downClobFile() throws SQLException, IOException {
  DbConn dbc = new DbConn();
  Connection conn = dbc.getconn();
  conn.setAutoCommit(false);

  Statement st = conn.createStatement();

  conn.commit();
  ResultSet rs = st.executeQuery(

  "select file_contet from  clob_test  where  id=133 ");

  if (rs.next()) {

   java.sql.Clob clob = rs.getClob(1);

   InputStream ins = clob.getAsciiStream();

   // 用文件模拟输出流

   File file = new File("d://output2.doc");

   OutputStream fout = new FileOutputStream(file);

   // 下面将BLOB数据写入文件

   byte[] b = new byte[1024];

   int len = 0;

   while ((len = ins.read(b)) != -1) {

    fout.write(b, 0, len);

   }

   // 依次关闭

   fout.close();

   ins.close();

   
  }
  conn.commit();

  conn.close();
 } 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值