设为首页 收藏本站
查看: 1354|回复: 0

[经验分享] sap abap 程序 中使用 FTP . <转载>

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-9-20 14:43:42 | 显示全部楼层 |阅读模式
  原博 http://blog.iyunv.com/sunfeng8848/article/details/3500305
  report  z04ftp2.
data: begin of ig_ftp_result occurs 0,
        line(100),
      end of ig_ftp_result.
data: l_path(128)       type c.  "文件路径,必须以/结尾
data: l_filename(128)   type c value 'CH210276140222_likp.txt'.  "文件名
data: l_ftpcommand(100) type c.  "FTP命令
data: l_handle          type i.                 "HAND
"源路径,必须以/结尾
data: cons_source(128) type c  value '/usr/sap/AA2/D01/data/'.
data:  cons_dens(128) type c .                      "目标路径
constants cons_key type i  value 26101957.
data  pr_return(1) type c value '0'.
*// INITIALIZATION
initialization.
*服务器上下载文件的路径
"  CONCATENATE '/usr/sap/AA2/D01/data/' INTO cons_source.
*// START OF SELECTION
start-of-selection.
  perform frm_ftp_file.

form frm_ftp_file .
  data:
    l_dstlen          type i,                 "DESTINATION LEN
    l_pw(64)          type c.                 "密码长度一定要够否则出错
* 连接FTP服务器
  l_pw = 'sap888'.
*-- FTP_CONNECT requires an encrypted password to work
*   CREATE THE NEW PW BASE ON LOGIN FTP PASS WORD.
  call function 'HTTP_SCRAMBLE'
  exporting
    source      = l_pw
    sourcelen   = 6
    key         = cons_key
  importing
    destination = l_pw.
  do 3 times." 连接三次,以为一次可能会不成功
*   OPEN THE FTP SERVER.
    call function 'FTP_CONNECT'
      exporting
        user            = 'sap01'   "USER
        password        = l_pw             "PASS WORD
        host            = '9.186.155.115 9980'
        rfc_destination = 'SAPFTPA'        "DEFAULT
      importing
        handle          = l_handle
      exceptions
        not_connected   = 1
        others          = 2.
    if sy-subrc = 0.
      exit.
    endif.
  enddo.
  if sy-subrc <> 0.
    write :/ sy-datum, sy-uzeit, sy-uname,  'CONNECT FTP FAILED!'.            "MESSAGEG
    stop.
  endif.

* Change local directory
  clear l_ftpcommand.
  concatenate 'lcd' cons_source into l_ftpcommand separated by space.
  perform frm_ftp_command using l_ftpcommand pr_return.
   if pr_return = '1'.
     write:/ sy-datum, sy-uzeit, sy-uname,  'FTP改变本地路径错误!'.
     stop.
   endif.
* Change ftp directory
  if cons_dens <> ''.
    clear l_ftpcommand.
    concatenate 'cd' cons_dens into l_ftpcommand separated by space.
    perform frm_ftp_command using l_ftpcommand pr_return.
  endif.
* Change TRANSFER MODE
  clear l_ftpcommand.
*  l_ftpcommand = 'binary'.
  l_ftpcommand = 'ascii'.
  perform frm_ftp_command using l_ftpcommand pr_return.
  if pr_return = '1'.
    write:/ sy-datum, sy-uzeit, sy-uname,  '改变FTP传输模式出现错误!'.
    stop.
  endif.
* Put File into FTP SERVER
  clear l_ftpcommand.
  concatenate 'put'  l_filename into l_ftpcommand separated by space.
  perform frm_ftp_command using l_ftpcommand pr_return.
  if pr_return = '1'.
    write:/ sy-datum, sy-uzeit, sy-uname,  '文件传输中出现错误!'.
    stop.
  endif.
*  断开FTP服务器
  call function 'FTP_DISCONNECT'
    exporting
      handle = l_handle.
  write:/ sy-datum, sy-uzeit, sy-uname,  '文件传输成功!'.
endform.                    " FRM_FTP_FILE
************************************************************************
*& FORM FRM_FTP_COMMAND                                                *
************************************************************************
*& FTP Command                                                         *
************************************************************************
form frm_ftp_command using pr_command pr_ret.
  call function 'FTP_COMMAND'
    exporting
      handle                = l_handle
      command               = pr_command
*     COMPRESS              =
*     RFC_DESTINATION       =
*     VERIFY                =
*   IMPORTING
*     FILESIZE              =
*     FILEDATE              =
*     FILETIME              =
    tables
      data                  = ig_ftp_result
   exceptions
     tcpip_error           = 1
     command_error         = 2
     data_error            = 3
     others                = 4
            .
* Disconnect
  if sy-subrc <> 0." 调用中出错立即断开连接
    pr_ret = '1'.
    call function 'FTP_DISCONNECT'
      exporting
        handle = l_handle.
    exit.
  endif.
endform.                    "FRM_FTP_COMMAND

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-116344-1-1.html 上篇帖子: sap登录组 下篇帖子: SAP RFC user 最小权限
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表