在plsql中使用表单数据上传后,损坏的文件

时间:2019-01-08 06:36:51

标签: oracle oracle11g oracle-sqldeveloper oracle-apex plsqldeveloper

我想使用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上传文件。

0 个答案:

没有答案