如何实现SAP的RFC函数调用(原创)

连接sap系统需要通过sap javaconnect来连接,对于sapjco.jar系列文件有32位与64位之分【32位用的JAR版本是 2.1.10 (2011-05-10) ,64位用的JAR版本是 2.1.7 (2006-06-12)】。即对jdk有严格要求。现说明客户端32位部署及服务端64位部署两种情况:


一、 我本地是32位,部署本地客户端 sapjco32.zip
a) 将附件相对应位数的librfc32.dll、sapjcorfc.dll、msvcp71.dll、msvcr71.dll四个文件拷贝至system32,其中前两个文件是必须拷贝(后面2个可以不用)。
b) 将相对应位数的sapjco-2.1.7.jar拷贝至对应模块lib下,然后将其部署好。

这里我把它配置到我们的pom.xml里面了,如下:

  1. 首先把相应的JAR文件放在项目的\WEB-INF\lib目录下面。

     2.  在pom.xml里面指向该文件。

<dependency>
    <groupId>com.sap</groupId>
    <artifactId>sapjco</artifactId>
    <version>2.1.7</version>
    <scope>system</scope>
    <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/sapjco-2.1.7.jar</systemPath>
</dependency>


二、 服务器是64位 sapjco64.zip
a) 将附件相对应位数的librfc32.dll、sapjcorfc.dll、msvcp71.dll、msvcr71.dll四个文件拷贝至system32及SysWOW64文件夹下
b) 将相对应位数的sapjco-2.1.10.jar拷贝至服务端的lib下,然后将其部署好。同上。

<dependency>
    <groupId>com.sap</groupId>
    <artifactId>sapjco</artifactId>
    <version>2.1.10</version>
    <scope>system</scope>
    <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/sapjco-2.1.10.jar</systemPath>
</dependency>

 

如何在JAVA里面调用RFC函数,简单的demo如下:[详细的工具类 SapUtil.java]

package org.jeecgframework.core.util;

import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.JCO;
import com.sap.mw.jco.JCO.Structure;

public class SapUtil {
    public static double getRate(String rateType, String fromCurrency, String toCurrency, String date) {
    JCO.Client client = null;
    double rate = 0;
    try {
        client = addClientPool();
        JCO.Function func = getFunction(client, "BAPI_EXCHANGERATE_GETDETAIL");
        JCO.ParameterList inputParameterList = func.getImportParameterList();
        inputParameterList.getField("RATE_TYPE").setValue(rateType);
        inputParameterList.getField("FROM_CURR").setValue(fromCurrency);
        inputParameterList.getField("TO_CURRNCY").setValue(toCurrency);
        inputParameterList.getField("DATE").setValue(date);
        client.execute(func);
        JCO.ParameterList outputParameterList = func.getExportParameterList();
        Structure rateStructure = outputParameterList.getStructure("EXCH_RATE");
        rate = rateStructure.getDouble("EXCH_RATE");
        System.out.println(rate);
    } catch (JCO.Exception e) {
        e.printStackTrace();
    } finally {
        if (client != null) {
            JCO.releaseClient(client);
        }
    }
    return rate;
    }

    private static JCO.Client addClientPool() {
        String client = "800";
        String user = "crmuser1";
        String password = "CRM1";
        String language = "1";
        String host = "10.10.1.80"; //正式机
        String sysnr = "00";
        JCO.Client sapclient = null;
        try {
            sapclient = JCO.createClient(client, user, password, language, host, sysnr);
            sapclient.connect();
        } catch (JCO.Exception e) {
            throw new RuntimeException("SAP连接错误:" + e.getMessage());
        }
        return sapclient;
    }

    private static JCO.Function getFunction(JCO.Client client, String funcName) {
        String repositoryName = "repository";
        JCO.Function func = null;
        try {
            JCO.Repository repository = new JCO.Repository(repositoryName, client);
            IFunctionTemplate ft = repository.getFunctionTemplate(funcName);
            func = ft.getFunction();
        } catch (JCO.Exception e) {
            e.printStackTrace();
        }
        return func;
    }

    public static void main(String[] args) {
        getRate("M", "USD", "CNY", "20141001");
        getRate("M", "USD", "CNY", "00000000");
        getRate("M", "EUR", "CNY", null);
        getRate("M", "EUR", "CNY", "20141001");
    }
}

 

PS:可能遇到的问题(都是32位和64位所用文件不一致问题)

    1. java.lang.ExceptionInInitializerError: JCO.classInitialize(): Could not load middleware layer 'com.sap.mw.jco.rfc.MiddlewareRFC'JCO.nativeInit():
      Could not initialize dynamic link library sapjcorfc [C:\Windows\System32\sapjcorfc.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform].

    2. java.lang.ExceptionInInitializerError: JCO.classInitialize(): Could not load middleware layer 'com.sap.mw.jco.rfc.MiddlewareRFC'JCO.nativeInit():
      Could not initialize dynamic link library sapjcorfc. Found version "2.1.7 (2006-06-12)" but required version "2.1.10 (2011-05-10)".

posted on 2016-05-25 15:22  诡计的不老歌  阅读(2528)  评论(0编辑  收藏  举报