代码语言:javascript复制
*看我文章发给我看。跟我玩绕口令呢。
*(某xx系统别tm总看我文章发给我们)特么的,我用得着你网上找文章吗。
REPORT zhttp_test MESSAGE-ID 00.
************************************************************************
* Tables Definitions
************************************************************************
TABLES: rlgrap.
************************************************************************
* Data Definitions 定义数据
************************************************************************
TYPES: BEGIN OF ty_file,
line(1024) TYPE x,
END OF ty_file.
DATA: gt_file TYPE TABLE OF ty_file.
DATA: gv_file_name TYPE sdbah-actid,
gv_file_type TYPE sdbad-funct,
gv_file TYPE xstring.
************************************************************************
* Includes Module 包含模块
************************************************************************
************************************************************************
* Selection Screen 选择屏幕
************************************************************************
PARAMETERS: p_file LIKE rlgrap-filename OBLIGATORY.
************************************************************************
* Initialization 初始化事件
************************************************************************
INITIALIZATION.
************************************************************************
* At Selection Screen PAI事件
************************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM frm_f4_file.
************************************************************************
* At Selection Screen Output PBO事件
************************************************************************
AT SELECTION-SCREEN OUTPUT.
************************************************************************
* Report Format 报表格式
************************************************************************
TOP-OF-PAGE.
END-OF-PAGE.
************************************************************************
* Main Process 主要逻辑
************************************************************************
START-OF-SELECTION.
"读取上传文件
PERFORM frm_read_upload_file.
"调用http接口上传文件
PERFORM frm_call_http_to_upload_file.
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& 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
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
"请求返回的状态码
CALL METHOD lo_http_client->response->get_status
IMPORTING
code = lv_code.
"获取接口返回的数据
lv_response = lo_http_client->response->get_cdata( ).
cl_demo_output=>write( lv_response ).
cl_demo_output=>display( ).
ENDFORM.