如何发布图像多部分/表单数据从pl/sql到web服务?



我想从plsql代码发送图像到我的web服务。我在oracle数据库18c xe Apache Tomcat监听器上使用APEX 21.1。在我的web服务中,我需要在multipart/form-data中发送图像的唯一路径。

下面的代码是用来发送文件的,但是不知道如何添加参数

DECLARE
p_url          VARCHAR2(255) := 'http://192.168.0.120:8000/api/v1/recognition/faces';
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(100) := '----WebKitFormBoundary7MA4YWxkTrZu0gW';
buffer raw(32767);
amount number(15) := 32767;
offset number(15) := 21;
l_attachment blob;
l_file_name  VARCHAR2(255);
l_mime_type  VARCHAR2(255);

l_response_header_name varchar2(256);
l_response_header_value varchar2(1024);
l_response_body varchar2(32767);
lang_context integer;
warning      varchar2(1000);
blb          blob;
tmp_blob blob default EMPTY_BLOB();
dest_offset integer := 1;
src_offset  integer := 1;
BEGIN
SELECT IMAGE, FILENAME, MIMETYPE
INTO l_attachment, l_file_name, l_mime_type
FROM TEST_TABLE
WHERE ID_ID = 21;
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: chunked' || l_newline ||
l_newline ||
apex_web_service.blob2clobbase64(l_attachment) ||
l_newline || '--' || lco_boundary || '--';
dbms_output.put_line(p_request_body);
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, 'content-type', 'multipart/form-data');                            
utl_http.set_header(utl_req, 'x-api-key', 'ce83481a-701f-xxxx-a3ec-exxxxxb5665e');
utl_http.set_header(utl_req, 'User-Agent', 'Mozilla/4.0');
utl_http.set_header(utl_req,
'Content-Type',
'multipart/form-data; 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);

dbms_output.put_line('Response> Status Code: ' || utl_resp.status_code);

for i in 1 .. utl_http.get_header_count(utl_resp) loop
utl_http.get_header(utl_resp, i, l_response_header_name, l_response_header_value);
dbms_output.put_line('Response> ' || l_response_header_name || ': ' || l_response_header_value);
end loop;
utl_http.read_text(utl_resp, l_response_body, 32767);

--utl_http.read_raw(utl_resp, response_body, 32767);

dbms_output.put_line('Response body>');
dbms_output.put_line(l_response_body);

utl_http.end_response(utl_resp);
EXCEPTION
WHEN UTL_HTTP.TOO_MANY_REQUESTS THEN
utl_http.END_RESPONSE(utl_resp);

END;

in postman it is working fine

POST /api/v1/recognition/faces?subject=56543 HTTP/1.1
Host: 192.168.0.120:8000
content-type: multipart/form-data
x-api-key: ce83481a-701f-xxxx-a3ec-exxxxxb5665e
Content-Length: 188
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="/C:/facerec/ASASAS.jpg"
Content-Type: image/jpeg
(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW

指导我解决我的问题,提前感谢。

其实我在很久以前就遇到过这个问题。我没有自己解决这个问题,而是使用了Rafael Uchoa编写的一个包。

这里是github的链接:https://github.com/rafaeluchoa/utl_http_multipart

关于你的问题。将图像转换为BLOB,然后使用以下过程:

utl_http_multipart.add_file(...)
-- https://github.com/rafaeluchoa/utl_http_multipart