首页 > 解决方案 > 长生到pdf

问题描述

下午好,我需要帮助来阅读 Oracle 数据库中的一个字段。我有一个包含 pdf 的长原始字段,我需要将其保存到文件中。有人对我该如何做有任何建议吗?

标签: sqloraclepdflong-integerraw

解决方案


如果您有权访问 Oracle 支持,文档 ID 1433573.1中有一个示例

样品程序

CREATE OR REPLACE PROCEDURE WritePDFToFILE (myfilename IN VARCHAR2)
IS
    v_blob          BLOB;
    blob_length     INTEGER;
    out_file        UTL_FILE.FILE_TYPE;
    -- chunk size and buffer size must match
    chunk_size      BINARY_INTEGER := 32767;
    v_buffer        RAW (32767);
    blob_position   INTEGER := 1;
BEGIN
    -- Retrieve the BLOB for reading
    SELECT filedata
      INTO v_blob
      FROM blob_pdf_tbl
     WHERE filename = myfilename;

    -- Retrieve the SIZE of the BLOB
    blob_length := DBMS_LOB.GETLENGTH (v_blob);

    -- Open a handle to the location where you are going to write the BLOB to a file
    -- NOTE: The 'wb' parameter means "write in byte mode" and is only availabe
    --       in the UTL_FILE package with Oracle 10g or later
    out_file :=
        UTL_FILE.FOPEN ('PDF_DIR_LOC',
                        myfilename,
                        'wb',
                        chunk_size);

    -- Write the BLOB to file in chunks
    WHILE blob_position <= blob_length
    LOOP
        IF blob_position + chunk_size - 1 > blob_length
        THEN
            chunk_size := blob_length - blob_position + 1;
        END IF;

        DBMS_LOB.READ (v_blob,
                       chunk_size,
                       blob_position,
                       v_buffer);
        UTL_FILE.PUT_RAW (out_file, v_buffer, TRUE);
        blob_position := blob_position + chunk_size;
    END LOOP;

    -- Close the file handle
    UTL_FILE.FCLOSE (out_file);
END;
/

推荐阅读