java读取Oracle的BFile文件

Posted on 2013-07-25 10:25  JasmineLiu  阅读(1603)  评论(0编辑  收藏  举报

/**
*
* @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);
     }
}
}
}

Copyright © 2024 JasmineLiu
Powered by .NET 8.0 on Kubernetes