ZBC_RFC_UTIL_BASE64_A_FTP
* Autor : Roberto Mejia Andrade *
* Consultora : Xternal *
* Módulo : SF *
* Fecha : 15.02.2016 *
* Req : R3_001 *
* Descripción: Guardar archivo en base 64 a FTP *
*----------------------------------------------------------------------*
DATA: ls_handle TYPE i,
ls_long TYPE i,
ls_valida TYPE flag,
ls_aux TYPE c LENGTH 255,
ls_aux1 TYPE string,
ls_aux2 TYPE string,
ls_user TYPE c LENGTH 300,
ls_clave TYPE c LENGTH 300,
ls_host TYPE c LENGTH 300,
ls_xst TYPE xstring,
ls_mensaje TYPE string,
ls_ruta TYPE c LENGTH 300,
ls_ruta1 TYPE c LENGTH 300,
ls_data TYPE c LENGTH 26200,
ls_dato TYPE string,
ltd_data LIKE STANDARD TABLE OF ls_data,
ltd_data1 LIKE STANDARD TABLE OF ls_data,
ltd_data2 LIKE STANDARD TABLE OF ls_data.
FIELD-SYMBOLS: <ls_arb64> TYPE zwut_arb64,
<ls_mensaje> TYPE zflmin..
* Si los parámetros IP_SVFTP o IP_USUAR no existen se obtendrán estos valores
* de la tabla ZTBC_DATA donde CODIG = 'X5
IF ip_svftp IS INITIAL.
SELECT SINGLE descr INTO ip_svftp FROM ztbc_data
WHERE codig = 'X5' AND
argum = 'SERVERFTP'.
ENDIF.
IF ip_usuar IS INITIAL.
SELECT SINGLE descr INTO ip_usuar FROM ztbc_data
WHERE codig = 'X5' AND
argum = 'USUARIOFTP'.
SELECT SINGLE descr INTO ip_clave FROM ztbc_data
WHERE codig = 'X5' AND
argum = 'PASSFTP'.
ENDIF.
* Usar IP_RFCDS igual a 'SAPFTPA' para cuando la función se ejecuta como
* proceso en fondo, sino IP_RFCDS = 'SAPFTP'
IF sy-batch = 'X'.
ip_rfcds = 'SAPFTPA'.
ELSE.
ip_rfcds = 'SAPFTP'.
ENDIF.
ls_user = ip_usuar.
ls_clave = ip_clave.
ls_host = ip_svftp.
LOOP AT it_arb64 ASSIGNING <ls_arb64>.
* Estos datos servirán para la conexión con el FTP mediante la función FTP_CONNECT
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = ls_user
password = ls_clave
host = ls_host
rfc_destination = ip_rfcds
IMPORTING
handle = ls_handle
EXCEPTIONS
not_connected = 1
OTHERS = 2.
IF sy-subrc = 0.
CLEAR: ltd_data[], ls_ruta1, ls_valida.
ls_aux = <ls_arb64>-rtarc.
IF ls_aux IS NOT INITIAL.
IF ls_aux+0(1) = '/'.
CLEAR ls_aux+0(1).
ENDIF.
CONDENSE ls_aux NO-GAPS.
ls_long = strlen( ls_aux ).
ls_long = ls_long - 1.
IF ls_aux+ls_long(1) = '/'.
CLEAR ls_aux+ls_long(1).
ENDIF.
ENDIF.
CLEAR ls_long.
CONCATENATE <ls_arb64>-rtarc <ls_arb64>-nmarc INTO ls_ruta
SEPARATED BY '/'.
REPLACE ALL OCCURRENCES OF '//' IN ls_ruta WITH '/'.
CHECK ls_valida IS INITIAL.
CLEAR ltd_data[].
IF <ls_arb64>-tpope = 'B'.
* Elimina el archivo existente
APPEND INITIAL LINE TO et_mensj ASSIGNING <ls_mensaje>.
CLEAR ls_ruta1.
ls_ruta1 = ls_ruta.
CONCATENATE 'del' ls_ruta INTO ls_ruta SEPARATED BY space.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = ls_handle
command = ls_ruta
TABLES
data = ltd_data
EXCEPTIONS
command_error = 1
tcpip_error = 2.
IF sy-subrc = 0.
<ls_mensaje>-cmin = 'S'.
CONCATENATE ls_ruta1 ': Archivo eliminado'
INTO <ls_mensaje>-dsmin.
ELSE.
<ls_mensaje>-cmin = 'E'.
CONCATENATE ls_ruta1 ': Archivo no eliminado'
INTO <ls_mensaje>-dsmin.
ENDIF.
ELSEIF <ls_arb64>-tpope = 'C'.
SPLIT ls_aux AT '/' INTO ls_aux ls_aux1.
IF ls_aux1 IS INITIAL.
* Verificamos si existe la ruta
CONCATENATE 'dir' ls_aux INTO ls_ruta1 SEPARATED BY space.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = ls_handle
command = ls_ruta1
TABLES
data = ltd_data
EXCEPTIONS
command_error = 1
tcpip_error = 2.
IF sy-subrc NE 0.
* Procedemos a crear el directorio
CLEAR ls_ruta1.
CONCATENATE 'mkdir' ls_aux INTO ls_ruta1 SEPARATED BY space.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = ls_handle
command = ls_ruta1
TABLES
data = ltd_data
EXCEPTIONS
command_error = 1
tcpip_error = 2.
ENDIF.
ELSE.
WHILE ls_aux1 IS NOT INITIAL.
* Verificamos si existe la ruta
CONCATENATE 'dir' ls_aux INTO ls_ruta1 SEPARATED BY space.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = ls_handle
command = ls_ruta1
TABLES
data = ltd_data
EXCEPTIONS
command_error = 1
tcpip_error = 2.
IF sy-subrc NE 0.
* Procedemos a crear el directorio
CLEAR ls_ruta1.
CONCATENATE 'mkdir' ls_aux INTO ls_ruta1 SEPARATED BY space.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = ls_handle
command = ls_ruta1
TABLES
data = ltd_data
EXCEPTIONS
command_error = 1
tcpip_error = 2.
ENDIF.
SPLIT ls_aux1 AT '/' INTO ls_aux2 ls_aux1.
CONCATENATE ls_aux ls_aux2 INTO ls_aux SEPARATED BY '/'.
IF ls_aux1 IS INITIAL.
* Verificamos si existe la ruta
CONCATENATE 'dir' ls_aux INTO ls_ruta1 SEPARATED BY space.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = ls_handle
command = ls_ruta1
TABLES
data = ltd_data
EXCEPTIONS
command_error = 1
tcpip_error = 2.
IF sy-subrc NE 0.
* Procedemos a crear el directorio
CLEAR ls_ruta1.
CONCATENATE 'mkdir' ls_aux INTO ls_ruta1 SEPARATED BY space.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = ls_handle
command = ls_ruta1
TABLES
data = ltd_data
EXCEPTIONS
command_error = 1
tcpip_error = 2.
ENDIF.
ENDIF.
ENDWHILE.
ENDIF.
* Crear los archivos
IF <ls_arb64>-bas64 IS NOT INITIAL.
* Pasamos de base 64 a xstring
CALL FUNCTION 'SSFC_BASE64_DECODE'
EXPORTING
b64data = <ls_arb64>-bas64
IMPORTING
bindata = ls_xst
EXCEPTIONS
ssf_krn_error = 1
ssf_krn_noop = 2
ssf_krn_nomemory = 3
ssf_krn_opinv = 4
ssf_krn_input_data_error = 5
ssf_krn_invalid_par = 6
ssf_krn_invalid_parlen = 7
OTHERS = 8.
* Pasamos de xstring a binario
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = ls_xst
IMPORTING
output_length = ls_long
TABLES
binary_tab = ltd_data2.
* Función para cargar archivo al servidor FTTP
CALL FUNCTION 'FTP_R3_TO_SERVER'
EXPORTING
handle = ls_handle
fname = ls_ruta
blob_length = ls_long
* character_mode = 'X'
TABLES
blob = ltd_data2
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
IF sy-subrc = 0.
APPEND INITIAL LINE TO et_mensj ASSIGNING <ls_mensaje>.
<ls_mensaje>-cmin = 'S'.
CONCATENATE <ls_arb64>-nmarc ': Archivo cargado correctamente'
INTO <ls_mensaje>-dsmin.
ELSE.
APPEND INITIAL LINE TO et_mensj ASSIGNING <ls_mensaje>.
<ls_mensaje>-cmin = 'E'.
CONCATENATE <ls_arb64>-nmarc ': Archivo con errores / Atchivo no cargado'
INTO <ls_mensaje>-dsmin.
ENDIF.
ENDIF.
ENDIF.
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = ls_handle.
ELSE.
APPEND INITIAL LINE TO et_mensj ASSIGNING <ls_mensaje>.
<ls_mensaje>-cmin = 'E'.
CONCATENATE ls_host ': No se pudo conectar al servidor FTTP'
INTO <ls_mensaje>-dsmin.
EXIT.
ENDIF.
ENDLOOP.
No hay comentarios:
Publicar un comentario