FTP服务器和ABAP的连接

来源:互联网 发布:祖国流氓我放心知乎 编辑:程序博客网 时间:2024/05/29 04:03

*&---------------------------------------------------------------------*
*& Report  ZLITEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zlitest.

TYPE-POOLS:slis,truxs.
TABLES: mseg,mkpf,makt,ekko,mara.

DATA: l_ftpcommand(128) TYPE c.  "FTP command
DATA: l_handle          TYPE i.  "HANDle
DATA:  pr_return TYPE c.
DATA lt_ftp TYPE truxs_t_text_data.
DATA ls_ftp TYPE truxs_t_text_data WITH HEADER LINE.

DATA: hdl TYPE i,
    key TYPE i VALUE 26101957,
    slen TYPE i.
DATA dest LIKE rfcdes-rfcdest VALUE 'SAPFTP'.


DATA:l_file_name(30) TYPE c.
DATA:l_path(255) TYPE c.
DATA:l_file(255) TYPE c.
DATA:  BEGIN OF lt_output OCCURS 0,
   name(10) TYPE c,
  age(2) TYPE n,
  address(20) TYPE c,
  END OF lt_output.

DATA:lt_output_ftp LIKE TABLE OF lt_output WITH HEADER LINE.
DATA  pv_pw(30) TYPE c.
lt_output-name = 'LIYUWU'.
lt_output-age = 24.
lt_output-address = 'hunang yongzhou'.
APPEND lt_output.
lt_output-name = 'FANGCHUN'.
lt_output-age = 22.
lt_output-address = 'shichuan'.
APPEND lt_output.



LOOP AT lt_output.
  MOVE-CORRESPONDING lt_output TO lt_output_ftp.
  APPEND lt_output_ftp.
  CLEAR lt_output_ftp.
ENDLOOP.

l_file_name = 'ftcput.txt'.

CALL FUNCTION 'FILE_GET_NAME'
  EXPORTING
    client           = sy-mandt
    logical_filename = 'ZFTOSHIBA'
    operating_system = sy-opsys
    eleminate_blanks = 'X'
  IMPORTING
    file_name        = l_path
  EXCEPTIONS
    file_not_found   = 1
    OTHERS           = 2.

CONCATENATE l_path l_file_name INTO l_file.

TRANSLATE l_file TO LOWER CASE.
*
*CALL FUNCTION 'ZTRANSVMI_FILE_TO_FTP'
*  EXPORTING
*    pv_path          = l_file
*    pv_host          = '10.10.11.131'
*    pv_user          = 'ittest'
*    pv_pw            = 'abcd!1234'
*    pv_ftppath       = 'toshiba\vmi\put'
*    pv_ftpfn         = 'miput.TXT'
** IMPORTING
**   PV_SUBRC         =
*  TABLES
*    pv_tbl           = lt_output_ftp
*          .
CALL FUNCTION 'SAP_CONVERT_TO_TEX_FORMAT'
  EXPORTING
    i_field_seperator          ='|'
*   I_LINE_HEADER              =
*   I_FILENAME                 =
*   I_APPL_KEEP                = ' '
  TABLES
    i_tab_sap_data             = lt_output_ftp
 CHANGING
   i_tab_converted_data       =  lt_ftp
 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.
ENDIF.

OPEN DATASET l_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc EQ 0.
  LOOP AT lt_ftp INTO ls_ftp.
    TRANSFER ls_ftp TO l_file.
  ENDLOOP.
ENDIF.

CLOSE DATASET l_file.
slen = STRLEN( 'abcd!1234' ) .

DO 10 TIMES.
  CALL FUNCTION 'HTTP_SCRAMBLE'
    EXPORTING
      SOURCE      = 'abcd!1234'
      sourcelen   = slen
      key         = key
    IMPORTING
      destination = pv_pw.

  CALL FUNCTION 'FTP_CONNECT'
    EXPORTING
      user                   =       'ittest'
      password               =   pv_pw
*   ACCOUNT                =
      host                   =     '10.10.11.131'
      rfc_destination        =   dest
*   GATEWAY_USER           =
*   GATEWAY_PASSWORD       =
*   GATEWAY_HOST           =
   IMPORTING
     handle                 = hdl.
* EXCEPTIONS
*   NOT_CONNECTED          = 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.
    EXIT.
  ENDIF.
ENDDO.
CLEAR l_ftpcommand.
CONCATENATE 'cd'  'TOSHIBA\VMI\PUT' INTO l_ftpcommand SEPARATED BY space.
perform frm_ftp_command using l_ftpcommand
      changing pr_return hdl.
clear l_ftpcommand.
l_ftpcommand = 'ascii'.
perform frm_ftp_command using l_ftpcommand
      changing pr_return hdl.
clear l_ftpcommand.
concatenate 'put'  l_file  'liyutest.txt'
into l_ftpcommand separated by space.
perform frm_ftp_command using l_ftpcommand
      changing pr_return
        hdl.




*&---------------------------------------------------------------------*
*&      Form  frm_ftp_command
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PV_COMMAND text
*      -->CHANGE     text
*      -->PV_RET     text
*      -->PV_HANDLE  text
*----------------------------------------------------------------------*
FORM frm_ftp_command USING pv_command changing pv_ret pv_handle.
  DATA:BEGIN OF it_result OCCURS 0,
    line(100) TYPE c,
    END OF it_result.
  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle        = pv_handle
      command       = pv_command
      compress      = 'N'
    TABLES
      data          = it_result
    EXCEPTIONS
      tcpip_error   = 1
      command_error = 2
      data_error    = 3.
  IF sy-subrc <> 0.
    pv_ret = '1'.
    CALL FUNCTION'FTP_DISCONNECT'
    EXPORTING
      handle = pv_handle.
    EXIT.
  ENDIF.
ENDFORM.                    "frm_ftp_command

0 0
原创粉丝点击