如何使用 Oracle utl_file 编写图像 clob
How do I use Oracle utl_file to write an image clob
我有一个生成自动电子邮件的 Oracle Apex 应用程序。在 Apex 中,用户将 JPG 图像插入富文本字段。该图像被保存到 CLOB 字段中。调用存储过程时,它会读取 JPG 图像并将其存储到名为 l_image_clob 的局部变量中。该程序将嵌入图像(注意:这是一个嵌入图像,它不是电子邮件附件)连同电子邮件正文的其余部分发送到用户列表。一切正常。
现在我试图将存储在 l_image_clob 中的 JPG 图像的内容保存到 windows 服务器上的 JPG 文件中。以下代码生成一个文件,命名正确且大小正确,但系统不可读。当我尝试使用 Microsoft Paint 打开它时出现错误 "this is not a valid bitmap file"。如何使用 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);
感谢您的关注。
我找到了答案。 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 过程时,它会解码图像并根据我在调用中提供的目录和文件名创建一个文件。
我有一个生成自动电子邮件的 Oracle Apex 应用程序。在 Apex 中,用户将 JPG 图像插入富文本字段。该图像被保存到 CLOB 字段中。调用存储过程时,它会读取 JPG 图像并将其存储到名为 l_image_clob 的局部变量中。该程序将嵌入图像(注意:这是一个嵌入图像,它不是电子邮件附件)连同电子邮件正文的其余部分发送到用户列表。一切正常。
现在我试图将存储在 l_image_clob 中的 JPG 图像的内容保存到 windows 服务器上的 JPG 文件中。以下代码生成一个文件,命名正确且大小正确,但系统不可读。当我尝试使用 Microsoft Paint 打开它时出现错误 "this is not a valid bitmap file"。如何使用 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);
感谢您的关注。
我找到了答案。 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 过程时,它会解码图像并根据我在调用中提供的目录和文件名创建一个文件。