Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

ftp to a server

Former Member
0 Likes
875

Hi experts,

I need to ftp a file from the local drive to one of the servers on the landscape. I have the following information:

1. hostname,

2. IP

3. username

4. password

Can u please advise on the code to use the above paramaters and ftp the file.

Thanks in advance.

Regards,

fs

1 ACCEPTED SOLUTION
Read only

andreas_mann3
Active Contributor
0 Likes
838

or try sample-report RSFTP004

A.

4 REPLIES 4
Read only

Former Member
0 Likes
838

Hi,

Here is the sample code


REPORT ZFTPSAP LINE-SIZE 132.

DATA: BEGIN OF MTAB_DATA OCCURS 0,
LINE(132) TYPE C,
END OF MTAB_DATA.

DATA: MC_PASSWORD(20) TYPE C,
MI_KEY TYPE I VALUE 26101957,
MI_PWD_LEN TYPE I,
MI_HANDLE TYPE I.

START-OF-SELECTION.

*-- Your SAP-UNIX FTP password (case sensitive)
MC_PASSWORD = 'password'.

DESCRIBE FIELD MC_PASSWORD LENGTH MI_PWD_LEN.

*-- FTP_CONNECT requires an encrypted password to work
CALL 'AB_RFC_X_SCRAMBLE_STRING'
     ID 'SOURCE' FIELD MC_PASSWORD ID 'KEY' FIELD MI_KEY
     ID 'SCR' FIELD 'X' ID 'DESTINATION' FIELD MC_PASSWORD
     ID 'DSTLEN' FIELD MI_PWD_LEN.

CALL FUNCTION 'FTP_CONNECT'
     EXPORTING
*-- Your SAP-UNIX FTP user name (case sensitive)
       USER            = 'userid'
       PASSWORD        = MC_PASSWORD
*-- Your SAP-UNIX server host name (case sensitive)
       HOST            = 'unix-host'
       RFC_DESTINATION = 'SAPFTP'
     IMPORTING
       HANDLE          = MI_HANDLE
     EXCEPTIONS
       NOT_CONNECTED   = 1
       OTHERS          = 2.

CHECK SY-SUBRC = 0.

CALL FUNCTION 'FTP_COMMAND'
     EXPORTING
       HANDLE = MI_HANDLE
       COMMAND = 'dir'
     TABLES
       DATA = MTAB_DATA
     EXCEPTIONS
       TCPIP_ERROR = 1

       COMMAND_ERROR = 2
       DATA_ERROR = 3
       OTHERS = 4.

IF SY-SUBRC = 0.
  LOOP AT MTAB_DATA.
    WRITE: / MTAB_DATA.
  ENDLOOP.
ELSE.
* do some error checking.
  WRITE: / 'Error in FTP Command'.
ENDIF.

CALL FUNCTION 'FTP_DISCONNECT'
     EXPORTING
       HANDLE = MI_HANDLE
     EXCEPTIONS
       OTHERS = 1.   

Regards

Sudheer

Read only

Former Member
0 Likes
838

Hi,

This program is useful if you want to move the data from internal table to a file on to the external server.

In other words this program will create a file on the external server with the data which is available on the internal table.

NOTE:

Replace 'XXX.XXX.XX.XXX' with your host name.

Check the below code.

tables: t777a. "Building Addresses

  • Internal Table for Building table.

data: begin of it_t777a occurs 0,

build like t777a-build, "Building

stext like t777a-stext, "Object Name

cname like t777a-cname, "Address Supplement (c/o)

ort01 like t777a-ort01, "City

pstlz like t777a-pstlz, "Postal Code

regio like t777a-regio, "Region (State, Province, County)

end of it_t777a.

  • Internal Table for taking all fields of the above table in one line

  • separated by ‘|’(pipe).

data: begin of it_text occurs 0,

text(131),

end of it_text.

Constants: c_key type i value 26101957,

c_dest type rfcdes-rfcdest value 'SAPFTPA'.

data: g_dhdl type i, "Handle

g_dlen type i, "pass word length

g_dpwd(30). "For storing password

    • Selection Screen Starts

SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE TEXT-001.

parameters: p_user(30) default 't777a' obligatory,

p_pwd(30) default 't777a' obligatory,

p_host(64) default 'XXX.XXX.XX.XXX' obligatory.

SELECTION-SCREEN END OF BLOCK blk1.

SELECTION-SCREEN BEGIN OF BLOCK blk2 WITH FRAME TITLE TEXT-002.

parameters: p_file like rlgrap-filename default 't777a_feed.txt'.

SELECTION-SCREEN END OF BLOCK blk2.

  • Password not visible.

at Selection-screen output.

loop at screen.

if screen-name = 'P_PWD'.

screen-invisible = '1'.

modify screen.

endif.

endloop.

g_dpwd = p_pwd.

  • Start of selection

start-of-selection.

  • To fetch the data records from the table T777A.

select build stext cname ort01 pstlz regio

from t777a

into table it_t777a.

  • Sort the internal table by build.

if not it_t777a[] is initial.

sort it_t777a by build.

endif.

  • Concatenate all the fields of above internal table records in one line

  • separated by ‘|’(pipe).

loop at it_t777a.

concatenate it_t777a-build it_t777a-stext it_t777a-cname

it_t777a-ort01 it_t777a-pstlz it_t777a-regio

into it_text-text separated by '|'.

append it_text.

clear it_text.

endloop.

    • To get the length of the password.

g_dlen = strlen( g_dpwd ).

    • Below Function module is used to Encrypt the Password.

CALL FUNCTION 'HTTP_SCRAMBLE'

EXPORTING

SOURCE = g_dpwd "Actual password

SOURCELEN = g_dlen

KEY = c_key

IMPORTING

DESTINATION = g_dpwd. "Encyrpted Password

*Connects to the FTP Server as specified by user.

Call function 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

text = 'Connecting to FTP Server'.

    • Below function module is used to connect the FTP Server.

    • It Accepts only Encrypted Passwords.

    • This Function module will provide a handle to perform different

    • operations on the FTP Server via FTP Commands.

call function 'FTP_CONNECT'

EXPORTING

user = p_user

password = g_dpwd

host = p_host

rfc_destination = c_dest

IMPORTING

handle = g_dhdl

EXCEPTIONS

NOT_CONNECTED.

if sy-subrc ne 0.

format color col_negative.

write:/ 'Error in Connection'.

else.

write:/ 'FTP Connection is opened '.

endif.

**Transferring the data from internal table to FTP Server.

CALL FUNCTION 'FTP_R3_TO_SERVER'

EXPORTING

HANDLE = g_dhdl

FNAME = p_file

CHARACTER_MODE = 'X'

TABLES

TEXT = it_text

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.

ELSE.

write:/ 'File has created on FTP Server'.

ENDIF.

Call function 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

text = 'File has created on FTP Server'.

    • To Disconnect the FTP Server.

CALL FUNCTION 'FTP_DISCONNECT'

EXPORTING

HANDLE = g_dhdl.

    • To Disconnect the Destination.

CALL FUNCTION 'RFC_CONNECTION_CLOSE'

EXPORTING

destination = c_dest

EXCEPTIONS

others = 1.

Read only

andreas_mann3
Active Contributor
0 Likes
839

or try sample-report RSFTP004

A.

Read only

Former Member
0 Likes
838

Thanks for ur response. The code worked great