*从应用服务器目录获取文件,并保存到本地文件 。
CONSTANTS: LC_MAX_TRANSFER_LINES TYPE I VALUE 10000.
CONSTANTS: LC_FILEFORMAT_ASCII LIKE RLGRAP-FILETYPE
VALUE 'ASC'.
CONSTANTS: LC_FILEFORMAT_BINARY LIKE RLGRAP-FILETYPE
VALUE 'BIN'.
************************************************************************
* internal tables
************************************************************************
************************************************************************
* fields / variables
************************************************************************
DATA LG_MAX_LEN TYPE I VALUE 2550.
DATA:I_FILE_OVERWRITE TYPE ESP1_BOOLEAN .
DATA:I_FILE_APPL TYPE RCGFILETR-FTAPPL .
DATA:E_FLG_OPEN_ERROR TYPE ESP1_BOOLEAN.
DATA:E_OS_MESSAGE TYPE C .
*下载文件到本地。
* Local data ----------------------------------------------------------
DATA: L_FILELENGTH TYPE I.
DATA: L_DATA_TAB(256) TYPE C OCCURS 100000 WITH HEADER LINE.
DATA: L_FILENAME TYPE STRING.
DATA: L_AUTH_FILENAME LIKE AUTHB-FILENAME.
DATA: L_RETURN TYPE C.
DATA: L_FILE_EMPTY TYPE BOOLEAN.
DATA: L_MODE TYPE C.
DATA: L_LINES TYPE I.
DATA:LT_FILE_TABLE TYPE FILETABLE,
LS_FILE_TABLE TYPE FILE_TABLE.
DATA:LV_RC TYPE INT4 .
DATA:
LV_FILENAME TYPE STRING,
LV_PATH TYPE STRING,
LV_FULLPATH TYPE STRING.
*选择打开的文件。
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG
EXPORTING
* WINDOW_TITLE =
* DEFAULT_EXTENSION =
* DEFAULT_FILE_NAME =
* WITH_ENCODING =
* FILE_FILTER =
* INITIAL_DIRECTORY =
PROMPT_ON_OVERWRITE = 'X'
CHANGING
FILENAME = LV_FILENAME
PATH = LV_PATH
FULLPATH = LV_FULLPATH
* USER_ACTION =
* FILE_ENCODING =
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
NOT_SUPPORTED_BY_GUI = 3
INVALID_DEFAULT_FILE_NAME = 4
OTHERS = 5.
IF SY-SUBRC <> 0.
RETURN .
ENDIF.
* Function body -------------------------------------------------------
* init
E_FLG_OPEN_ERROR = SPACE.
CLEAR E_OS_MESSAGE.
L_FILENAME = LV_FULLPATH.
*应用服务器目录+文件名称
CONCATENATE GS_CONFIG_TAB-FILEP CNS_FILENAME INTO I_FILE_APPL .
* check the authority to read the file from the application server
L_AUTH_FILENAME = I_FILE_APPL.
CALL FUNCTION 'AUTHORITY_CHECK_DATASET'
EXPORTING
* PROGRAM =
ACTIVITY = SABC_ACT_READ
FILENAME = L_AUTH_FILENAME
EXCEPTIONS
NO_AUTHORITY = 1
ACTIVITY_UNKNOWN = 2
OTHERS = 3.
IF NOT SY-SUBRC IS INITIAL.
CASE SY-SUBRC.
WHEN 1.
* no auhtority
RAISE AP_NO_AUTHORITY.
WHEN OTHERS.
RAISE AP_FILE_OPEN_ERROR.
ENDCASE.
ENDIF.
I_FILE_OVERWRITE = 'X'.
* check if the file on the front-end exists
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_EXIST
EXPORTING
FILE = L_FILENAME
RECEIVING
RESULT = L_RETURN
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
WRONG_PARAMETER = 3
NOT_SUPPORTED_BY_GUI = 4
OTHERS = 5.
IF NOT SY-SUBRC IS INITIAL.
* RAISE FE_FILE_OPEN_ERROR.
ELSE.
* if file exists continue only if parameter is specified
IF L_RETURN = 'X' AND
I_FILE_OVERWRITE = SPACE.
RAISE FE_FILE_EXISTS.
ENDIF.
ENDIF. " not sy-subrc is initial.
* Begin Correction 24.09.2010 1505368 ********************
* validate physical filename against logical filename
* CALL FUNCTION 'FILE_VALIDATE_NAME'
* EXPORTING
* logical_filename = LC_LOGICAL_FILENAME_FTAPPL_2
* CHANGING
* physical_filename = I_FILE_APPL
* EXCEPTIONS
* OTHERS = 1.
*
* IF sy-subrc <> 0.
* e_flg_open_error = true.
* MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
* WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
* EXIT.
* ENDIF.
* End Correction 24.09.2010 1505368 ********************
* open the file on the application server
OPEN DATASET I_FILE_APPL FOR INPUT MESSAGE E_OS_MESSAGE
IN TEXT MODE ENCODING DEFAULT WITH WINDOWS LINEFEED .“根据应用服务器系统确定换行符
IF NOT SY-SUBRC IS INITIAL.
E_FLG_OPEN_ERROR = 'X'.
EXIT.
ENDIF. " not sy-subrc is initial
* write packages to the front-end
L_FILE_EMPTY = SPACE.
L_MODE = SPACE. " at first we write into the file
WHILE L_FILE_EMPTY = SPACE.
* read all lines from application server file
CLEAR L_DATA_TAB. REFRESH L_DATA_TAB.
L_LINES = 0.
WHILE L_FILE_EMPTY = SPACE .
READ DATASET I_FILE_APPL INTO L_DATA_TAB.
IF NOT SY-SUBRC IS INITIAL.
L_FILE_EMPTY = 'X'.
ELSE.
L_LINES = L_LINES + 1.
APPEND L_DATA_TAB.
ENDIF.
ENDWHILE. " l_file_empty = SPACE and
* check if data table is empty
READ TABLE L_DATA_TAB INDEX 1.
IF SY-SUBRC IS INITIAL.
CALL FUNCTION 'C13Z_DOWNLOAD'
EXPORTING
* BIN_FILESIZE = ' '
CODEPAGE = 'UTF-8'
FILENAME = L_FILENAME
FILETYPE = LC_FILEFORMAT_ASCII
MODE = L_MODE
* WK1_N_FORMAT = ' '
* WK1_N_SIZE = ' '
* WK1_T_FORMAT = ' '
* WK1_T_SIZE = ' '
* COL_SELECT = ' '
* COL_SELECTMASK = ' '
IMPORTING
FILELENGTH = L_FILELENGTH
TABLES
DATA_TAB = L_DATA_TAB
* FIELDNAMES =
EXCEPTIONS
FILE_OPEN_ERROR = 1
FILE_WRITE_ERROR = 2
INVALID_FILESIZE = 3
INVALID_TABLE_WIDTH = 4
INVALID_TYPE = 5
NO_BATCH = 6
UNKNOWN_ERROR = 7
OTHERS = 8.
IF NOT SY-SUBRC IS INITIAL.
CASE SY-SUBRC.
WHEN 2 .
CLOSE DATASET I_FILE_APPL.
RAISE FE_FILE_OPEN_ERROR.
WHEN OTHERS.
CLOSE DATASET I_FILE_APPL.
RAISE FE_FILE_WRITE_ERROR.
ENDCASE.
ENDIF. " not sy-subrc is initial
L_MODE = 'A'. " next lines we append
ELSE.
IF L_MODE IS INITIAL. " the first loop
* file on application server has no contents
CLOSE DATASET I_FILE_APPL.
RAISE AP_FILE_EMPTY.
ENDIF.
ENDIF. " sy-subrc is initial
ENDWHILE. " l_file_empty = SPACE.
* close the dataset
CLOSE DATASET I_FILE_APPL.