FUNCTION ZSEND_FTP.
*”———————————————————————-
*”*”Local Interface:
*” IMPORTING
*” REFERENCE(P_LOOPTIME) TYPE SY-LOOPC DEFAULT 5000
*” REFERENCE(P_FTPHOST) TYPE C DEFAULT ‘172.30.2.170’
*” REFERENCE(P_CMD) TYPE C
*” REFERENCE(P_FNAME) TYPE C
*” REFERENCE(P_SEPERATOR) TYPE CHAR01 DEFAULT ‘^’
*” REFERENCE(P_FORMAT) TYPE CHAR03 DEFAULT ‘TEX’
*” REFERENCE(P_XMLDATA) OPTIONAL
*” REFERENCE(P_USER) TYPE C DEFAULT ‘PODADM’
*” REFERENCE(P_PASSW) TYPE C DEFAULT [email protected]‘
*” REFERENCE(P_ENCODE) TYPE ABAP_ENCOD DEFAULT ‘UTF-8’
*” TABLES
*” ITAB OPTIONAL
*” IT_FIELDCAT OPTIONAL
*” EXCEPTIONS
*” FTP_ERROR
*” CONV_ERROR
*”———————————————————————-
DATA: mi_key TYPE i VALUE 26101957,
mi_handle TYPE i,
slen TYPE i.
DATA: lv_error.
DATA: suser(30) TYPE c VALUE ‘PODADM’,
spwd(30) TYPE c VALUE [email protected]‘.
DATA: csvtab TYPE truxs_t_text_data,
xmltab TYPE truxs_xml_table.
TYPES: BEGIN OF blob,
line(132) TYPE x,
END OF blob.
DATA: bindata TYPE TABLE OF blob WITH HEADER LINE.
DATA: l_v_leng TYPE i.
DATA: e_string TYPE string.
DATA: e_xstring TYPE xstring.
* DATA: fieldcat TYPE lvc_s_fcat.
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_bindata> TYPE STANDARD TABLE,
<dyn_wa>,
<dyn_itab>,
<dyn_value2>,
<dyn_value1>.
DATA: dy_table TYPE REF TO data,
dy_line TYPE REF TO data,
it_stru TYPE lvc_t_fcat,
wa_stru TYPE lvc_s_fcat.
DATA : idetails TYPE abap_compdescr_tab,
xdetails TYPE abap_compdescr,
i_time type i.
DATA : ref_table_des TYPE REF TO cl_abap_structdescr.
FIELD-SYMBOLS: <dyn_field>,
<dyn_value>.
* CLEAR p_looptime.
IF p_looptime <= 20 AND p_looptime <> 0.
i_time = p_looptime.
ELSE.
i_time = 500.
ENDIF.
IF p_format = ‘TEX’.
IF it_fieldcat[] IS NOT INITIAL.
LOOP AT it_fieldcat ASSIGNING <dyn_field>.
ASSIGN COMPONENT ‘FIELDNAME’ OF STRUCTURE <dyn_field> TO <dyn_value>.
IF sy-subrc = 0.
wa_stru-fieldname = <dyn_value>.
wa_stru-inttype = ‘C’.
wa_stru-intlen = 255.
APPEND wa_stru TO it_stru.
CLEAR: wa_stru.
ENDIF.
ENDLOOP.
ELSE.
ref_table_des ?= cl_abap_typedescr=>describe_by_data( itab ).
idetails[] = ref_table_des->components[].
LOOP AT idetails INTO xdetails.
wa_stru-fieldname = xdetails-name.
wa_stru-inttype = ‘C’.
wa_stru-intlen = 255.
APPEND wa_stru TO it_stru.
CLEAR: wa_stru.
ENDLOOP.
ENDIF.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_stru
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO <dyn_table>.
CREATE DATA dy_line LIKE LINE OF <dyn_table>. ” 建立一个与动态内表结构相同的数据对象,且数据对象为是一个结构
ASSIGN dy_line->* TO <dyn_wa>. ” 用<dyn_wa>指针指向该结构
LOOP AT itab ASSIGNING <dyn_itab>.
LOOP AT it_stru INTO wa_stru.
ASSIGN COMPONENT wa_stru-fieldname OF STRUCTURE <dyn_itab> TO <dyn_value1>.
IF sy-subrc = 0.
ASSIGN COMPONENT wa_stru-fieldname OF STRUCTURE <dyn_wa> TO <dyn_value2>.
IF sy-subrc = 0.
MOVE <dyn_value1> TO <dyn_value2>.
SHIFT <dyn_value2> LEFT DELETING LEADING space.
* <dyn_value2> = <dyn_value1>.
REPLACE ALL OCCURRENCES OF ‘;’ IN <dyn_value2> WITH space.
REPLACE ALL OCCURRENCES OF ‘^’ IN <dyn_value2> WITH space.
REPLACE ALL OCCURRENCES OF ‘,’ IN <dyn_value2> WITH space.
ENDIF.
ENDIF.
ENDLOOP.
APPEND <dyn_wa> TO <dyn_table>.
ENDLOOP.
DATA: lv_text TYPE string.
IF <dyn_wa> IS ASSIGNED.
CLEAR: <dyn_wa>.
ENDIF.
* FREE: <dyn_value2>, <dyn_value1>, <dyn_wa>, <dyn_field>, <dyn_value>.
LOOP AT it_fieldcat ASSIGNING <dyn_field>.
CLEAR: lv_text.
ASSIGN COMPONENT ‘SELTEXT_L’ OF STRUCTURE <dyn_field> TO <dyn_value>.
IF sy-subrc <> 0.
ASSIGN COMPONENT ‘SCRTEXT_L’ OF STRUCTURE <dyn_field> TO <dyn_value>.
ENDIF.
IF <dyn_value> IS ASSIGNED.
IF <dyn_value> IS INITIAL.
ASSIGN COMPONENT ‘SELTEXT_M’ OF STRUCTURE <dyn_field> TO <dyn_value>.
IF sy-subrc <> 0.
ASSIGN COMPONENT ‘SCRTEXT_M’ OF STRUCTURE <dyn_field> TO <dyn_value>.
ENDIF.
ENDIF.
ENDIF.
IF <dyn_value> IS ASSIGNED.
lv_text = <dyn_value>.
ENDIF.
ASSIGN COMPONENT ‘FIELDNAME’ OF STRUCTURE <dyn_field> TO <dyn_value>.
ASSIGN COMPONENT <dyn_value> OF STRUCTURE <dyn_wa> TO <dyn_value2>.
IF sy-subrc = 0.
<dyn_value2> = lv_text.
ENDIF.
ENDLOOP.
IF <dyn_wa> IS NOT INITIAL.
INSERT <dyn_wa> INTO <dyn_table> INDEX 1.
ENDIF.
ENDIF.
****
DATA: l_dom TYPE REF TO if_ixml_element,
m_document TYPE REF TO if_ixml_document,
g_ixml TYPE REF TO if_ixml,
* w_string TYPE xstring,
w_size TYPE i,
w_result TYPE i,
w_line TYPE string,
it_xml TYPE dcxmllines,
s_xml LIKE LINE OF it_xml,
w_rc LIKE sy-subrc.
DATA: lt_ftpcomm TYPE TABLE OF char16384.
DATA: xml TYPE dcxmllines.
DATA: rc TYPE sy-subrc,
BEGIN OF xml_tab OCCURS 0,
d LIKE LINE OF xml,
END OF xml_tab.
DATA: wa_sy LIKE syst.
*** convert table
DATA: crlf(2) TYPE c.
DATA:wa_csvtab LIKE LINE OF csvtab,
wa_xmltab LIKE LINE OF xmltab.
crlf = cl_abap_char_utilities=>cr_lf.
IF p_user = ”.
suser = ‘PODADM’.
spwd = [email protected]‘.
ELSE.
suser = p_user.
spwd = p_passw.
ENDIF.
slen = STRLEN( spwd ).
IF p_format = ‘TEX’ OR p_format IS INITIAL.
CALL FUNCTION ‘SAP_CONVERT_TO_TEX_FORMAT’
EXPORTING
i_field_seperator = p_seperator
TABLES
i_tab_sap_data = <dyn_table>
CHANGING
i_tab_converted_data = csvtab
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 RAISING conv_error.
ENDIF.
LOOP AT csvtab INTO wa_csvtab.
CONCATENATE e_string wa_csvtab crlf INTO e_string.
ENDLOOP.
TRY.
CALL METHOD cl_bcs_convert=>string_to_xstring
EXPORTING
iv_string = e_string
* iv_convert_cp = ‘X’
iv_codepage = P_ENCODE “‘UTF-8’
* iv_add_bom =
RECEIVING
ev_xstring = e_xstring.
.
CATCH cx_bcs .
ENDTRY.
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING
buffer = e_xstring
IMPORTING
output_length = w_size
TABLES
binary_tab = bindata[].
ASSIGN bindata[] TO <dyn_bindata>.
ENDIF.
IF p_format = ‘EDI’ AND p_xmldata IS NOT INITIAL.
e_string = p_xmldata.
TRY.
CALL METHOD cl_bcs_convert=>string_to_xstring
EXPORTING
iv_string = e_string
* iv_convert_cp = ‘X’
iv_codepage = P_ENCODE “‘UTF-8’
* iv_add_bom =
RECEIVING
ev_xstring = e_xstring.
.
CATCH cx_bcs .
ENDTRY.
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING
buffer = e_xstring
IMPORTING
output_length = w_size
TABLES
binary_tab = bindata[].
ASSIGN bindata[] TO <dyn_bindata>.
ENDIF.
IF p_format = ‘XML’.
CLASS cl_ixml DEFINITION LOAD.
g_ixml = cl_ixml=>create( ).
IF g_ixml IS INITIAL.
RAISE conv_error.
ENDIF.
m_document = g_ixml->create_document( ).
IF m_document IS INITIAL.
MESSAGE e101(zsdmsg) WITH ‘XML convert error1’ RAISING conv_error.
ENDIF.
IF p_xmldata IS INITIAL.
ASSIGN itab[] TO <dyn_table>.
CALL FUNCTION ‘SDIXML_DATA_TO_DOM’
EXPORTING
name = ‘XML_DATA’
dataobject = <dyn_table>
IMPORTING
data_as_dom = l_dom
CHANGING
document = m_document
EXCEPTIONS
illegal_name = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 RAISING conv_error.
ENDIF.
ELSE.
CALL FUNCTION ‘SDIXML_DATA_TO_DOM’
EXPORTING
name = ‘XML_DATA’
dataobject = p_xmldata
IMPORTING
data_as_dom = l_dom
CHANGING
document = m_document
EXCEPTIONS
illegal_name = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 RAISING conv_error.
ENDIF.
ENDIF.
IF l_dom IS INITIAL.
MESSAGE e101(zsdmsg) WITH ‘XML convert error2’ RAISING conv_error.
ENDIF.
w_rc = m_document->append_child( new_child = l_dom ).
IF w_rc <> 0.
MESSAGE e101(zsdmsg) WITH ‘XML convert error3’ RAISING conv_error.
ENDIF.
CALL FUNCTION ‘SDIXML_DOM_TO_XML’
EXPORTING
document = m_document
IMPORTING
* xml_as_string = w_string
size = w_size
TABLES
xml_as_table = it_xml
EXCEPTIONS
no_document = 1
OTHERS = 2.
LOOP AT it_xml INTO xml_tab-d.
APPEND xml_tab.
ENDLOOP.
ASSIGN xml_tab[] TO <dyn_bindata>.
ENDIF.
IF p_format = ‘PDF’.
e_xstring = p_xmldata.
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING
buffer = e_xstring
IMPORTING
output_length = w_size
TABLES
binary_tab = bindata[].
ASSIGN bindata[] TO <dyn_bindata>.
ENDIF.
IF <dyn_bindata> IS NOT ASSIGNED.
MESSAGE e101(zsdmsg) WITH ‘XML convert error4’ RAISING conv_error.
ENDIF.
CALL FUNCTION ‘HTTP_SCRAMBLE’
EXPORTING
SOURCE = spwd
sourcelen = slen
key = mi_key
IMPORTING
destination = spwd.
CLEAR: lv_error.
DO i_time TIMES.
IF lv_error = ‘X’.
WAIT UP TO 10 SECONDS.
ENDIF.
CLEAR: lv_error, wa_sy.
CALL FUNCTION ‘FTP_CONNECT’
EXPORTING
user = suser
password = spwd
host = p_ftphost
rfc_destination = ‘SAPFTP’
IMPORTING
handle = mi_handle
EXCEPTIONS
not_connected = 1
OTHERS = 2.
IF sy-subrc <> 0.
lv_error = ‘X’.
wa_sy = sy.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4 INTO LV_MESSAGE.
ENDIF.
CALL FUNCTION ‘FTP_COMMAND’
EXPORTING
handle = mi_handle
command = p_cmd
TABLES
data = lt_ftpcomm
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
lv_error = ‘X’.
wa_sy = sy.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4 INTO LV_MESSAGE.
ENDIF.
CALL FUNCTION ‘FTP_R3_TO_SERVER’
EXPORTING
handle = mi_handle
fname = p_fname
blob_length = w_size
* CHARACTER_MODE =
TABLES
blob = <dyn_bindata>
* TEXT =
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4
.
IF sy-subrc <> 0.
lv_error = ‘X’.
wa_sy = sy.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4 INTO LV_MESSAGE.
ENDIF.
CALL FUNCTION ‘FTP_DISCONNECT’
EXPORTING
handle = mi_handle.
IF lv_error = ”.
EXIT.
ENDIF.
ENDDO.
IF lv_error = ‘X’.
MESSAGE ID wa_sy-msgid TYPE wa_sy-msgty NUMBER wa_sy-msgno
WITH wa_sy-msgv1 wa_sy-msgv2 wa_sy-msgv3 wa_sy-msgv4 RAISING ftp_error.
ENDIF.
ENDFUNCTION.
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/19569.html