一段使用Java程序和JCO(Java connector)连接SAP ABAP Netweaver系统的代码

简介: 一段使用Java程序和JCO(Java connector)连接SAP ABAP Netweaver系统的代码
package jco;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoRepository;
import com.sap.conn.jco.JCoTable;
import com.sap.conn.jco.ext.DestinationDataProvider;
/**
 * basic examples for Java to ABAP communication  
 * See help: https://help.sap.com/saphelp_nwpi711/helpdata/en/48/70792c872c1b5ae10000000a42189c/frameset.htm
 */
public class StepByStepClient
{
  static String DESTINATION_NAME = "ABAP_AS_WITHOUT_POOL";
  static public final String ABAP_DURATION = "abapLayerDuration";
  static public final String UPSELL_PRODUCT = "upsellProducts";
  static public final String PRODUCT_ID = "productID";
  static public final String PRODUCT_TEXT = "productText";
  static private final String FILE_ID = "fileID";
  static private final String FILE_OWNER = "fileOwner";
  static private final String FILE_CDATE = "fileCreationDate";
  static private final String FILE_NAME = "fileName";
    @SuppressWarnings("unused")
  static private Properties prepareProperty(){
        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "ldcixcd.jerry");
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "00");
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "111");
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "WANGJER");
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "pleaseInputYourPassword");
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   "en");
        createDestinationDataFile(DESTINATION_NAME, connectProperties);
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT,    "10");
        createDestinationDataFile(DESTINATION_NAME, connectProperties);
        return connectProperties;
    }
    static private void getProductImageTest(){
      JCoDestination destination = null;
    try {
      destination = JCoDestinationManager.getDestination(DESTINATION_NAME);
      JCoRepository repo = destination.getRepository();
        JCoFunction stfcConnection = repo.getFunction("ZDIS_GET_MATERIAL_IMAGES");
        JCoParameterList imports = stfcConnection.getImportParameterList();
        String materialID = "16";
        imports.setValue("IV_MATERIAL_ID", materialID);
        stfcConnection.execute(destination);
        JCoParameterList exports = stfcConnection.getExportParameterList();
          int abapDuration = exports.getInt("EV_DURATION");
          StringBuilder sb = new StringBuilder();
          sb.append("{ \"" + ABAP_DURATION + "\": " + abapDuration + ",");
          sb.append("\"" + UPSELL_PRODUCT + "\":[");
          JCoTable codes = exports.getTable("ET_IMAGES");
          int row = codes.getNumRows();
          System.out.println("Total rows: " + row);
          System.out.println("ABAP duration: " + abapDuration);
          for( int i = 0; i < row; i++){
            codes.setRow(i);
                sb.append("{\"" + FILE_ID + "\":" + codes.getString("FILEID") + ","
                    + "\"" + FILE_OWNER + "\":\"" + codes.getString("OWNER") + "\"" + ",");
                sb.append("{\"" + FILE_CDATE + "\":" + codes.getString("CREATION_DATE") + ","
                    + "\"" + FILE_NAME + "\":\"" + codes.getString("FILENAME") + "\""); 
                storeLocalFile(codes);
                if( i < row - 1){
                  sb.append("},");
                }
                else{
                  sb.append("}");
                }
          }
          sb.append("]}");
          System.out.println("Final json: " + sb.toString());
    } catch (JCoException e) {
      e.printStackTrace();
    }
    }
    static private void storeLocalFile(JCoTable codes){
      InputStream is = codes.getBinaryStream("FILECONTENT");
      try {
        File file = new File("c:\\temp\\" + codes.getString("FILENAME"));
        byte[] bytes = new byte[is.available()];
        is.read(bytes);
        /*String asB64 = Base64.getEncoder().encodeToString(bytes);
        byte[] asBytes = Base64.getDecoder().decode(asB64);*/
      OutputStream output = new FileOutputStream(file);
      BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
      bufferedOutput.write(bytes);
      bufferedOutput.close();
      is.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
     }
    @SuppressWarnings("unused")
  static private void getUpsellProductTest(){
      JCoDestination destination = null;
    try {
      destination = JCoDestinationManager.getDestination(DESTINATION_NAME);
      JCoRepository repo = destination.getRepository();
        JCoFunction stfcConnection = repo.getFunction("ZDIS_GET_UPSELL_MATERIALS");
        JCoParameterList imports = stfcConnection.getImportParameterList();
        String customerID = "1000040";
        String materialID = "11";
        imports.setValue("IV_CUSTOMER_ID", customerID);
        imports.setValue("IV_MATERIAL_ID", materialID);
        stfcConnection.execute(destination);
        JCoParameterList exports = stfcConnection.getExportParameterList();
        // int result = exports.getInt("EV_RESULT");
          int abapDuration = exports.getInt("EV_DURATION");
          StringBuilder sb = new StringBuilder();
          sb.append("{ \"" + ABAP_DURATION + "\": " + abapDuration + ",");
          sb.append("\"" + UPSELL_PRODUCT + "\":[");
          JCoTable codes = exports.getTable("ET_MATERIALS");
          int row = codes.getNumRows();
          System.out.println("Total rows: " + row);
          System.out.println("ABAP duration: " + abapDuration);
          for( int i = 0; i < row; i++){
            codes.setRow(i);
                System.out.println(codes.getString("MATERIAL_ID") + '\t' + codes.getString("MATERIAL_TEXT"));
                sb.append("{\"" + PRODUCT_ID + "\":" + codes.getString("MATERIAL_ID") + ","
                    + "\"" + PRODUCT_TEXT + "\":\"" + codes.getString("MATERIAL_TEXT") + "\"");
                if( i < row - 1){
                  sb.append("},");
                }
                else{
                  sb.append("}");
                }
          }
          sb.append("]}");
          System.out.println("Final json: " + sb.toString());
    } catch (JCoException e) {
      e.printStackTrace();
    }
    }
    static public void main(String[] arg) {
      getProductImageTest();
    }
    static private void createDestinationDataFile(String destinationName, Properties connectProperties)
    {
        File destCfg = new File(destinationName+".jcoDestination");
        try
        {
            FileOutputStream fos = new FileOutputStream(destCfg, false);
            connectProperties.store(fos, "for tests only !");
            fos.close();
        }
        catch (Exception e)
        {
            throw new RuntimeException("Unable to create the destination files", e);
        }
    }
}


