Skip to Content
avatar image
Former Member

How to send a attachment from Java layer to ABAP Layer

Hi,

There is a document browsed and selected as attachment in the Flex UI.

How to save this document in ABAP table in the R/3 system.

Since Flex can not communicate directly with ABAP, I need to write a JAVA code (Either a servlet or an EJB) and expose this as a webservice to the Flex.

My Java webservice will make a RFC call to ABAP.

I have no clues on how to send/save the document from Java to ABAP Table.

Please give me some techincal inputs on this.

Thanks & Regards,

Kishore Babu T

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

2 Answers

  • avatar image
    Former Member
    Jul 07, 2009 at 09:40 AM

    KishoreBabu,

    Step 1 ) You need to first read the input file in java using standard InputStreamReader. And read the file line by line and save into a vector or arraylist or something as per ur requirement.

    Sample code ( to be written in servlet):

    try{

    FileItemFactory factory = new DiskFileItemFactory();

    /Create a new file upload handler

    ServletFileUpload upload = new ServletFileUpload(factory);

    //Parse the request

    items = upload.parseRequest(request);

    Iterator iter = items.iterator();

    while (iter.hasNext()) {

    FileItem item = (FileItem) iter.next();

    if (item.isFormField()) {

    ;

    }

    else {

    //it must be the file

    filename = item.getName();

    uploadedStream = item.getInputStream();

    uploadedStream = item.getInputStream();

    nputStreamReader inR = new InputStreamReader( uploadedStream );

    BufferedReader buf = new BufferedReader( inR );

    String line;

    int count = 0;

    while ( ( line = buf.readLine() ) != null ) {

    // read line by line store in a vector in java

    Step 2) Once you have read & stored the file contents in java variable, you need to then call ur webservice using the java variable as input. Use the set methods of ur webservice class to set the input data. Make an RFC call using JCO and set the input parameters as required.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 14, 2009 at 09:34 AM

    What you need to do is to send this document to sap via rfc using binary mode.

    First java part inside JCO

    What you need to do is to send this document to sap via rfc using binary mode.

    First java part inside JCO

    byte[] data = your file as byte array 
                JCoTable contentTable = jCoFunction.getTableParameterList().getTable("T_CONTENT");
                ByteArrayInputStream dataStream = new ByteArrayInputStream(data);
                BufferedInputStream bufferedInputStream = new BufferedInputStream(dataStream);
                byte[] dataPart;
                while (bufferedInputStream.available() > 0){
                    dataPart = new byte[1022];// This size is quite important //
                    bufferedInputStream.read(dataPart);
                    contentTable.appendRow();
                    contentTable.setValue("LINE",dataPart);
    
                }
                jCoFunction.getExportParameterList().setValue("E_FILE_SIZE",data.length); 
    

    Now the abap types of lines

    *"  EXPORTING
    *"     VALUE(E_FILE_SIZE) TYPE  BDS_COMPSI
    *"  TABLES
    *"      T_CONTENT STRUCTURE  BAPICONTEN OPTIONAL
    

    and then you can save this to a table. just look for BAPICONTEN examples. In next post code how to save it to BDS. I just dont know why formatting does not work

    Edited by: Marcin Winkiel on Jul 14, 2009 11:35 AM

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member
       DATA :
        LV_CLASSNAME         TYPE BAPIBDS01-CLASSNAME,
        LV_CLASSTYPE         TYPE BAPIBDS01-CLASSTYPE,
        LV_OBJECT_KEY        TYPE BAPIBDS01-OBJKEY,
        LS_FILES             LIKE BAPIFILES,
        LT_FILES             LIKE BAPIFILES OCCURS 1,
        LS_SIGNATURE         TYPE BAPISIGNAT,
        LT_SIGNATURE_OLD     TYPE BAPISIGNAT OCCURS 1,
        LS_SIGNATURE_OLD     TYPE BAPISIGNAT,
        LT_SIGNATURE         TYPE BAPISIGNAT OCCURS 1,
        LT_RELATIONS         LIKE BAPIRELAT OCCURS 1,
        LS_RELATIONS         TYPE BAPIRELAT,
        LT_COMPONENTS        LIKE BAPICOMPON OCCURS 1,
        LS_COMPONENTS        TYPE BAPICOMPON,
        LV_DESCRIPTION       TYPE CHAR25,
        LS_PROPERTIES        TYPE BAPIPROPER,
        LT_PROPERTIES        TYPE SBDST_PROPERTIES,
        LV_VBELN             TYPE VBELN,
        LV_VERSION           TYPE CHAR2.
      
        IF T_CONTENT[] IS NOT INITIAL.
      
          LS_COMPONENTS-DOC_COUNT = '00000001'.
          LS_COMPONENTS-MIMETYPE = 'application/msword'.
          LS_COMPONENTS-COMP_SIZE = I_FILE_SIZE.
          APPEND LS_COMPONENTS TO LT_COMPONENTS.
      
          LV_OBJECT_KEY = I_OBJKEY.
      
          CALL METHOD CL_BDS_DOCUMENT_SET=>GET_INFO
            EXPORTING
               CLASSNAME           = I_CLASSNAME
               CLASSTYPE           = I_CLASSTYPE
               OBJECT_KEY          = LV_OBJECT_KEY
            CHANGING
               SIGNATURE           = LT_SIGNATURE_OLD
          EXCEPTIONS
            NOTHING_FOUND       = 1
            ERROR_KPRO          = 2
            INTERNAL_ERROR      = 3
            PARAMETER_ERROR     = 4
            NOT_AUTHORIZED      = 5
            NOT_ALLOWED         = 6
            OTHERS              = 7.
      
          IF SY-SUBRC <> 0.
      *    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
      *               WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
          ENDIF.
      
          CLEAR LS_SIGNATURE_OLD.
          IF SY-SUBRC = 0.
            DELETE LT_SIGNATURE_OLD
                WHERE PROP_VALUE NS LV_VBELN.
      
            IF NOT LT_SIGNATURE_OLD[] IS INITIAL.
              SORT LT_SIGNATURE_OLD BY DOC_VER_NO DESCENDING.
              READ TABLE LT_SIGNATURE_OLD INTO LS_SIGNATURE_OLD INDEX 1.
      
              IF NOT LS_SIGNATURE_OLD IS INITIAL.
      
                LV_DESCRIPTION = LS_SIGNATURE-PROP_VALUE.
      
                LS_PROPERTIES-PROP_NAME  = 'DESCRIPTION'.
                LV_VERSION = LS_SIGNATURE_OLD-DOC_VER_NO + 1.
                SHIFT LV_VERSION LEFT DELETING LEADING SPACE.
                SHIFT LV_VERSION RIGHT DELETING TRAILING SPACE.
                CONCATENATE 'Doc.' I_OBJKEY 'Ver.' LV_VERSION INTO LS_PROPERTIES-PROP_VALUE
                SEPARATED BY space.
                APPEND LS_PROPERTIES TO LT_PROPERTIES.
      
                CALL METHOD CL_BDS_DOCUMENT_SET=>CREATE_VERSION_WITH_TABLE
             EXPORTING
      *      LOGICAL_SYSTEM  =
               CLASSNAME       = I_CLASSNAME
               CLASSTYPE       = I_CLASSTYPE
      *      CLIENT          =
      *      OBJECT_KEY      =
               DOC_ID          = LS_SIGNATURE_OLD-DOC_ID
               DOC_VER_NO      = LS_SIGNATURE_OLD-DOC_VER_NO
               DOC_VAR_ID      = LS_SIGNATURE_OLD-DOC_VAR_ID
      *    IMPORTING
      *      NEW_DOC_VER_NO  =
             CHANGING
               COMPONENTS      = LT_COMPONENTS
               CONTENT         = T_CONTENT[]
               PROPERTIES      = LT_PROPERTIES
            EXCEPTIONS
              ERROR_KPRO      = 1
              INTERNAL_ERROR  = 2
              NOTHING_FOUND   = 3
              PARAMETER_ERROR = 4
              NOT_AUTHORIZED  = 5
              NOT_ALLOWED     = 6
              OTHERS          = 7
                   .
                IF SY-SUBRC <> 0.
      *   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
      *              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
                ENDIF.
      
              ENDIF.
            ENDIF.
          ELSE.
      
          LS_SIGNATURE-DOC_COUNT = '00000001'.
          LS_SIGNATURE-PROP_NAME = 'BDS_DOCUMENTCLASS'.
          LS_SIGNATURE-PROP_VALUE = 'DOC'.
      
          APPEND LS_SIGNATURE TO LT_SIGNATURE.
          LV_VERSION = 1.
          LS_SIGNATURE-DOC_COUNT = '00000001'.
          LS_SIGNATURE-PROP_NAME  = 'DESCRIPTION'.
          CONCATENATE 'Doc.' I_OBJKEY 'Ver.' LV_VERSION INTO LS_SIGNATURE-PROP_VALUE
          SEPARATED BY space.
          APPEND LS_SIGNATURE TO LT_SIGNATURE.
      
          LS_SIGNATURE-DOC_COUNT = '00000001'.
          LS_SIGNATURE-PROP_NAME  = 'LANGUAGE'.
          LS_SIGNATURE-PROP_VALUE = SY-LANGU.
          APPEND LS_SIGNATURE TO LT_SIGNATURE.
      
            CALL METHOD CL_BDS_DOCUMENT_SET=>CREATE_WITH_TABLE
        EXPORTING
      *    LOGICAL_SYSTEM  =
          CLASSNAME       = I_CLASSNAME
          CLASSTYPE       = I_CLASSTYPE
      *    CLIENT          =
          COMPONENTS      = LT_COMPONENTS
          CONTENT         = T_CONTENT[]
        CHANGING
          OBJECT_KEY      = LV_OBJECT_KEY
          SIGNATURE       = LT_SIGNATURE
          EXCEPTIONS
            INTERNAL_ERROR  = 1
            ERROR_KPRO      = 2
            PARAMETER_ERROR = 3
            NOT_AUTHORIZED  = 4
            NOT_ALLOWED     = 5
            NOTHING_FOUND   = 6
            OTHERS          = 7
              .
            IF SY-SUBRC <> 0.
      * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
      *            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
            ENDIF.
      
          ENDIF.
      
        ENDIF.
      
      ENDFUNCTION.
      

      Edited by: Marcin Winkiel on Jul 14, 2009 11:42 AM