-- If necessary, please modify
-- user: PLPDF
-- directory: C:\ll\
--

CREATE OR REPLACE JAVA SOURCE NAMED "BlobHandler" AS
import java.lang.*;
import java.sql.*;
import oracle.sql.*;
import java.io.*;

public class BlobHandler
{
public static void ExportBlob(String myFile, BLOB myBlob) throws Exception
{
// Bind the object to the database object
// Open streams for the output file and the blob
File binaryFile = new File(myFile);
FileOutputStream outStream = new FileOutputStream(binaryFile);
InputStream inStream = myBlob.getBinaryStream();

// Get the optimum buffer size and use this to create the read/write buffer
int size = myBlob.getBufferSize();
byte[] buffer = new byte[size];
int length = -1;

// Transfer the data
while ((length = inStream.read(buffer)) != -1)
{
outStream.write(buffer, 0, length);
outStream.flush();
}
// Close everything down
inStream.close();
outStream.close();
} 
};
/

ALTER java source "BlobHandler" compile;
show errors java source "BlobHandler"

CREATE OR REPLACE PROCEDURE ExportBlob (p_file IN VARCHAR2,
p_blob IN BLOB)
AS LANGUAGE JAVA 
NAME 'BlobHandler.ExportBlob(java.lang.String, oracle.sql.BLOB)';
/

EXEC Dbms_Java.Grant_Permission('PLPDF', 'SYS:java.io.FilePermission', 'C:\ll\*', 'read,write,execute,delete');

EXEC dbms_java.grant_permission('PLPDF','SYS:java.lang.RuntimePermission','writeFileDescriptor', null);
EXEC dbms_java.grant_permission('PLPDF','SYS:java.lang.RuntimePermission','readFileDescriptor', null);
EXEC dbms_java.grant_permission('PLPDF','SYS:java.util.PropertyPermission','C:\ll\*','read,write');

-- usage
DECLARE
v_blob BLOB;
cursor c1 is select blob_file from STORE_BLOB;
BEGIN
open c1;
fetch c1 into v_blob;
ExportBlob('c:\ll\test2.pdf',v_blob);
close c1;

END;
/