matinal:SAP ABAP调用http接口上传文件_sap通过接口传输附件

*& Form FRM_F4_FILE
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_f4_file .
CALL FUNCTION ‘F4_FILENAME’
IMPORTING
file_name = p_file.
ENDFORM.

&---------------------------------------------------------------------
*& Form FRM_READ_UPLOAD_FILE
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_read_upload_file .

DATA: lv_file_path TYPE string,
lv_file_length TYPE i,
lv_file_name TYPE dbmsgora-filename.

lv_file_path = p_file.
lv_file_name = p_file.

CALL FUNCTION ‘SPLIT_FILENAME’
EXPORTING
long_filename = lv_file_name "上传文件路径
IMPORTING
pure_filename = gv_file_name "文件名称
pure_extension = gv_file_type. "文件后缀

CALL FUNCTION ‘GUI_UPLOAD’
EXPORTING
filename = lv_file_path
filetype = ‘BIN’
IMPORTING
filelength = lv_file_length
TABLES
data_tab = gt_file
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.
MESSAGE s001 WITH ‘上传文件失败’ DISPLAY LIKE ‘E’.
LEAVE LIST-PROCESSING.
ENDIF.

"转xstring
CALL FUNCTION ‘SCMS_BINARY_TO_XSTRING’
EXPORTING
input_length = lv_file_length
IMPORTING
buffer = gv_file
TABLES
binary_tab = gt_file
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE s001 WITH ‘转xstring失败’ DISPLAY LIKE ‘E’.
LEAVE LIST-PROCESSING.
ENDIF.

ENDFORM.

&---------------------------------------------------------------------
*& Form FRM_CALL_HTTP_TO_UPLOAD_FILE
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_call_http_to_upload_file .

DATA: lo_http_client TYPE REF TO if_http_client, "http客户端
lo_http_entity TYPE REF TO if_http_entity, "http实体
lt_http_header TYPE tihttpnvp, "http头部信息
ls_http_header TYPE ihttpnvp,
lv_url TYPE string, "http请求
lv_len TYPE i, "请求数据的长度
lv_response TYPE string, "http响应返回信息
lv_code TYPE i. "http状态码

DATA: lv_error_code TYPE sysubrc,
lv_error_msg TYPE string.

DATA: lv_file_name TYPE savwctxt-fieldcont,
lv_name_encode TYPE savwctxt-fieldcont.

DATA: lv_content_disposition TYPE string,
lv_content_type TYPE string.

lv_url = ‘http://192.168.194.12:8080/jeecg-boot/ncip/file/upload’.

"创建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
OTHERS = 4.

"设置http为post请求
lo_http_client->request->set_method( ‘POST’ ).

ls_http_header-name = ‘X-Access-Token’.
ls_http_header-value = ‘eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2ODYxODM1OTcsInVzZXJuYW1lIjoiYWRtaW4ifQ.fpxtvBsFDVR5WmjP3iLXhD-K7ZiULofqwQ1S_DE9Hxk’.
APPEND ls_http_header TO lt_http_header.

ls_http_header-name = ‘Content-Type’.
ls_http_header-value = ‘multipart/form-data’.
APPEND ls_http_header TO lt_http_header.

"设置http header
CALL METHOD lo_http_client->request->set_header_fields
EXPORTING
fields = lt_http_header.

  • 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_http_entity = lo_http_client->request->if_http_entity~add_multipart( ).

    "utf-8编码文件名(目的是让上传后的文件名跟原来一样,不然会乱码)
    lv_file_name = gv_file_name.
    CALL FUNCTION ‘WWW_URLENCODE’
    EXPORTING
    value = lv_file_name
    IMPORTING
    value_encoded = lv_name_encode.

    lv_content_disposition = ‘form-data; name=“file”; filename="’ && lv_name_encode && ‘.’ && gv_file_type && ‘"’.

    CALL METHOD lo_http_entity->set_header_field
    EXPORTING
    name = ‘Content-Disposition’
    "value = ‘form-data; name=“file”; filename=“测试文件.pdf”’
    value = lv_content_disposition.

    "文件后缀转小写
    TRANSLATE gv_file_type TO LOWER CASE.

    CASE gv_file_type.
    WHEN ‘jpg’.
    lv_content_type = ‘image/jpeg’.
    WHEN ‘png’.
    lv_content_type = ‘image/png’.
    WHEN ‘txt’.
    lv_content_type = ‘text/plain’.
    WHEN ‘pdf’.
    lv_content_type = ‘application/pdf’.
    WHEN ‘xlsx’.
    lv_content_type = ‘application/vnd.openxmlformats-officedocument.spreadsheetml.sheet’.
    WHEN ‘xls’.
    lv_content_type = ‘application/vnd.ms-excel’.
    WHEN ‘docx’.
    lv_content_type = ‘application/vnd.openxmlformats-officedocument.wordprocessingml.document’.
    WHEN ‘doc’.
    lv_content_type = ‘application/msword’.
    ENDCASE.

    CALL METHOD lo_http_entity->set_content_type
    EXPORTING
    content_type = lv_content_type.

    lv_len = xstrlen( gv_file ).

    CALL METHOD lo_http_entity->set_data
    EXPORTING
    data = gv_file
    offset = 0
    length = lv_len.

    "发送请求
    CALL METHOD lo_http_client->send
    EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state = 2
    http_processing_failed = 3
    http_invalid_timeout = 4
    OTHERS = 5.

    IF sy-subrc NE 0.

    CALL METHOD lo_http_client->get_last_error
    IMPORTING
    code = lv_error_code
    message = lv_error_msg.

    ENDIF.

    "请求返回结果
    CALL METHOD lo_http_client->receive
    EXCEPTIONS

常用的JavaScript设计模式

  • 单体模式

  • 工厂模式

  • 例模式

函数

  • 函数的定义

  • 局部变量和全局变量

  • 返回值

  • 匿名函数

  • 自运行函数

  • 闭包

  • 18
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值