oracle - 在 plsql 中使用表单数据上传后文件损坏
问题描述
我想从 PL/SQL 做一个 POST,使用 enctype "Multipart/form-data" 来上传存储在表中的 jpeg blob 文件。我能够发送 .txt 文件。但是当我尝试上传图像文件时,图像文件已损坏。我对plsql不太感兴趣,所以没有适当的方式。
DECLARE
p_url VARCHAR2(255) := 'http://localhost:8080/uploadFile';
utl_req utl_http.req;
utl_resp utl_http.resp;
req_length BINARY_INTEGER;
response_body VARCHAR2(32767);
p_request_body clob;
l_newline VARCHAR2(50) := chr(13) || chr(10);
lco_boundary CONSTANT VARCHAR2(30) := 'AaB03x';
buffer raw(32767);
amount number(15) := 32767;
offset number(15) := 1;
l_attachment blob;
l_file_name VARCHAR2(255);
l_mime_type VARCHAR2(255);
lang_context integer;
warning varchar2(1000);
blb blob;
tmp_blob blob default EMPTY_BLOB();
dest_offset integer:=1;
src_offset integer:=1;
BEGIN
select BLOB_CONTENT, FILENAME, MIME_TYPE into l_attachment, l_file_name,
l_mime_type from X_FILES where ID=12;
p_request_body := l_newline
|| '--'
|| lco_boundary
|| l_newline
|| 'Content-Disposition: form-data; name="file";
filename="'||l_file_name||'"'
|| l_newline
|| 'Content-Type: '||l_mime_type
|| l_newline
|| 'Content-Transfer-Encoding: binary'
|| l_newline
|| l_newline
|| CLOBFROMBLOB(l_attachment)
|| l_newline
|| '--'
|| lco_boundary
|| '--';
dbms_lob.createtemporary(blb, FALSE);
dest_offset := 1;
src_offset := 1;
lang_context := 0;
dbms_lob.converttoblob( blb, p_request_body, dbms_lob.getlength(p_request_body), dest_offset, src_offset, 0, lang_context, warning );
dbms_lob.append(blb,l_attachment);
req_length := dbms_lob.getlength(blb);
utl_req := utl_http.begin_request(url => p_url,method => 'POST',http_version => 'HTTP/1.1');
utl_http.set_header(utl_req,'User-Agent','Mozilla/4.0');
utl_http.set_header(utl_req,'Content-Type','multipart/mixed; boundary="' || lco_boundary || '"');
dbms_output.put_line(req_length);
IF
req_length <= 32767
THEN
utl_http.set_header(utl_req, 'Content-Length', req_length);
utl_http.write_raw(utl_req, blb);
ELSIF req_length > 32767 THEN
utl_http.set_header(utl_req, 'Transfer-Encoding', 'chunked');
WHILE ( offset < req_length ) LOOP
dbms_lob.read(blb, amount, offset, buffer);
utl_http.write_raw(utl_req, buffer);
offset := offset + amount;
END LOOP;
END IF;
utl_resp := utl_http.get_response(utl_req);
utl_http.read_raw(utl_resp,response_body,32767);
utl_http.end_response(utl_resp);
EXCEPTION
WHEN UTL_HTTP.TOO_MANY_REQUESTS THEN
utl_http.END_RESPONSE(utl_resp);
END;
我想使用 miltipart/form-data 来上传文件。
解决方案
推荐阅读
- python - 读取 txt 文件并插入文本小部件后显示的括号。任何简单的修复方法?
- python - 避免来自 Python 标准输入流的控制序列(如 ^[[C)
- php - 缓慢的简单雄辩的查询
- django - 我们是否需要在 django 的 ManyToManyField 中使用 on_delete 参数?
- asp.net-mvc - 添加引用时无法解决依赖项面临错误
- php - 如何销毁离开页面的具体 $_SESSION 变量?
- reactjs - 在 Apache 服务器 CENTOS 环境上部署 React App 不起作用
- asp.net-core - ASP.Net Core web api 404 未找到
- ios - 这里 Map SDK IOS (Premium) 点击标记
- python - Python初学者:如何将列表作为输入(不创建列表)