---
title: "java 处理 BLOB OutputStream(插入空的Blob字段到表中)"
author: "ccfangle"
date: "2015-07-08T18:45:06Z"
tags: []
categories: ["数据库"]
slug: "java-blob-outputstream-blob"
draft: false
lastmod: "2015-07-08T18:45:06Z"
id: 2225322
url: "https://ccfangle.iteye.com/blog/2225322"
---

1.插入占位数据：

insert into TEST\_CUICF(t1,t2) values(12,empty\_blob())

2.获得占位数据：

select t1,t2 from TEST\_CUICF t for update

 

BLOB oracleBlobMockIn = oracleBlobMockIn = control.createMock(BLOB.class);

...

  oracleBlobMockIn.getBinaryStream();  
  expectLastCall().andReturn(new FileInputStream(inPdfFile)).times(1);

...

  ResultSet rs = st.executeQuery(sql);  
  BLOB oracleBlobMockOut = null;  
  if (rs.next()) {  
   rs.getString("t1");  
   oracleBlobMockOut = (BLOB) rs.getBlob("t2");  
  }  
...

  control.replay();  
  Watermark.addPdfWatermark(oracleBlobMockIn, oracleBlobMockOut, markImagePath);  
  control.verify();  
  control.reset();

 

3.应用 输出流 (增加水印处理)

 public static void addPdfWatermark(BLOB inputPdfFileBlob, BLOB outPdfFileBlob, String markImagePath) throws Exception {  
  if (null == WATERMARK\_IMAGE) {  
   logger.info("watermark\_image inital~~");  
   WATERMARK\_IMAGE = Image.getInstance(markImagePath);  
   WATERMARK\_IMAGE.setAbsolutePosition(10, 250);  
  }

  PdfReader reader = null;  
  PdfStamper stamp = null;  
  try {  
   reader = new PdfReader(inputPdfFileBlob.getBinaryStream(), "PDF".getBytes());  
   stamp = new PdfStamper(reader, ***outPdfFileBlob.getBinaryOutputStream()); //获得输出流***

   int numberOfPages = reader.getNumberOfPages();  
   for (int i = 1; i &lt;= numberOfPages; i++) {  
    PdfContentByte under = stamp.getUnderContent(i);  
    under.addImage(WATERMARK\_IMAGE);  
   }

  } finally {  
   stamp.close();  
  }  
 }

4.关闭连接

  con.commit();

  con.close();