ABAP 呼叫HTTP上傳附件

2022-11-09 18:01:30

1、需求說明

在SAP中呼叫第三方檔案伺服器的HTTP請求,將檔案儲存在檔案伺服器上,並返回儲存的檔案地址。
SAP儲存返回的檔案地址,通過瀏覽器進行存取。

2、需求實現

2.1、POSTMAN測試

通過POSTMAN測試成功,獲取返回的檔案地址

並且可以看到請求對應程式語言的範例程式碼

 

2.2、程式碼範例

通過檔案伺服器URL建立http_client,設定型別為multipart/form-data,並傳輸請求引數

"-----------------------------@斌將軍-----------------------------
TYPES
: BEGIN OF ty_itab, raw(255) TYPE x, END OF ty_itab. DATA:lt_bin TYPE STANDARD TABLE OF ty_itab, ls_bin LIKE LINE OF lt_bin. DATA: lv_len TYPE i, "傳送報文長度 lv_url TYPE string, "介面地址 lo_http_client TYPE REF TO if_http_client, "http使用者端 lo_part TYPE REF TO if_http_entity, lv_value TYPE string, lv_name TYPE string, lv_file TYPE string, lv_rec_json TYPE string, "接收的JSON lv_code TYPE i, "HTTP 返回狀態 lv_reason TYPE string. " HTTP 狀態描述 DATA:lv_filelength TYPE i, lv_xstring TYPE xstring. DATA:lv_account TYPE string, lv_pass TYPE string.
。。。。。。
"建立http使用者端 CALL METHOD cl_http_client=>create_by_url EXPORTING url = lv_url IMPORTING client = lo_http_client EXCEPTIONS argument_not_found = 1 plugin_not_active = 2 internal_error = 3 pse_not_found = 4 pse_not_distrib = 5 pse_errors = 6 oa2c_set_token_error = 7 oa2c_missing_authorization = 8 oa2c_invalid_config = 9 oa2c_invalid_parameters = 10 oa2c_invalid_scope = 11 oa2c_invalid_grant = 12 OTHERS = 13. IF sy-subrc <> 0. e_return-type = 'E'. e_return-message = '無法建立http client'. RAISE error. ENDIF. "設定http method 為POST CALL METHOD lo_http_client->request->set_header_field EXPORTING name = '~request_method' value = 'POST'. "SET protocol version CALL METHOD lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ). "content type CALL METHOD lo_http_client->request->set_content_type EXPORTING content_type = 'multipart/form-data'. "使用者名稱 CALL METHOD lo_http_client->request->set_form_field EXPORTING name = 'accessKey' value = lv_account. "資料夾名稱 CALL METHOD lo_http_client->request->set_form_field EXPORTING name = 'bucketName' value = 'sap'. "密碼 CALL METHOD lo_http_client->request->set_form_field EXPORTING name = 'secretKey' value = lv_pass. "
"-----------------------------@斌將軍-----------------------------

瀏覽器對應的編碼和語言設定,可以參考瀏覽器中測試時,真實傳送的請求引數

"-----------------------------@斌將軍-----------------------------
"
瀏覽器支援的 MIME 型別 CALL METHOD lo_http_client->request->set_header_field EXPORTING name = 'Accept' value = '*/*'. "瀏覽器支援的壓縮編碼 CALL METHOD lo_http_client->request->set_header_field EXPORTING name = 'Accept-Encoding' value = 'gzip, deflate'. "瀏覽器支援的語言 CALL METHOD lo_http_client->request->set_header_field EXPORTING name = 'Accept-Language' value = 'zh-CN,zh;q=0.9,de;q=0.8,en;q=0.7'.
"-----------------------------@斌將軍-----------------------------

新增上傳的檔案,其中檔名需要進行URL編碼,可避免檔案的中文亂碼

其中name="file" 對應POSTMAN請求引數中的file

"-----------------------------@斌將軍-----------------------------
CALL
METHOD lo_http_client->request->if_http_entity~set_formfield_encoding EXPORTING formfield_encoding = cl_http_request=>if_http_entity~co_encoding_raw. lo_part = lo_http_client->request->if_http_entity~add_multipart( ). "拼接上傳的檔名,並將檔名轉碼 lv_name = i_filename. lv_name = cl_http_utility=>escape_url( lv_name ). lv_value = 'form-data; name="file"; filename="' && lv_name && '";'. CALL METHOD lo_part->set_header_field EXPORTING name = 'content-disposition' * value = 'form-data; name="file"; filename="11.txt";'. value = lv_value. CALL METHOD lo_part->set_content_type EXPORTING content_type = 'application/x-www-form-urlencoded'.
"-----------------------------@斌將軍-----------------------------

