Wednesday, May 29, 2013

Export Image Or Blob File From Database To Folder/Directory.

###############    Execute The Following Java Source In Database   ##################

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
      {
        File binaryFile = new File(myFile);
        FileOutputStream outStream = new FileOutputStream(binaryFile);
        InputStream inStream = myBlob.getBinaryStream();

        int size = myBlob.getBufferSize();
        byte[] buffer = new byte[size];
        int length = -1;

        while ((length = inStream.read(buffer)) != -1)
        {
          outStream.write(buffer, 0, length);
          outStream.flush();
        }

        inStream.close();
        outStream.close();
      }

    };

####################   Create Procedure Using Java Source   ###########################


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

############# Given Permition The Following For Execute Java Source Code ###########

    DECLARE
     V_USER VARCHAR2(30) := 'RND'; -- USER NAME
    BEGIN
      DBMS_JAVA.grant_permission(V_USER, 'java.io.FilePermission', '<<ALL FILES>>', 'read ,write, execute, delete');
      DBMS_JAVA.grant_permission(V_USER, 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '');
      DBMS_JAVA.grant_permission(V_USER, 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', '');
    END;
    /

#########    Export Image Or BLOB File To Directory/Folder ################

DECLARE
CURSOR c1 IS
SELECT ID, PHATO
FROM TEST_IMAGE; 
   
BEGIN
FOR i IN c1 LOOP
  
 EXPORT_IMAGE('C:\IMG\'||i.ID||'.jpg',i.PHATO);
     
END LOOP;
END;
/

10 comments:

  1. This is brilliant, we have some legacy Java code in an Oracle database which attempts to do this but in a very contrived manner. Yours is streamlined, does exactly what it says on the tin, and is exactly the type of code I'm trying to learn how to write. Nice one Rajib.

    ReplyDelete
    Replies
    1. Thank you Ivan Faulkner.Some legacy in using java code in oracle database but I will try to help you any type of java code using in oracle database.

      Delete
  2. Thank you Rajib. I wrote the code above and used EXPORT_IMAGE to write an image to a file with JPG extension. The file was created successfully. Then I FTP the file to a Windows file server. When I view the contents, it's a colorful palette and not what I know is in the original BLOB. Why is that? I am using Oracle 11g.

    ReplyDelete
    Replies
    1. I forgot to mention that I wrote the file to a Linux directory. Then, I transferred it from there to Windows file system

      Delete
    2. PL/SQL procedure successfully completed. all code is complied but file is not created...please give solution

      Delete
  3. Please open image from you Linux operating system after extraction if there is problem it can problem of extraction other wise it may be problem of your operating system/ ftp server.

    ReplyDelete
  4. could you please explain how to execute it, and where to place the files ?! I'm not too expert with Oracle, or Java but I do understand most of it. I just need to know how to execute it, and where to place the files. thanks

    ReplyDelete
  5. please give me solutions this eror
    ORA-06502: PL/SQL: numeric or value error: Bulk Bind: Truncated Bind

    ReplyDelete
    Replies
    1. Dear CHARANPREET SINGH, Today I have post another way to export image. You can follow instruction from my this article (http://rajiboracle.blogspot.com/2014/10/export-blob-from-database-to-physical.html)

      Delete
  6. Dear CHARANPREET SINGH, Today I have post another way to export image. You can follow instruction from my this article (http://rajiboracle.blogspot.com/2014/10/export-blob-from-database-to-physical.html)

    ReplyDelete