如何将SAP数据传输到其他系统(Transferring Data from SAP to Other Systems)
在sap里有GUI_DOWNLOAD 函数将sap的数据下载到客户端机器(presentation server),而Dataset则是将数据传输到应用服务器(Application server)。然而在有些时候需要将数据传输到第三方其他系统(3rd Party System),这是我们就可以使用FTP命令来完成数据传输。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
1、相关函数 HTTP_SCRAMBLE FTP_CONNECT FTP_R3_TO_SERVER FTP_DISCONNECT RFC_CONNECTION_CLOSE 2、函数说明 HTTP_SCRAMBLE: 将密码转化为SAP的格式 样例代码 l_pwd = p_pwd. l_slen = STRLEN( l_pwd ). CALL FUNCTION 'HTTP_SCRAMBLE' exporting source = l_pwd sourcelen = l_slen key = c_key importing destination = l_pwd. FTP_CONNECT : 连接其他系统 * To Connect to the Server using FTP 样例代码 CALL FUNCTION 'FTP_CONNECT' EXPORTING user = p_user password = l_pwd host = p_host rfc_destination = c_dest IMPORTING handle = w_hdl 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. FTP_R3_TO_SERVER: 将SAP的内表数据按字符方式传输到其他系统. 样例代码 CALL FUNCTION 'FTP_R3_TO_SERVER' EXPORTING handle = w_hdl fname = <file path of destination system> character_mode = 'X' TABLES text = <internal table data> 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 RAISING invalid_output_file. ENDIF. FTP_DISCONNECT: 关闭SAP与其他系统的连接. 样例代码 * To disconnect the FTP CALL FUNCTION 'FTP_DISCONNECT' EXPORTING handle = w_hdl. RFC_CONNECTION_CLOSE:关闭SAP与其他系统的RFC连接. 样例代码 CALL FUNCTION 'RFC_CONNECTION_CLOSE' EXPORTING destination = c_dest EXCEPTIONS OTHERS = 1. 3、SAP的样例代码 report rsftp004. parameters: suser(30) type c lower case, spwd(30) type c lower case, shost(64) type c lower case, duser(30) type c lower case, dpwd(30) type c lower case, dhost(64) type c lower case, lines type i default 1000, pasv. selection-screen skip 1. parameters: dest like rfcdes-rfcdest default 'SAPFTP'. types: begin of text, line(120) type c, end of text. types: begin of blob, line(80) type x, end of blob. data: shdl type i, dhdl type i, key type i value 26101957, slen type i, bline(80) type x, sdocid like sysuuid-c, ddocid like sysuuid-c, blob_length type i, cmd(120), error. data: result type table of text with header line, bindata type table of blob with header line. * Create data set extended check off. error = 0. bline = '0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F' & '0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F' & '0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F' & '0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F'. do lines times. bindata-line = bline. append bindata. enddo. call function 'SYSTEM_UUID_C_CREATE' importing uuid = sdocid. call function 'SYSTEM_UUID_C_CREATE' importing uuid = ddocid. * connect to ftp server - source slen = strlen( spwd ). call function 'HTTP_SCRAMBLE' exporting source = spwd sourcelen = slen key = key importing destination = spwd. call function 'SAPGUI_PROGRESS_INDICATOR' exporting text = 'Connect to FTP Server - Source'. call function 'FTP_CONNECT' exporting user = suser password = spwd host = shost rfc_destination = dest importing handle = shdl. * connect to ftp server - destination slen = strlen( dpwd ). call function 'HTTP_SCRAMBLE' exporting source = dpwd sourcelen = slen key = key importing destination = dpwd. call function 'SAPGUI_PROGRESS_INDICATOR' exporting text = 'Connect to FTP Server - Destination'. call function 'FTP_CONNECT' exporting user = duser password = dpwd host = dhost rfc_destination = dest importing handle = dhdl. if not pasv is initial. refresh result. call function 'FTP_COMMAND' exporting handle = shdl command = 'set passive on' tables data = result exceptions tcpip_error = 1 command_error = 2 data_error = 3. if sy-subrc eq 0. write: / 'Set passive mode - Source'. endif. refresh result. call function 'FTP_COMMAND' exporting handle = dhdl command = 'set passive on' tables data = result exceptions tcpip_error = 1 command_error = 2 data_error = 3. if sy-subrc eq 0. write: / 'Set passive mode - Destination'. endif. skip 1. endif. * Create file on Source blob_length = lines * 80. call function 'SAPGUI_PROGRESS_INDICATOR' exporting text = 'Create File on Source'. call function 'FTP_R3_TO_SERVER' exporting handle = shdl fname = sdocid blob_length = blob_length tables blob = bindata. * Copy Files call function 'SAPGUI_PROGRESS_INDICATOR' exporting text = 'Copy File to Destination'. refresh result. call function 'FTP_COPY' exporting handle_source = shdl handle_destination = dhdl file_source = sdocid file_destination = ddocid tables data = result exceptions tcpip_error = 1 command_error = 2 data_error = 3 others = 4. if sy-subrc ne 0. error = 1. endif. loop at result. write / result-line. endloop. * compare content if error eq 0. call function 'SAPGUI_PROGRESS_INDICATOR' exporting text = 'Compare Content'. skip 1. refresh bindata. call function 'FTP_SERVER_TO_R3' exporting handle = shdl fname = sdocid importing blob_length = blob_length tables blob = bindata. slen = lines * 80. if slen ne blob_length. error = 1. write: / 'Length error - expected',slen,'received',blob_length. else. loop at bindata. if bindata-line ne bline. slen = sy-tabix * 80. write: / 'Content error at',slen,bindata-line. error = 1. exit. endif. endloop. endif. refresh bindata. call function 'FTP_SERVER_TO_R3' exporting handle = dhdl fname = ddocid importing blob_length = blob_length tables blob = bindata. slen = lines * 80. if slen ne blob_length. error = 1. write: / 'Length error - expected',slen,'received',blob_length. else. loop at bindata. if bindata-line ne bline. slen = sy-tabix * 80. write: / 'Content error at',slen,bindata-line. error = 1. exit. endif. endloop. endif. if error eq 0. write: / 'Content compare OK'. else. write: / 'Content compare error'. endif. skip 1. endif. * Delete concatenate 'del' sdocid into cmd separated by ' '. refresh result. call function 'SAPGUI_PROGRESS_INDICATOR' exporting text = 'Delete Files'. call function 'FTP_COMMAND' exporting handle = shdl command = cmd tables data = result exceptions tcpip_error = 1 command_error = 2 data_error = 3. loop at result. write / result-line. endloop. concatenate 'del' ddocid into cmd separated by ' '. refresh result. call function 'FTP_COMMAND' exporting handle = dhdl command = cmd tables data = result exceptions tcpip_error = 1 command_error = 2 data_error = 3. loop at result. write / result-line. endloop. * Disconnect call function 'FTP_DISCONNECT' exporting handle = shdl. call function 'FTP_DISCONNECT' exporting handle = dhdl. call function 'RFC_CONNECTION_CLOSE' exporting destination = dest exceptions others = 1. if error ne 0. format color col_negative. write: / 'Error im Test'. else. format color col_positive. write: / ' Test OK'. endif. * password not visible at selection-screen output. loop at screen. if screen-name = 'SPWD' or screen-name = 'DPWD'. screen-invisible = '1'. modify screen. endif. endloop. |
本文作者: GavinDong
版权属于: GavinDong博客
文章链接: https://gavindong.com/1123.html
如果使用过程中遇到问题,可 **点击此处** 交流沟通。
版权所有,转载时必须以链接形式注明作者和原始出处及本声明。