ABAP FTP链接文件服务器

2023-10-13 18:35:28 浏览数 (2)

代码语言:javascript复制
*&---------------------------------------------------------------------*
*& Report YBO_FTP_TEST
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT YBO_FTP_TEST.


CONSTANTS: C_KEY TYPE I VALUE 26101957."密钥
CONSTANTS: C_PWD(10) VALUE 'admin'.
CONSTANTS: C_USER(22) VALUE 'admin'.
CONSTANTS: C_HOST(22) VALUE '192.168.88.88'.
**SAPFTPA:表示以SAP服务器为目的地,上传与下载都会放在SAP服务器上;SAPFTP:以Client端为目的地
CONSTANTS: C_RFCDEST LIKE RFCDES-RFCDEST VALUE 'SAPFTP'.
DATA:LV_PWD(40).
DATA:LV_COMMAND(99),
     LV_LEN         TYPE I,
     LV_HDL         TYPE I.
DATA: BEGIN OF LT_RESULT OCCURS 0,
        LINE(100) TYPE C,
      END OF LT_RESULT.

DATA:LV_FILENAME TYPE CHAR128.
DATA OREF TYPE REF TO CX_ROOT.

DATA: BEGIN OF LT_DATA_TXT OCCURS 0 ,
        LINE(500),
      END OF LT_DATA_TXT.

DATA: BEGIN OF LT_DATA_BINARY OCCURS 0,
        X(2000) TYPE X,
      END OF LT_DATA_BINARY.
DATA: LV_BINARY_LEN TYPE I.
DATA:  L_CODEPAGE(4) TYPE N .
DATA:  L_ENCODING(20).

GET TIME.
* temp file name 用户名 日期 时间 传入的文件名
CONCATENATE SY-DATUM SY-UZEIT INTO LV_FILENAME.

* 将密码转化为SAP的格式
LV_LEN = STRLEN( C_PWD ).
CALL FUNCTION 'HTTP_SCRAMBLE'
  EXPORTING
    SOURCE      = C_PWD
    SOURCELEN   = LV_LEN
    KEY         = C_KEY
  IMPORTING
    DESTINATION = LV_PWD. "加密密码


DATA: LS_FTPSERVER TYPE SAPFTP_SERVERS.
SELECT SINGLE * FROM SAPFTP_SERVERS  INTO LS_FTPSERVER WHERE FTP_SERVER_NAME = C_HOST AND FTP_SERVER_PORT = 21.
IF SY-SUBRC <> 0.
  LS_FTPSERVER-FTP_SERVER_NAME = C_HOST.
  LS_FTPSERVER-FTP_SERVER_PORT = 21.
  LS_FTPSERVER-DESCRIPTION = 'FTP SERVER'.
  INSERT INTO SAPFTP_SERVERS VALUES LS_FTPSERVER .
  COMMIT WORK AND WAIT.
ENDIF.

TRY .
    CALL FUNCTION 'FTP_CONNECT'
      EXPORTING
        USER            = C_USER
        PASSWORD        = LV_PWD
        HOST            = C_HOST
        RFC_DESTINATION = C_RFCDEST
      IMPORTING
        HANDLE          = LV_HDL.
  CATCH CX_ROOT INTO OREF.
    MESSAGE OREF->GET_TEXT( ) TYPE 'S' DISPLAY LIKE 'E'.
    RETURN.
ENDTRY.

** 进入指定的FTP服务器目录
LV_COMMAND = 'cd /sap'.

TRY .
    CALL FUNCTION 'FTP_COMMAND'
      EXPORTING
        HANDLE  = LV_HDL
        COMMAND = LV_COMMAND
      TABLES
        DATA    = LT_RESULT.
    LOOP AT LT_RESULT.
      WRITE:/ LT_RESULT.
    ENDLOOP.
  CATCH CX_ROOT INTO OREF.
    MESSAGE OREF->GET_TEXT( ) TYPE 'S' DISPLAY LIKE 'E'.
    PERFORM FRM_DISCONNECT.
    RETURN.
ENDTRY.

APPEND 'ceshi' TO LT_DATA_TXT.

CALL FUNCTION 'SCP_CODEPAGE_BY_EXTERNAL_NAME'
  EXPORTING
    EXTERNAL_NAME = 'GB2312'
  IMPORTING
    SAP_CODEPAGE  = L_CODEPAGE.
L_ENCODING = L_CODEPAGE.

TRY.
**将文本内表转换为某种编码格式的二进制码流内表
    CALL FUNCTION 'SCMS_TEXT_TO_BINARY'
      EXPORTING
        ENCODING      = L_ENCODING
      IMPORTING
        OUTPUT_LENGTH = LV_BINARY_LEN "编码后占多少字节
      TABLES
        TEXT_TAB      = LT_DATA_TXT
        BINARY_TAB    = LT_DATA_BINARY.
  CATCH CX_ROOT INTO OREF.
    MESSAGE OREF->GET_TEXT( ) TYPE 'S' DISPLAY LIKE 'E'.
    RETURN.
ENDTRY.


TRY .
    CALL FUNCTION 'FTP_R3_TO_SERVER'
      EXPORTING
        HANDLE      = LV_HDL
        FNAME       = LV_FILENAME
        BLOB_LENGTH = LV_BINARY_LEN
      TABLES
        BLOB        = LT_DATA_BINARY. "二进制输出
  CATCH CX_ROOT INTO OREF.
    MESSAGE OREF->GET_TEXT( ) TYPE 'S' DISPLAY LIKE 'E'.
    PERFORM FRM_DISCONNECT.
    RETURN.
ENDTRY.

FORM FRM_DISCONNECT.
* disconnect
* 关闭SAP与其他系统的连接
  CALL FUNCTION 'FTP_DISCONNECT'
    EXPORTING
      HANDLE = LV_HDL.

*关闭SAP与其他系统的RFC连接.
  CALL FUNCTION 'RFC_CONNECTION_CLOSE'
    EXPORTING
      DESTINATION = C_RFCDEST
    EXCEPTIONS
      OTHERS      = 1.
ENDFORM.

0 人点赞