相关文章
|
3天前
|
Java 数据库连接 数据库
深入探讨Java连接池技术如何通过复用数据库连接、减少连接建立和断开的开销,从而显著提升系统性能
在Java应用开发中,数据库操作常成为性能瓶颈。本文通过问题解答形式,深入探讨Java连接池技术如何通过复用数据库连接、减少连接建立和断开的开销,从而显著提升系统性能。文章介绍了连接池的优势、选择和使用方法,以及优化配置的技巧。
8 1
|
5天前
|
JavaScript Java 项目管理
Java毕设学习 基于SpringBoot + Vue 的医院管理系统 持续给大家寻找Java毕设学习项目(附源码)
基于SpringBoot + Vue的医院管理系统,涵盖医院、患者、挂号、药物、检查、病床、排班管理和数据分析等功能。开发工具为IDEA和HBuilder X,环境需配置jdk8、Node.js14、MySQL8。文末提供源码下载链接。
|
8天前
|
移动开发 前端开发 JavaScript
java家政系统成品源码的关键特点和技术应用
家政系统成品源码是已开发完成的家政服务管理软件,支持用户注册、登录、管理个人资料,家政人员信息管理,服务项目分类,订单与预约管理,支付集成,评价与反馈,地图定位等功能。适用于各种规模的家政服务公司,采用uniapp、SpringBoot、MySQL等技术栈,确保高效管理和优质用户体验。
|
10天前
|
XML JSON 监控
告别简陋:Java日志系统的最佳实践
【10月更文挑战第19天】 在Java开发中,`System.out.println()` 是最基本的输出方法,但它在实际项目中往往被认为是不专业和不足够的。本文将探讨为什么在现代Java应用中应该避免使用 `System.out.println()`,并介绍几种更先进的日志解决方案。
32 1
|
运维 监控 Kubernetes
SAP MTA 里定义 SAP UI5 module 的一个例子
SAP MTA 里定义 SAP UI5 module 的一个例子
|
XML JSON 数据格式
SAP UI5 XML 视图中数据绑定路径语法的难点和易混淆点的专题讲解
SAP UI5 XML 视图中数据绑定路径语法的难点和易混淆点的专题讲解
|
JavaScript 前端开发
SAP UI5 里 FlexBox 控件使用的一个例子
SAP UI5 里 FlexBox 控件使用的一个例子
|
前端开发
关于 SAP UI5 应用 render2 实例异步创建的编码实现(2)
关于 SAP UI5 应用 render2 实例异步创建的编码实现
关于 SAP UI5 应用 render2 实例异步创建的编码实现(1)
关于 SAP UI5 应用 render2 实例异步创建的编码实现
|
XML JavaScript 前端开发
SAP UI5 的数据绑定语法概述
SAP UI5 的数据绑定语法概述