【问题标题】:How do I use Oracle utl_file to write an image clob如何使用 Oracle utl_file 编写图像 clob
【发布时间】:2015-02-01 02:23:51
【问题描述】:

我有一个生成自动电子邮件的 Oracle Apex 应用程序。在 Apex 中,用户将 JPG 图像插入到富文本字段中。该图像被保存到 CLOB 字段中。调用存储过程时,它会读取 JPG 图像并将其存储到名为 l_image_clob 的局部变量中。该程序将嵌入的图像(注意:这是一个嵌入的图像,它不是电子邮件附件)与电子邮件正文的其余部分一起发送到用户列表。一切正常。
现在我正在尝试将存储在 l_image_clob 中的 JPG 图像的内容保存到 Windows 服务器上的 JPG 文件中。以下代码生成一个文件,该文件命名正确且大小正确,但系统无法读取。当我尝试使用 Microsoft Paint 打开它时,我收到错误“这不是一个有效的位图文件”。如何使用 utl_file 来执行此操作?

Here's the code which creates the file that is "not a valid bitmap file"
      -- Create a file based on the content of l_image_clob
      l_image_filename := 'image_' || p_event_pkey || '_' || i ||
      '.' || l_image_ext;
      l_file_handle := utl_file.fopen(l_dirname , l_image_filename, 'wb');
      -- wb is write byte. This returns file handle
      <<inner_loop>>
      for i in 1 .. ceil( length( l_image_clob ) / chnksz )
      loop
        utl_file.put_raw( l_file_handle, 
          utl_raw.cast_to_raw( substr( l_image_clob, (i-1) * chnksz + 1, chnksz )));
        utl_file.fflush(l_file_handle);
      end loop inner_loop; 
      utl_file.fclose(l_file_handle); 

感谢您查看此内容。

【问题讨论】:

    标签: jpeg clob utl-file


    【解决方案1】:

    我找到了答案。 Apex 启动的图像是 base64 编码的。因此我不得不解码它。有人帮我做了一个程序来做到这一点。我修改后的代码现在看起来像这样:

    -- Create a file based on the content of l_image_clob
    l_image_filename := 'image_' || p_event_pkey || '_' || i ||
    '.' || l_image_ext;
    clob_base64_to_file(l_image_clob, l_dirname, l_image_filename);
    

    调用过程如下:

    create or replace procedure clob_base64_to_file( 
      p_clob        in  clob, 
      p_dir         in  varchar2, 
      p_filename    in  varchar2
      )
    is
      t_buffer          varchar2(32767);
      t_pos             number := 1;
      t_len             number;
      t_fh              utl_file.file_type;
      t_size            number := nls_charset_decl_len( 32764, 
                        nls_charset_id( 'char_cs' ) );
    begin
      t_fh := utl_file.fopen( p_dir, p_filename, 'wb', 32767 );
      t_len := length( p_clob );
      loop
        exit when t_pos > t_len;
        t_buffer := replace( replace( substr( p_clob, t_pos, t_size ), 
                    chr(10) ), chr(13) );
        t_pos := t_pos + t_size;
        while t_pos <= t_len and mod( length( t_buffer ), 4 ) > 0
        loop
          t_buffer := t_buffer || replace( replace( substr( p_clob, t_pos, 1 ), 
                    chr(10) ), chr(13) );
          t_pos := t_pos + 1;
        end loop;
        utl_file.put_raw( t_fh, 
          utl_encode.base64_decode( utl_raw.cast_to_raw( t_buffer ) ) );
      end loop;
      utl_file.fclose( t_fh );
    end;
    

    当我调用 clob_base64_to_file 过程时,它会解码图像并根据我在调用中提供的目录和文件名创建一个文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      • 2014-12-26
      • 2011-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多