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