外围系统如何用JAVA获取SAP PA30人事主数据的相片?SAP ABAP开发技巧

客户这几天搞了个考勤展示外挂系统。

在公司的进门口搞了几块电子大屏幕。

显示每个员工的相片、到岗外出勤快、请假相关信息。

号称数据库仓储驾驶舱展现。老板喜欢~

销售也喜欢。

 

每到春天,客户就带着他们潜在的客户来参观考察。

展示各种电子显示屏。看起来确实高大上。

所有PA30的人事数据都同步过去了,就剩下头像相片信息没过去。

JAVA小哥哥想了很久,总算实现了~这里分享一下实现思路和解决方案。

SAP系统中,相片的存储格式为流文件,需要使用函数下载。

 

JAVA实现代码方案:

package com.ibm.ibmwriteback.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import sun.misc.BASE64Decoder;

public class SAPPhotoSync {
public static void getSAPEmployeePhoto(){
try{
HttpClient httpClient = new HttpClient();
System.out.println(“start visiting webservice!”);
PostMethod postMethod = new PostMethod(“http://10.1.0.217/cordys/com.eibus.web.soap.Gateway.wcp?organization=o=ibm,cn=cordys,cn=defaultInst1,o=ibm”);
String soapRequestData =
“<soapenv:Envelope xmlns:soapenv=\”http://schemas.xmlsoap.org/soap/envelope/\” xmlns:sap=\”http://schemas.cordys.com/tbpm/sapbpc\”>”+
“<soapenv:Header/>” +
“<soapenv:Body>” +
“<sap:ZHR_GET_PHOTO>” +
“<sap:PERNR>00000001</sap:PERNR>”+
“</sap:ZHR_GET_PHOTO>” +
“</soapenv:Body>” +
“</soapenv:Envelope>”;
StringRequestEntity requestEntity = new StringRequestEntity(soapRequestData,”text/xml”,”UTF-8″);
postMethod.setRequestEntity(requestEntity);

int statusCode = httpClient.executeMethod(postMethod);
if(statusCode == 200) {
System.out.println(“visiting webservice success!”);
InputStream result = postMethod.getResponseBodyAsStream();
SAXReader sb = new SAXReader();
Document doc = sb.read(result, “UTF-8”);
Element root = doc.getRootElement();
Iterator<Element> it = root.element(“Body”).element(“ZHR_GET_PHOTOResponse”).element(“T_BINARCHIVOBJECT”).elementIterator(“item”);
String str = “”;
while(it.hasNext()){
Element node = it.next();
str+=node.elementText(“LINE”);
}
BASE64Decoder decoder = new BASE64Decoder();
byte[] strByte = decoder.decodeBuffer(str);

String exportFile = “C:/empPhoto.jpg”;
FileOutputStream out = new FileOutputStream(new File(exportFile));
out.write(strByte);
out.close();
}else{
System.out.println(“error:”+postMethod.getResponseBodyAsString());
}
System.out.println(“visiting webservice end!”);
}catch(Exception e){
e.printStackTrace();
}
}

public static void main(String[] args){
getSAPEmployeePhoto();
}
}

SAP测试代码调用:

PARAMETERS p_id TYPE pa0001-pernr OBLIGATORY.

START-OF-SELECTION.

DATA lt_tbl1024 TYPE STANDARD TABLE OF tbl1024 WITH HEADER LINE.

CALL FUNCTION ‘ZHR_GET_PHOTO’
EXPORTING
pernr                   = p_id
* IMPORTING
*   E_RETURN                =
*   E_MSG                   =
TABLES
t_binarchivobject       = lt_tbl1024[] .

CALL FUNCTION ‘GUI_DOWNLOAD’
EXPORTING
*   BIN_FILESIZE                    =
filename                        = ‘D:\\SAP\TEST.PNG’
filetype                        = ‘BIN’
*   APPEND                          = ‘ ‘
*   WRITE_FIELD_SEPARATOR           = ‘ ‘
*   HEADER                          = ’00’
*   TRUNC_TRAILING_BLANKS           = ‘ ‘
*   WRITE_LF                        = ‘X’
*   COL_SELECT                      = ‘ ‘
*   COL_SELECT_MASK                 = ‘ ‘
*   DAT_MODE                        = ‘ ‘
*   CONFIRM_OVERWRITE               = ‘ ‘
*   NO_AUTH_CHECK                   = ‘ ‘
codepage                        = ‘8400’
*   IGNORE_CERR                     = ABAP_TRUE
*   REPLACEMENT                     = ‘#’
*   WRITE_BOM                       = ‘ ‘
*   TRUNC_TRAILING_BLANKS_EOL       = ‘X’
*   WK1_N_FORMAT                    = ‘ ‘
*   WK1_N_SIZE                      = ‘ ‘
*   WK1_T_FORMAT                    = ‘ ‘
*   WK1_T_SIZE                      = ‘ ‘
*   WRITE_LF_AFTER_LAST_LINE        = ABAP_TRUE
*   SHOW_TRANSFER_STATUS            = ABAP_TRUE
* IMPORTING
*   FILELENGTH                      =
TABLES
data_tab                        = lt_tbl1024[]
*   FIELDNAMES                      =
EXCEPTIONS
file_write_error                = 1
no_batch                        = 2
gui_refuse_filetransfer         = 3
invalid_type                    = 4
no_authority                    = 5
unknown_error                   = 6
header_not_allowed              = 7
separator_not_allowed           = 8
filesize_not_allowed            = 9
header_too_long                 = 10
dp_error_create                 = 11
dp_error_send                   = 12
dp_error_write                  = 13
unknown_dp_error                = 14
access_denied                   = 15
dp_out_of_memory                = 16
disk_full                       = 17
dp_timeout                      = 18
file_not_found                  = 19
dataprovider_exception          = 20
control_flush_error             = 21
OTHERS                          = 22
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

 

运行完毕,即可下载SAP PA30的头像相片信息到SAP本地系统或者在外围系统显示出来。

如果想要进一步优化,可以先加入检测代码函数:HR_IMAGE_EXISTS

您学废了么?

 

如果这篇文章对您有用,欢迎转载,

转载请务必注明来源《今日热点在看》http://www.hot583.com/

详细实现代码见微信公众号hot583或者在这个链接联系我哦~

本文链接:http://www.hot583.com/2022/03/13/how-java-get-sap-pa30-photo-data/