上傳本地檔案為二進位制,並轉為xstring格式

"-----------------------------@斌將軍-----------------------------
CALL
FUNCTION 'GUI_UPLOAD' EXPORTING filename = lv_file filetype = 'BIN' IMPORTING filelength = lv_filelength TABLES data_tab = lt_bin EXCEPTIONS file_open_error = 1 file_read_error = 2 no_batch = 3 gui_refuse_filetransfer = 4 invalid_type = 5 no_authority = 6 unknown_error = 7 bad_data_format = 8 header_not_allowed = 9 separator_not_allowed = 10 header_too_long = 11 unknown_dp_error = 12 access_denied = 13 dp_out_of_memory = 14 disk_full = 15 dp_timeout = 16 OTHERS = 17. IF sy-subrc <> 0. e_return-type = 'E'. e_return-message = '讀取檔案失敗'. RAISE error. ENDIF. CALL FUNCTION 'SCMS_BINARY_TO_XSTRING' EXPORTING input_length = lv_filelength IMPORTING buffer = lv_xstring TABLES binary_tab = lt_bin EXCEPTIONS failed = 1 OTHERS = 2.
"-----------------------------@斌將軍-----------------------------

將檔案流放入client中並行送,接收返回結果

"-----------------------------@斌將軍-----------------------------
lv_len = xstrlen( lv_xstring ). CALL METHOD lo_part->set_data EXPORTING data = lv_xstring offset = 0 length = lv_len. "傳送 CALL METHOD lo_http_client->send EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 OTHERS = 4. IF sy-subrc <> 0. CALL METHOD lo_http_client->get_last_error IMPORTING message = DATA(lv_error_message). CALL METHOD lo_http_client->close. e_return-type = 'E'. e_return-message = '通訊失敗'. RAISE error. ENDIF. "接收 CALL METHOD lo_http_client->receive EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 OTHERS = 9. IF sy-subrc <> 0. CALL METHOD lo_http_client->get_last_error IMPORTING message = lv_error_message. CALL METHOD lo_http_client->close. e_return-type = 'E'. e_return-message = '接收失敗'. RAISE error. ENDIF. "提取返回字串 CLEAR:lv_code,lv_reason. CALL METHOD lo_http_client->response->get_status IMPORTING code = lv_code reason = lv_reason. "獲取返回的JSON CLEAR:lv_rec_json. lv_rec_json = lo_http_client->response->get_cdata( ). "關閉介面 CALL METHOD lo_http_client->close.
"-----------------------------@斌將軍-----------------------------

3、補充說明

3.1、檔案型別

在設定上傳檔案的型別時需要注意

檔案有對應的型別引數,例如:

TXT:text/plain

Excel:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

Word:application/vnd.openxmlformats-officedocument.wordprocessingml.document

PDF:application/pdf

PNG:image/png

應用程式:application/octet-stream

測試得出以下結論:(可能有出入,僅供參考)

1、如果上傳檔案時,設定了對應的引數,

1.1、且瀏覽器支援預覽,則存取檔案地址時,直接能在瀏覽器中顯示檔案的內容,比如:TXT、PDF、PNG等

1.2、如果瀏覽器不支援預覽,則存取檔案地址時,瀏覽器會自動下載對應的檔案到本地,比如Excel、Word、應用程式等

2、如果上傳檔案時,沒有設定對應的引數,比如直接設定application/x-www-form-urlencoded、application/octet-stream,就算是TXT檔案,瀏覽器也不會支援預覽,直接下載

3、如果上傳檔案時,沒有設定正確的引數,比如Excel檔案,設定了text/plain引數,則會導致檔案識別失敗,瀏覽器既不下載也不能正確預覽

3.2、引數細節

關於引數中的q=0.8

 q:相對品質因數。它指定使用者喜歡哪種語言,範圍從0到1,預設為1。

該質量值表示使用者對由該範圍指定的語言的偏好的估計