메뉴 건너뛰기

SAP 한국 커뮤니티

FTP File Tranfer 샘플 소스

양키(이경환) 2014.03.04 22:11 조회 수 : 5147

REPORT zftptrasfr
NO STANDARD PAGE HEADING LINE-SIZE 255.
 
************************************************************************
*T Y P E S
************************************************************************
TYPES:
BEGIN OF x_cmdout,
  line(100) TYPE c,
END OF x_cmdout.
 
TYPE-POOLS : slis.
 
************************************************************************
*    D A T A
************************************************************************
*data specifications of FTP server
 
*Handler and Key
DATA: w_cmd(40) TYPE c,
      w_hdl TYPE i,
      w_logid TYPE zda_log_ipl VALUE 1,
      w_key TYPE i VALUE 26101957,
      w_slen TYPE i,
      wa_iplftp TYPE ztbl_ipl_ftp,
      it_iplftp TYPE STANDARD TABLE OF ztbl_ipl_ftp,
      wa_cmdout TYPE x_cmdout,
      it_cmdout TYPE STANDARD TABLE OF x_cmdout.
 
*Constant declarations
CONSTANTS:  c_dest TYPE rfcdes-rfcdest VALUE 'SAPFTPA',
            c_host(11) TYPE c VALUE '172.XX.X.XX',
            c_ftp(6) TYPE c VALUE 'FTPDIRECTORY',
            c_sap(16) TYPE c VALUE '/sap/inbound/SAPDIRECTORY'.
 
************************************************************************
*    P A R A M E T E R S
************************************************************************
 
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS: p_user(30) TYPE c LOWER CASE ,
            p_pwd(30) TYPE c LOWER CASE ,
            p_host(64) TYPE c LOWER CASE DEFAULT c_host ,
            p_edi TYPE zedi, " 5-character domain containing EDI profiles
            p_ftp TYPE e_dexcommfilepath LOWER CASE DEFAULT c_ftp,
            p_sap TYPE esefilepath LOWER CASE DEFAULT c_sap.
SELECTION-SCREEN END OF BLOCK b1.
 
************************************************************************
*AT SELECTION SCREEN Events
************************************************************************
 
AT SELECTION-SCREEN OUTPUT.
 
  LOOP AT SCREEN.
    CASE screen-name.
      WHEN 'P_PWD'."Set the password field as invisible
        screen-invisible = '1'.
        MODIFY SCREEN.
    ENDCASE.
  ENDLOOP.
 
************************************************************************
*    S T A R T - O F - S E L E C T I O N
************************************************************************
 
START-OF-SELECTION.
 
*Connect to the FTP server.
  PERFORM ftp_connect.
 
*Find all files in the directory and store inside the Log table
  PERFORM log_files.
 
*Check for transfer of files to FTP.
  IF it_iplftp[] IS INITIAL.
    MESSAGE text-003 TYPE 'I'.
    LEAVE LIST-PROCESSING.
  ENDIF.
 
*Change the local save directory and download to SAP application server.
  PERFORM move_files.
 
*Close the connection
  PERFORM close_ftp.
 
************************************************************************
*END-OF-SELECTION
************************************************************************
END-OF-SELECTION.
 
*Display report.
  PERFORM disp_res.
 
*&---------------------------------------------------------------------
*& Form FTP_CONNECT
*&---------------------------------------------------------------------
*Make a connection to the FTP server
*----------------------------------------------------------------------
FORM ftp_connect .
 
  SET EXTENDED CHECK OFF.
  w_slen = strlen( p_pwd ).
 
  CALL FUNCTION 'HTTP_SCRAMBLE'
    EXPORTING
      source      = p_pwd
      sourcelen   = w_slen
      key         = w_key
    IMPORTING
      destination = p_pwd.
 
  CALL FUNCTION 'FTP_CONNECT'
    EXPORTING
      user            = p_user
      password        = p_pwd
      host            = p_host
      rfc_destination = c_dest
    IMPORTING
      handle          = w_hdl
    EXCEPTIONS
      not_connected   = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
*Message will arise in case of any issues in connecting to the FTP server.
    MESSAGE text-e01 TYPE 'I'.
    LEAVE LIST-PROCESSING.
  ENDIF.
 
ENDFORM. " FTP_CONNECT
*&---------------------------------------------------------------------
*& Form LOG_FILES
*&---------------------------------------------------------------------
*find all the files in the home directory
*----------------------------------------------------------------------
FORM log_files .
 
