Java Mailing List Archive

http://www.dba.5341.com/

Home » Home (12/2007) » oracle l »

Java to write a blob to disk, does any one have the java to read a blob from disk

Juan Cachito Reyes Pacheco

2004-02-26


>From Mark A. Williams from Indianapolis, IN USA
Here is the script in java to save a blob to disk (works perfectly)

Adjust for your environment and line wrapping may need to be undone...

connect / as sysdba;

grant javauserpriv to scott;

begin
dbms_java.grant_permission('SCOTT',
'java.io.FilePermission','c:\temp\blob.txt', 'write');
end;
/

connect scott/tiger;

create or replace java source named "exportBLOB" as
import java.lang.*;
import java.io.*;
import java.sql.*;
import oracle.sql.*;
public class exportBLOB
{
public static void do_export(BLOB p_blob, String p_file) throws Exception
{
  // create file output stream
  File l_file = new File(p_file);
  FileOutputStream l_out = new FileOutputStream(l_file);

  // get an input stream from the blob
  InputStream l_in = p_blob.getBinaryStream();

  // get buffer size from blob and use this to create buffer for stream
  int l_size = p_blob.getBufferSize();
  byte[] l_buffer = new byte[l_size];
  int l_length = -1;

  // write the blob data to the output stream
  while ((l_length = l_in.read(l_buffer)) != -1)
  {
   l_out.write(l_buffer, 0, l_length);
   l_out.flush();
  }

  // close the streams
  l_in.close();
  l_out.close();
  }
};
/

-- the requisite plsql wrapper
create or replace procedure exportBLOB (p_blob in blob, p_file in varchar2)
as
language java name 'exportBLOB.do_export(oracle.sql.BLOB,
java.lang.String)';
/

-- a small test of the above
declare
v_blob blob;
v_raw raw(32);
v_len number;
begin
-- create a raw to use in test
v_raw := utl_raw.cast_to_raw('Oracle!');
v_len := utl_raw.length(v_raw);

-- create a temporary lob to test
dbms_lob.createtemporary(v_blob, true);
dbms_lob.write(v_blob, v_len, 1, v_raw);

-- export it
exportBLOB (v_blob, 'c:\temp\blob.txt');
end;
/


----------------------------------------------------------------
Please see the official ORACLE-L FAQ: http://www.orafaq.com
----------------------------------------------------------------
To unsubscribe send email to: oracle-l-request@(protected)
put 'unsubscribe' in the subject line.
--
Archives are at http://www.freelists.org/archives/oracle-l/
FAQ is at http://www.freelists.org/help/fom-serve/cache/1.html
-----------------------------------------------------------------


©2008 dba.5341.com - Jax Systems, LLC, U.S.A.