java连接sap需要做的:
1.下载sap的jar,sapjco3相关的包
jar包放百度云上了,免费下载(jar包下载第四个就行了,32位,64位都包含)
链接:https://pan.baidu.com/s/15hw0BskEvH4hkN6sDIM5cQ
提取码:bobo
–来自百度网盘超级会员V5的分享
2.上代码测试
package com.lns.saptest.controller;
import com.sap.conn.jco.*;
import com.sap.conn.jco.ext.DestinationDataProvider;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
@RestController
public class SapConnectionController {
//这个跟我写的一样就行了,具体用处我不太清楚,欢迎评论补充
private static final String ABAP_AS_POOLED= "ABAP_AS_WITH_POOL";
static{
Properties connectProperties = new Properties();
//主要参数,根据你的配置来
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "88.88.88.88");//服务器
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "01"); //系统编号
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "051"); //SAP集团
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "RFC_OA"); //SAP用户名
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "12345678"); //密码
//下面三个参数默认跟我的一样就行了
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "zh"); //登录语言
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); //最大连接数
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); //最大连接线程
createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
}
//我用的是接口地址访问,自己测试用测试类main方法就可以测
@GetMapping("/sapConnection")
public void sapConnection() throws JCoException {
JCoDestination destination = connect();
//.ping方法是测试连接是否可用,如果这一步报错了,1.检查配置是否正确,2.检查sap的网络是否通
destination.ping();
System.out.println("连接sap成功");
JCoFunction function = destination.getRepository().getFunction("ZRFC_GET_MC_ACC_GG");
if (function == null) {
throw new RuntimeException("STFC_CONNECTION not found in SAP.");
}
//设置请求头参数:I_FGCODE是我的入参key
function.getImportParameterList().setValue("I_FGCODE", "000000000007900787");
try {
function.execute(destination);
} catch (AbapException e) {
System.out.println(e.toString());
}
//调用SAP函数成功会返回结果
JCoParameterList list = function.getExportParameterList();
//输出:E_MCCODE是我的输出参数key
System.out.println(list.getString("E_MCCODE"));
}
/**
*创建SAP接口属性文件。
*@paramnameABAP管道名称
*@paramsuffix属性文件后缀
*@paramproperties属性文件内容
*/
private static void createDataFile(String name, String suffix, Properties properties){
File cfg = new File(name+"."+suffix);
if(cfg.exists()){
cfg.deleteOnExit();
}
try{
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "for tests only !");
fos.close();
}catch (Exception e){
System.out.println("Create Data file fault, error msg: " + e.toString());
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}
}
/**
*获取SAP连接
*@returnSAP连接对象
*/
public static JCoDestination connect(){
JCoDestination destination =null;
try {
destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
} catch (JCoException e) {
System.out.println("Connect SAP fault, error msg: " + e.toString());
}
return destination;
}
}
有问题欢迎留言提问,看到就会回复
版权声明:本文为qq_43082438原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。