其他分享
首页 > 其他分享> > 服务器上传下载文件

服务器上传下载文件

作者:互联网

  1 *&---------------------------------------------------------------------*
  2 *& Report Z3426UPLOADFILE
  3 *&---------------------------------------------------------------------*
  4 *&
  5 *&---------------------------------------------------------------------*
  6 REPORT z3426uploadfile.
  7 DATA:BEGIN OF gt_file OCCURS 0,
  8        local_filename  TYPE string,
  9        remote_filename TYPE string,
 10        xcontent        TYPE xstring,
 11      END OF gt_file.
 12 
 13 PARAMETERS:p_path TYPE string DEFAULT 'C:\Users\XXXX\Desktop\Test'.
 14 
 15 AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
 16   PERFORM frm_file_search CHANGING p_path.
 17 
 18 START-OF-SELECTION.
 19   PERFORM frm_get_local_file.
 20   PERFORM frm_upload_file.
 21   PERFORM frm_get_remote_file.
 22   PERFORM frm_down_remote_file.
 23 
 24 FORM frm_file_search CHANGING p_path.
 25   CALL METHOD cl_gui_frontend_services=>directory_browse
 26     EXPORTING
 27       window_title    = '请选择文件打开目录'
 28       initial_folder  = 'C'
 29     CHANGING
 30       selected_folder = p_path.
 31 
 32   CALL METHOD cl_gui_cfw=>flush.
 33 ENDFORM.
 34 
 35 FORM frm_get_local_file.
 36   DATA:lv_filter   TYPE string,
 37        lv_count    TYPE i,
 38        lv_filename TYPE string,
 39        lt_file     TYPE line OCCURS 0 WITH HEADER LINE,
 40        lt_data     TYPE STANDARD TABLE OF raw255,
 41        lv_xcontent TYPE xstring.
 42   lv_filter = `*.*`.
 43   CALL METHOD cl_gui_frontend_services=>directory_list_files
 44     EXPORTING
 45       directory                   = p_path
 46       filter                      = lv_filter
 47       files_only                  = abap_true
 48     CHANGING
 49       file_table                  = lt_file[]
 50       count                       = lv_count
 51     EXCEPTIONS
 52       cntl_error                  = 1
 53       directory_list_files_failed = 2
 54       wrong_parameter             = 3
 55       error_no_gui                = 4
 56       not_supported_by_gui        = 5
 57       OTHERS                      = 6.
 58 
 59   LOOP AT lt_file INTO DATA(ls_file).
 60     CLEAR:lt_data,lv_xcontent.
 61     lv_filename = p_path && '\' && ls_file-line.
 62     CALL METHOD cl_gui_frontend_services=>gui_upload
 63       EXPORTING
 64         filename   = lv_filename
 65         filetype   = 'BIN'
 66       IMPORTING
 67         filelength = lv_count
 68       CHANGING
 69         data_tab   = lt_data.
 70 
 71     CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
 72       EXPORTING
 73         input_length = lv_count
 74       IMPORTING
 75         buffer       = lv_xcontent
 76       TABLES
 77         binary_tab   = lt_data.
 78     gt_file-local_filename = ls_file-line.
 79     gt_file-xcontent = lv_xcontent.
 80     APPEND gt_file.
 81     CLEAR:gt_file.
 82   ENDLOOP.
 83 
 84 ENDFORM.
 85 
 86 FORM frm_upload_file.
 87 
 88   DATA: BEGIN OF tab OCCURS 0,
 89           line(200),
 90         END OF tab.
 91   DATA:lv_str TYPE string.
 92 
 93   DATA(lv_dir) = `/tmp/3426/Testdir/` && sy-datum.
 94   DATA(command) = `mkdir -p ` && lv_dir.
 95   CALL 'SYSTEM' ID 'COMMAND' FIELD command
 96                 ID 'TAB'     FIELD tab-*sys*.
 97   LOOP AT gt_file.
 98     gt_file-remote_filename =  lv_dir && '/' && gt_file-local_filename.
 99     OPEN DATASET gt_file-remote_filename FOR OUTPUT IN BINARY MODE.
100     IF sy-subrc EQ 0.
101       TRANSFER gt_file-xcontent TO gt_file-remote_filename.
102       CLOSE DATASET gt_file-remote_filename.
103     ENDIF.
104     MODIFY gt_file.
105     CLEAR:gt_file.
106     "DELETE DATASET gt_file-remote_filename.
107   ENDLOOP.
108 ENDFORM.
109 
110 FORM frm_get_remote_file.
111   DATA:lv_xstr TYPE xstring,
112        gt_dir  TYPE epsfili OCCURS 0 WITH HEADER LINE,
113        gv_dir  TYPE epsf-epsdirnam.
114   gv_dir =  `/tmp/3426/Testdir/` && sy-datum.
115   CALL FUNCTION 'EPS_GET_DIRECTORY_LISTING'
116     EXPORTING
117       dir_name               = gv_dir
118     TABLES
119       dir_list               = gt_dir
120     EXCEPTIONS
121       invalid_eps_subdir     = 1
122       sapgparam_failed       = 2
123       build_directory_failed = 3
124       no_authorization       = 4
125       read_directory_failed  = 5
126       too_many_read_errors   = 6
127       empty_directory_list   = 7.
128   CLEAR:gt_file,gt_file[].
129   LOOP AT gt_dir.
130     gt_file-remote_filename = gv_dir && '/' && gt_dir-name.
131     OPEN DATASET gt_file-remote_filename FOR INPUT IN BINARY MODE.
132     READ DATASET gt_file-remote_filename INTO lv_xstr.
133     IF sy-subrc <> 0.
134       EXIT.
135     ELSE.
136       gt_file-xcontent = gt_file-xcontent && lv_xstr.
137     ENDIF.
138     CLOSE DATASET gt_file-remote_filename.
139     gt_file-local_filename = p_path && '\copy\' && gt_dir-name.
140     APPEND gt_file.
141     CLEAR:gt_file.
142   ENDLOOP.
143 ENDFORM.
144 
145 FORM frm_down_remote_file.
146   DATA:lv_count TYPE i,
147        lt_data  TYPE STANDARD TABLE OF raw255.
148 
149   LOOP AT gt_file.
150     CLEAR:lv_count,lt_data,lt_data[].
151     CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
152       EXPORTING
153         buffer        = gt_file-xcontent
154       IMPORTING
155         output_length = lv_count
156       TABLES
157         binary_tab    = lt_data.
158 
159     CALL METHOD cl_gui_frontend_services=>gui_download
160       EXPORTING
161         bin_filesize = lv_count
162         filename     = gt_file-local_filename
163         filetype     = 'BIN'
164       CHANGING
165         data_tab     = lt_data.
166   ENDLOOP.
167 ENDFORM.

 

标签:文件,gt,上传下载,filename,lv,file,服务器,TYPE,dir
来源: https://www.cnblogs.com/1187163927ch/p/16371276.html