*Change directory to the FTP directory for LUPIN
  CONCATENATE 'cd' p_ftp INTO w_cmd SEPARATED BY space.
 
  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle        = w_hdl
      command       = w_cmd
      compress      = 'N'
    TABLES
      data          = it_cmdout
    EXCEPTIONS
      tcpip_error   = 1
      command_error = 2
      data_error    = 3
      OTHERS        = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
  REFRESH it_cmdout.
  CLEAR w_cmd.
  w_cmd = 'ls'.
 
  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle        = w_hdl
      command       = w_cmd
      compress      = 'N'
    TABLES
      data          = it_cmdout
    EXCEPTIONS
      tcpip_error   = 1
      command_error = 2
      data_error    = 3
      OTHERS        = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
 
*All customized checks for file to be placed here.
  LOOP AT it_cmdout INTO wa_cmdout FROM 4.
 
*Check the command line for a file
    CHECK wa_cmdout-line+68(4) EQ '.txt'.
 
*Move all the files to the FTP table
    wa_iplftp-file_nm = wa_cmdout-line+39(33).
    wa_iplftp-log_id = w_logid.
 
*Currently the transfer date/time will be taken
    wa_iplftp-fdate = sy-datum."WA_CMDOUT-LINE+39(8).
    wa_iplftp-ftime = sy-uzeit."WA_CMDOUT-LINE+48(6).
    wa_iplftp-ernam = sy-uname.
 
    APPEND wa_iplftp TO it_iplftp.
 
  ENDLOOP.
  REFRESH it_cmdout.
 
ENDFORM. " LOG_FILES
*&---------------------------------------------------------------------
*& Form MOVE_FILES
*&---------------------------------------------------------------------
FORM move_files .
 
  DATA : l_indx TYPE i.
 
  CLEAR w_cmd.
  CONCATENATE 'lcd' p_sap INTO w_cmd SEPARATED BY space.
 
*Change the local directory to the sap inbound directory
  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle        = w_hdl
      command       = w_cmd
      compress      = 'N'
    TABLES
      data          = it_cmdout
    EXCEPTIONS
      command_error = 1
      tcpip_error   = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
 
  REFRESH it_cmdout.
*Move each file from FTP to SAP inbound directory
  LOOP AT it_iplftp INTO wa_iplftp WHERE stat EQ space OR
  stat EQ 'CR'.
 
    l_indx = sy-tabix.
 
    CLEAR w_cmd.
    REFRESH it_cmdout.
 
    CONCATENATE 'get' wa_iplftp-file_nm INTO w_cmd SEPARATED BY space.
 
    CALL FUNCTION 'FTP_COMMAND'
      EXPORTING
        handle        = w_hdl
        command       = w_cmd
        compress      = 'N'
      TABLES
        data          = it_cmdout
      EXCEPTIONS
        command_error = 1
        tcpip_error   = 2.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
 
    READ TABLE it_cmdout INTO wa_cmdout INDEX 2.
    IF sy-subrc EQ 0.
      IF wa_cmdout-line CS 'command successful'.
        wa_iplftp-stat = 'CR'.
        wa_iplftp-mess = text-004.
      ELSE.
        wa_iplftp-stat = 'EF'.
        wa_iplftp-mess = text-005.
      ENDIF.
    ENDIF.
 
    MODIFY it_iplftp FROM wa_iplftp INDEX l_indx.
*delete from FTP
    CLEAR w_cmd.
    CONCATENATE 'delete' wa_iplftp-file_nm INTO w_cmd SEPARATED BY space.
 
    CALL FUNCTION 'FTP_COMMAND'
      EXPORTING
        handle        = w_hdl
        command       = w_cmd
        compress      = 'N'
      TABLES
        data          = it_cmdout
      EXCEPTIONS
        command_error = 1
        tcpip_error   = 2.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
  ENDLOOP.
 
*Insert all table entries.
  INSERT ztbl_ipl_ftp FROM TABLE it_iplftp.
  IF sy-subrc NE 0.
    MESSAGE text-006 TYPE 'I'.
  ENDIF.
 
ENDFORM. " MOVE_FILES
*&---------------------------------------------------------------------
*& Form CLOSE_FTP
*&---------------------------------------------------------------------
FORM close_ftp .
 
  CALL FUNCTION 'FTP_DISCONNECT'
    EXPORTING
      handle = w_hdl.
 
  CALL FUNCTION 'RFC_CONNECTION_CLOSE'
    EXPORTING
      destination = c_dest
    EXCEPTIONS
      OTHERS      = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
 
ENDFORM. " CLOSE_FTP
*&---------------------------------------------------------------------
*& Form DISP_RES
*&---------------------------------------------------------------------
*Display the output
*----------------------------------------------------------------------
FORM disp_res .
 
  DATA : l_layout TYPE slis_layout_alv.
 
  l_layout-colwidth_optimize = 'X'.
 
*Display table contents of updated files
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      i_structure_name   = 'ZTBL_IPL_FTP'
      is_layout          = l_layout
    TABLES
      t_outtab           = it_iplftp
    EXCEPTIONS
      program_error      = 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.
  ENDIF.
 
ENDFORM. " DISP_RES