oracle 将base64图像转换为blob [重复]

xytpbqjk  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(146)

这个问题已经有答案了

Can I store binary string in CLOB column(1个答案)
上个月关门了。
我正在使用一个dhl网络服务,它在base64中返回我的标签pdf作为响应,我试图使用下面的方法转换,但它给了我错误。
我把64进制的短字符串。

DECLARE
    l_resp   VARCHAR2 (35000)
        := 'JVBERi0xLjQKJfbk/N8KMSAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwovVmVyc2lvbiAvMS40Ci9QYWdlcyAyIDAgUgo+PgplbmRvYmoKMyAwIG9iago8PAovTW9kRGF0ZSAo';

    FUNCTION base64decodeclobtoblob (p_clob VARCHAR2)
        RETURN BLOB
    IS
        l_blob     BLOB;

        l_raw      RAW (32767);

        l_amt      NUMBER := 7700;

        l_offset   NUMBER := 1;

        l_temp     VARCHAR2 (10000 CHAR);
    BEGIN
        BEGIN
            DBMS_LOB.createtemporary (l_blob, FALSE, DBMS_LOB.CALL);

            LOOP
                DBMS_LOB.read (p_clob,
                               l_amt,
                               l_offset,
                               l_temp);

                l_offset := l_offset + l_amt;

                l_raw :=
                    UTL_ENCODE.base64_decode (UTL_RAW.cast_to_raw (l_temp));

                DBMS_LOB.append (l_blob, TO_BLOB (l_raw));
            END LOOP;
        EXCEPTION
            WHEN NO_DATA_FOUND
            THEN
                NULL;
        END;

        RETURN l_blob;
    END;

    bl       BLOB;
BEGIN
    SELECT base64decodeclobtoblob (l_resp) INTO bl FROM DUAL;
END;

错误:PLS-00172: string literal too long

hxzsmxv2

hxzsmxv21#

您试图从一个没有那么多字符的字符串中读取7700字节。确保只读取字符串的长度:

DECLARE
    l_resp CLOB := 'JVBERi0xLjQKJfbk/N8KMSAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwovVmVyc2lvbiAvMS40Ci9QYWdlcyAyIDAgUgo+PgplbmRvYmoKMyAwIG9iago8PAovTW9kRGF0ZSAo';
    bl     BLOB;

    FUNCTION base64decodeclobtoblob (p_clob CLOB) RETURN BLOB
    IS
      l_blob     BLOB;
      l_raw      RAW (32767);
      c_len      NUMBER := LENGTH(p_clob);
      c_max_amt  NUMBER := 10000;
      l_amt      NUMBER;
      l_offset   NUMBER := 1;
      l_temp     VARCHAR2 (10000 CHAR);
    BEGIN
      DBMS_LOB.createtemporary(l_blob, FALSE, DBMS_LOB.CALL);
      LOOP
        EXIT WHEN l_offset >= c_len;
        l_amt := LEAST(c_max_amt, c_len + 1 - l_offset);
        DBMS_LOB.read (p_clob, l_amt, l_offset, l_temp);
        l_offset := l_offset + l_amt;
        l_raw := UTL_ENCODE.base64_decode (UTL_RAW.cast_to_raw (l_temp));
        DBMS_LOB.append (l_blob, TO_BLOB (l_raw));
      END LOOP;
      RETURN l_blob;
    END;
BEGIN
    bl := base64decodeclobtoblob(l_resp);
END;
/

相关问题