BLOG图片和CLOG BASE64码的转换

--BASE64转图片
CREATE OR REPLACE FUNCTION Decode_Base64(p_Clob_In IN CLOB) RETURN BLOB IS
  v_Blob           BLOB;
  v_Offset         INTEGER;
  v_Tem_Clob       CLOB;
  v_Buffer_Varchar VARCHAR2(32000);
  v_Buffer_Raw     RAW(32000);
  v_Buffer_Size    BINARY_INTEGER := 32000;
BEGIN
  IF p_Clob_In IS NULL THEN
    RETURN NULL;
  END IF;
  Dbms_Lob.Createtemporary(v_Blob, TRUE);
  v_Offset := 1;
  FOR i IN 1 .. Ceil(Dbms_Lob.Getlength(p_Clob_In) / v_Buffer_Size) LOOP
    Dbms_Lob.Read(p_Clob_In, v_Buffer_Size, v_Offset, v_Buffer_Varchar);
    v_Buffer_Raw := Utl_Encode.Base64_Decode(Utl_Raw.Cast_To_Raw(v_Buffer_Varchar));
    Dbms_Lob.Writeappend(v_Blob, Utl_Raw.Length(v_Buffer_Raw), v_Buffer_Raw);
    v_Offset := i * v_Buffer_Size + 1;
  END LOOP;
  RETURN v_Blob;
END Decode_Base64;

--图片转BASE64
CREATE OR REPLACE FUNCTION Encode_Base64(p_Blob IN BLOB) RETURN CLOB IS
  l_Clob CLOB; l_Step PLS_INTEGER := 12000;
BEGIN
  FOR i IN 0 .. Trunc((Dbms_Lob.Getlength(p_Blob) - 1)/l_Step) LOOP 
    l_Clob := l_Clob || Utl_Raw.Cast_To_Varchar2(Utl_Encode.Base64_Encode(Dbms_Lob.Substr(p_Blob, l_Step, i * l_Step + 1)));
END LOOP; RETURN l_Clob;
END;

posted @ 2020-05-05 15:49  游兮游兮  阅读(149)  评论(0编辑  收藏  举报