分享

oracle Blob 文件读写

 dna26 2012-11-13

import java.io.*;
import java.sql.*;

import oracle.sql.*;

public class Tests {
 /**
  * 创建表后插入一条记录
  *
  * @throws Exception
  */
 public static void createTable() throws Exception {
  Connection conn = null;
  PreparedStatement pstmt = null;

  String sql = "CREATE TABLE FORUM.systemp(blobid BLOB,clobid CLOB)";
  conn = PoolFactory.newInstance();
  // 添加一张表,再添加一条记录
  pstmt = conn.prepareStatement(sql);
  pstmt.executeUpdate();
  sql = "insert into systemp (blobid,clobid) values(empty_blob(),empty_clob())";
  pstmt = conn.prepareStatement(sql);
  pstmt.executeUpdate();

  pstmt.close();
  conn.close();
 }

 /**
  * 更新值为空
  *
  * @throws Exception
  */
 private static void updateSystemp() throws Exception {
  Connection conn = null;
  PreparedStatement pstmt = null;

  conn = PoolFactory.newInstance();

  String sql = "update systemp set blobid=empty_blob(),clobid=empty_clob()";
  pstmt = conn.prepareStatement(sql);
  pstmt.executeUpdate();

  pstmt.close();
  conn.close();
 }

 /**
  *
  * @param content
  *            String
  * @return Clob
  * @throws Exception
  */
 public static Clob retClob(Connection conn, String content)
   throws Exception {
  return (Clob) retBClob(conn, false, false, content);
 }

 /**
  *
  * @param Connection
  *            conn 连接
  * @param isBlob
  *            boolean 需要实例化的类型,TRUE返回BLOB实例,FALSE返回CLOB实例
  * @param isFile
  *            boolean 是否是文件
  * @param content
  *            String 需要转化的内容,或文件名
  * @return Object 把content进行BLOB,CLOB实例化。
  * @throws Exception
  */
 public static Object retBClob(Connection conn, boolean isBlob,
   boolean isFile, String content) throws Exception {
  // updateSystemp();

  PreparedStatement pstmt = null;
  ResultSet rs = null;

  int columnIndex = 2;
  if (isBlob) {
   columnIndex = 1;
  }

  conn.setAutoCommit(false);
  try {
   pstmt = conn.prepareStatement("select * from systemp FOR UPDATE");
   rs = pstmt.executeQuery();
   if (rs.next()) {
    return fillContent(rs.getObject(columnIndex), isBlob, isFile,
      content);
   }
  } catch (SQLException ex) {
   throw new Exception(
     "please establish the systemp table!\n that one column is Blob and the other is Clob!",
     ex);
  } finally {
   if (rs != null)
    rs.close();
   if (pstmt != null)
    pstmt.close();
  }

  throw new Exception("please insert into one record to table systemp!");
 }

 /**
  *
  * @param fileName
  *            String 文件路径,包含文件名
  * @return InputStream 读入文件的IO流
  * @throws Exception
  */
 private static InputStream getFileStream(String fileName) throws Exception {
  File f = new File(fileName);
  InputStream fis = new FileInputStream(f);

  return fis;
 }

 private static Object fillContent(Object o, boolean isBlob, boolean isFile,
   String content) throws Exception {
  OutputStream out = null;
  InputStream fis = null;
  //根据isBlob创建相应的输出流
  out = distinctOutputStream(o, isBlob);
  //创建相应的文件流或字符流
  fis = distinctInputStream(isFile, content);

  // int count = -1, total = 0;
  byte[] data = new byte[(int) fis.available()];
  fis.read(data);
  out.write(data);

  /*
   * byte[] data = new byte[blob.getBufferSize()]; 另一种实现方法,节省内存 while
   * ((count = fin.read(data)) != -1) { total += count; out.write(data, 0,
   * count); }
   */
  fis.close();
  out.close();

  return o;
 }

 /**
  * 根据BLOB,CLOB创建相应的输出流
  *
  * @param o
  *            Object
  * @param isBlob
  *            boolean
  * @return OutputStream
  * @throws Exception
  */
 private static OutputStream distinctOutputStream(Object o, boolean isBlob)
   throws Exception {
  OutputStream out = null;
  if (isBlob) {
   out = ((BLOB) o).getBinaryOutputStream();
  } else {
   out = ((CLOB) o).getAsciiOutputStream();
  }
  return out;
 }

 /**
  * 是否为文件,是则返回文件流,否则返回STRING流
  *
  * @param isFile
  *            boolean
  * @param content
  *            String 文件名或STRING流
  * @return InputStream
  * @throws Exception
  */
 private static InputStream distinctInputStream(boolean isFile,
   String content) throws Exception {
  InputStream fis = null;
  if (isFile) {
   fis = getFileStream(content);
  } else {
   fis = new ByteArrayInputStream(content.getBytes());
   // fis = new StringInputStream(content);
  }
  return fis;
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

 }


public static String blobToString(Object obj) {
  StringBuffer sb = new StringBuffer();
  try {
   if (obj instanceof BLOB) {
    System.out.println("11111111");
    InputStream is = ((BLOB) obj).getBinaryStream();
    byte[] buff = new byte[2048];
    while (is.read(buff) != -1) {
     sb.append(new String(buff));
    }
    if (is != null)
     is.close();
   } else if (obj instanceof CLOB) {
    System.out.println("2222222");
    int i = 0;
    Reader reader = ((CLOB) obj).getCharacterStream();
    char[] b = new char[10000];// 每次获取10K
    while ((i = reader.read(b)) != -1) {
     sb.append(b, 0, i);
    }
    if (reader != null)
     reader.close();
   } else
    return "";

  } catch (Exception e) {
  }
  System.out.println(sb.toString() + "1111");
  return sb.toString();

 }

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约