ABAP调用HTTP接口上传文件
一、Java上传文件的的接口
Controller
package org.jeecg.modules.ncip.controller; import javax.servlet.http.HttpServletRequest; import org.jeecg.common.api.vo.Result; import org.jeecg.modules.ncip.service.FileService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; /** * @author Amell * 2023年6月2日 下午4:10:27 * */ @RestController @RequestMapping("/ncip/file") public class FileController { @Autowired private FileService fileSerive; @RequestMapping(value = "/upload", method = RequestMethod.POST) public Result<?> uploadFile(@RequestParam MultipartFile file,HttpServletRequest req) { Result<?> result = fileSerive.uploadFile(file); return result; } }
Service
package org.jeecg.modules.ncip.service; import org.jeecg.common.api.vo.Result; import org.springframework.web.multipart.MultipartFile; /** * @author Amell * 2023年6月2日 下午5:28:58 * */ public interface FileService { Result<?> uploadFile(MultipartFile file); }
ServiceImpl
package org.jeecg.modules.ncip.service.impl; import java.io.File; import java.io.IOException; import java.net.URLDecoder; import java.text.SimpleDateFormat; import java.util.Date; import org.jeecg.common.api.vo.Result; import org.jeecg.modules.ncip.service.FileService; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; /** * @author Amell * 2023年6月2日 下午5:29:22 * */ @Service public class FileServiceImpl implements FileService{ @Override public Result<?> uploadFile(MultipartFile file) { Result<?> result= new Result<>(); //绝对路径 String realPath = "//192.168.194.12/File/"; SimpleDateFormat sdf=new SimpleDateFormat("YYYYMM"); String format=sdf.format(new Date()); //文件存放的目录(在绝对路径下建立年月名称的文件夹) File folder = new File(realPath+format); if(!folder.isDirectory()) { folder.mkdir(); } try { //文件名 String fileName = file.getOriginalFilename(); //文件后缀 //String suffix = fileName.substring(fileName.lastIndexOf("."),fileName.length()); //utf-8解码 fileName = URLDecoder.decode(fileName,"UTF-8"); //文件存在则先删除后再建立 File targetFile = new File(folder,fileName); if(!targetFile.exists()) { targetFile.mkdirs(); }else { targetFile.delete(); } file.transferTo(targetFile); result.setSuccess(true); } catch (IOException e) { e.printStackTrace(); result.error500("上传文件失败"); } return result; } }
二、Postman测试
http的headers添加token验证
http的body选择form-data,在key里点击下拉按钮,选择File
填写key名称(这里要跟Java接口的参数名一样),并点击select files选择文件
选择文件后点击send,看下接口返回的结果
点击code查看http代码,接下来就是要用http的代码来写abap
三、ABAP调用http接口上传文件
效果:
代码:
************************************************************************ * 程 序 名: * 程序描述:ABAP调用http接口上传文件 * 事务代码: ************************************************************************ * 修改日志 ************************************************************************ * 日期 版本 修改人 描述 * -------- ---- ------------ ------------------------------------------- * 20230607 1.0 Amell 创建程序 * ************************************************************************ REPORT zhttp_test MESSAGE-ID 00. ************************************************************************ * Tables Definitions ************************************************************************ TABLES: rlgrap. ************************************************************************ * Data Definitions 定义数据 ************************************************************************ TYPES: BEGIN OF ty_file, line(1024) TYPE x, END OF ty_file. DATA: gt_file TYPE TABLE OF ty_file. DATA: gv_file_name TYPE sdbah-actid, gv_file_type TYPE sdbad-funct, gv_file TYPE xstring. ************************************************************************ * Includes Module 包含模块 ************************************************************************ ************************************************************************ * Selection Screen 选择屏幕 ************************************************************************ PARAMETERS: p_file LIKE rlgrap-filename OBLIGATORY. ************************************************************************ * Initialization 初始化事件 ************************************************************************ INITIALIZATION. ************************************************************************ * At Selection Screen PAI事件 ************************************************************************ AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. PERFORM frm_f4_file. ************************************************************************ * At Selection Screen Output PBO事件 ************************************************************************ AT SELECTION-SCREEN OUTPUT. ************************************************************************ * Report Format 报表格式 ************************************************************************ TOP-OF-PAGE. END-OF-PAGE. ************************************************************************ * Main Process 主要逻辑 ************************************************************************ START-OF-SELECTION. "读取上传文件 PERFORM frm_read_upload_file. "调用http接口上传文件 PERFORM frm_call_http_to_upload_file. END-OF-SELECTION. *&---------------------------------------------------------------------* *& Form FRM_F4_FILE *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM frm_f4_file . CALL FUNCTION 'F4_FILENAME' IMPORTING file_name = p_file. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_READ_UPLOAD_FILE *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM frm_read_upload_file . DATA: lv_file_path TYPE string, lv_file_length TYPE i, lv_file_name TYPE dbmsgora-filename. lv_file_path = p_file. lv_file_name = p_file. CALL FUNCTION 'SPLIT_FILENAME' EXPORTING long_filename = lv_file_name "上传文件路径 IMPORTING pure_filename = gv_file_name "文件名称 pure_extension = gv_file_type. "文件后缀 CALL FUNCTION 'GUI_UPLOAD' EXPORTING filename = lv_file_path filetype = 'BIN' IMPORTING filelength = lv_file_length TABLES data_tab = gt_file EXCEPTIONS file_open_error = 1 file_read_error = 2 no_batch = 3 gui_refuse_filetransfer = 4 invalid_type = 5 no_authority = 6 unknown_error = 7 bad_data_format = 8 header_not_allowed = 9 separator_not_allowed = 10 header_too_long = 11 unknown_dp_error = 12 access_denied = 13 dp_out_of_memory = 14 disk_full = 15 dp_timeout = 16 OTHERS = 17. IF sy-subrc <> 0. MESSAGE s001 WITH '上传文件失败' DISPLAY LIKE 'E'. LEAVE LIST-PROCESSING. ENDIF. "转xstring CALL FUNCTION 'SCMS_BINARY_TO_XSTRING' EXPORTING input_length = lv_file_length IMPORTING buffer = gv_file TABLES binary_tab = gt_file EXCEPTIONS failed = 1 OTHERS = 2. IF sy-subrc <> 0. MESSAGE s001 WITH '转xstring失败' DISPLAY LIKE 'E'. LEAVE LIST-PROCESSING. ENDIF. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_CALL_HTTP_TO_UPLOAD_FILE *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM frm_call_http_to_upload_file . DATA: lo_http_client TYPE REF TO if_http_client, "http客户端 lo_http_entity TYPE REF TO if_http_entity, "http实体 lt_http_header TYPE tihttpnvp, "http头部信息 ls_http_header TYPE ihttpnvp, lv_url TYPE string, "http请求 lv_len TYPE i, "请求数据的长度 lv_response TYPE string, "http响应返回信息 lv_code TYPE i. "http状态码 DATA: lv_error_code TYPE sysubrc, lv_error_msg TYPE string. DATA: lv_file_name TYPE savwctxt-fieldcont, lv_name_encode TYPE savwctxt-fieldcont. DATA: lv_content_disposition TYPE string, lv_content_type TYPE string. lv_url = 'http://192.168.194.12:8080/jeecg-boot/ncip/file/upload'. "创建http客户端请求 CALL METHOD cl_http_client=>create_by_url EXPORTING url = lv_url IMPORTING client = lo_http_client EXCEPTIONS argument_not_found = 1 plugin_not_active = 2 internal_error = 3 OTHERS = 4. "设置http为post请求 lo_http_client->request->set_method( 'POST' ). ls_http_header-name = 'X-Access-Token'. ls_http_header-value = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2ODYxODM1OTcsInVzZXJuYW1lIjoiYWRtaW4ifQ.fpxtvBsFDVR5WmjP3iLXhD-K7ZiULofqwQ1S_DE9Hxk'. APPEND ls_http_header TO lt_http_header. ls_http_header-name = 'Content-Type'. ls_http_header-value = 'multipart/form-data'. APPEND ls_http_header TO lt_http_header. "设置http header CALL METHOD lo_http_client->request->set_header_fields EXPORTING fields = lt_http_header. * CALL METHOD lo_http_client->request->if_http_entity~set_formfield_encoding * EXPORTING * formfield_encoding = cl_http_request=>if_http_entity~co_encoding_raw. lo_http_entity = lo_http_client->request->if_http_entity~add_multipart( ). "utf-8编码文件名(目的是让上传后的文件名跟原来一样,不然会乱码) lv_file_name = gv_file_name. CALL FUNCTION 'WWW_URLENCODE' EXPORTING value = lv_file_name IMPORTING value_encoded = lv_name_encode. lv_content_disposition = 'form-data; name="file"; filename="' && lv_name_encode && '.' && gv_file_type && '"'. CALL METHOD lo_http_entity->set_header_field EXPORTING name = 'Content-Disposition' "value = 'form-data; name="file"; filename="测试文件.pdf"' value = lv_content_disposition. "文件后缀转小写 TRANSLATE gv_file_type TO LOWER CASE. CASE gv_file_type. WHEN 'jpg'. lv_content_type = 'image/jpeg'. WHEN 'png'. lv_content_type = 'image/png'. WHEN 'txt'. lv_content_type = 'text/plain'. WHEN 'pdf'. lv_content_type = 'application/pdf'. WHEN 'xlsx'. lv_content_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'. WHEN 'xls'. lv_content_type = 'application/vnd.ms-excel'. WHEN 'docx'. lv_content_type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'. WHEN 'doc'. lv_content_type = 'application/msword'. ENDCASE. CALL METHOD lo_http_entity->set_content_type EXPORTING content_type = lv_content_type. lv_len = xstrlen( gv_file ). CALL METHOD lo_http_entity->set_data EXPORTING data = gv_file offset = 0 length = lv_len. "发送请求 CALL METHOD lo_http_client->send EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 http_invalid_timeout = 4 OTHERS = 5. IF sy-subrc NE 0. CALL METHOD lo_http_client->get_last_error IMPORTING code = lv_error_code message = lv_error_msg. ENDIF. "请求返回结果 CALL METHOD lo_http_client->receive EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3. "请求返回的状态码 CALL METHOD lo_http_client->response->get_status IMPORTING code = lv_code. "获取接口返回的数据 lv_response = lo_http_client->response->get_cdata( ). cl_demo_output=>write( lv_response ). cl_demo_output=>display( ). ENDFORM.
落霞与孤鹜齐飞,秋水共长天一色