zoukankan      html  css  js  c++  java
  • java读取Oracle的BFile文件

    /**
    *
    * @author Jasmine
    */
    public class GetBlob
    {
    public static void main(String[] args)
    {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try
    {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
    Class.forName(driver);
    System.out.println("class");
    conn = DriverManager.getConnection(url, "lyy", "lyy");
    System.out.println("connect");
    stmt = conn.createStatement();
    String sql = "select files from lyy.filetable where id=4";
    rs = stmt.executeQuery(sql);
    System.out.println("sql="+sql);
    while (rs.next())
    {
          System.out.println(rs.getMetaData().getColumnTypeName(1));

    //获取locator
    BFILE bf = (BFILE)rs.getObject(1);
    System.out.println("isFileOpen="+bf.isFileOpen());
    OutputStream ops = null;
    InputStream ips = null;
    File file = new File("e:"+File.separator+bf.getName());
    try
    {
        System.out.println(""+bf.fileExists());
        bf.openFile();
        ips = bf.getBinaryStream();
        byte[] buffer =new byte[bf.getBytes().length];
        ops = new FileOutputStream(file);

        //生成os系统文件

       for (int i; (i = ips.read(buffer)) > 0;)
       {
            System.out.println("i=" + i);
            ops.write(buffer, 0, i);
            ops.flush();
        }

    catch (Exception ex)
    {
           ex.printStackTrace(System.out);
    }
    finally
    {
          ips.close();
          bf.closeFile();
          ops.close();
    }

    }

    }
    catch (Exception ex)
    {
           ex.printStackTrace(System.out);
    }
    finally
    {
         try
        {
               rs.close();
               stmt.close();
               conn.close();
        }
        catch (SQLException ex)
        {
               Logger.getLogger(GetBlob.class.getName()).log(Level.SEVERE, null, ex);
         }
    }
    }
    }

  • 相关阅读:
    【HDOJ6701】Make Rounddog Happy(启发式合并)
    【HDOJ6731】Angle Beats(极角排序)
    【BZOJ1132】Tro(叉积)
    【CF1236D】Alice and the Doll(set)
    Storm
    Spark
    Python基础(2)
    数据库漫谈
    Python基础(1)
    C/C++链接过程相关
  • 原文地址:https://www.cnblogs.com/liuyuanyuanGOGO/p/java_oracle_bfile.html
Copyright © 2011-2022 走看看