在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。
该质量值表示用户对由该范围指定的语言的偏好的估计