如何使用pl / sql将base64的+ 32k大小的BLOB解码为原始值?

时间:2019-02-04 08:16:58

标签: oracle plsql base64 clob

-此功能旨在将base64 blob解码为原始值,即使我使用的是4字节或24位的倍数缓冲区,我仍然得到无效的结果。

FUNCTION base64_blob_decode(p_blob BLOB)
                              RETURN BLOB                  

        IS
                          v_result_blob BLOB;
                          v_blob BLOB;
                          v_temp_blob BLOB;
                          v_buff binary_integer;
                          v_raw_buff raw(32676);
                          v_blob_size   INTEGER;
                          v_start_pos   INTEGER := 1;
                          v_base64_buff INTEGER;
                        BEGIN

-准备临时吊球并计算吊球大小

                    v_blob_size := LENGTH(p_blob);
                          dbms_output.put_line('v_blob_size'||v_blob_size);
                          dbms_lob.createtemporary(v_blob,false);
                          dbms_lob.createtemporary(v_temp_blob,false);
                          v_blob              := p_blob; 

-如果Blob的大小大于零,则对其进行解码

                          IF LENGTH(p_blob )   > 0 THEN
                            WHILE (v_blob_size > 0)
                            LOOP
  -- this part is to adjust the buffer size to the size of the last part
                -- the buffer size is multiple of 4 
                IF v_blob_size < 31992 AND v_blob_size > 0 THEN
                                v_buff      := v_blob_size;
                              ELSE
                                v_buff := 31992; 
                              END IF;
                read the buffer size in raw
                              v_raw_buff := DBMS_LOB.SUBSTR (v_blob , v_buff,v_start_pos);
                              v_raw_buff := UTL_ENCODE.BASE64_DECODE(v_raw_buff);
                              dbms_lob.writeappend(v_temp_blob, v_base64_buff, v_raw_buff );
                              v_start_pos :=v_start_pos  +v_buff;
                              v_blob_size := v_blob_size - v_buff;
                            END LOOP;

-返回结果blob

    v_result_blob := v_temp_blob;
    -- close the temporary blobs
                            dbms_lob.freetemporary(v_temp_blob);
                            dbms_lob.freetemporary(v_blob);
                          END IF;
                          RETURN v_result_blob;
                        EXCEPTION
                        WHEN OTHERS THEN
                          DBMS_OUTPUT.PUT_LINE('base64_blob_decode- SQLERRM:'||SQLERRM);
                          RETURN NULL;
                        END base64_blob_decode;

1 个答案:

答案 0 :(得分:0)

尝试更改此内容:

dbms_lob.writeappend(v_temp_blob, v_base64_buff, v_raw_buff );

dbms_lob.writeappend(lob_loc => v_temp_blob,
                     amount  => v_blob_size/4,
                     buffer  => v_raw_buff);