cancel
Showing results for 
Search instead for 
Did you mean: 

PI 7.31 Extra Byte in Java Mapping in HTTP with attachments to FTP scenario

Former Member
0 Kudos

Hi Gurus,

I have a HTTP with attachments(jpg files) to FTP scenario in SAP PI 7.31 environment.

I use a Java mapping to get an attachment incoming http request message(adapter type HTTP_AAE) and put this attachment as binary file type in binary transfer mode. Scenario works fine except one thing: in ftp server I receive a file with some extra bytes 0D. You can see an example.

I think it is a problem with usage of  method attachments.getContent(). Please, help to solve this question.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Java_Test;


import java.io.*;
import java.util.*;
import com.sap.aii.mapping.api.*;

public class Java_For_SCN extends AbstractTransformation {

    private Map param;
    TransformationInput transformationInput = null;
    InputAttachments inputAttachments = null;
    InputStream in = null;

    public Java_For_SCN() {
    }

    public void setParameter(Map map) {
        param = map;
        if (param == null) {
            param = new HashMap();
        }
    }


    public void transform(TransformationInput tranIn, TransformationOutput tranOut) throws StreamTransformationException {
        transformationInput = tranIn;
        //GET INPUT ATTACHEMENTS OBJECT 
        inputAttachments = tranIn.getInputAttachments();
        this.execute((InputStream) tranIn.getInputPayload().getInputStream(), (OutputStream) tranOut.getOutputPayload().getOutputStream());
    }

    public void execute(InputStream inputstream, OutputStream outputstream) {
        try {
            if (transformationInput != null)
            {             
                if (inputAttachments != null)
                {
                    //Important: First Enable the "Read Attachments" check box in Operation mapping if your code needs to handle attachments.  
                    if (inputAttachments.areAttachmentsAvailable())
                    {
                        String AttachmentID = "";
                        Collection<String> CollectionIDs = inputAttachments.getAllContentIds(true);
                        Object[] arrayObj = CollectionIDs.toArray();
                        int attachmentCount = arrayObj.length;
                        this.getTrace().addInfo("attachmentCount = " + Integer.toString(attachmentCount));
                       
                        if ( attachmentCount >=1 ) {
                        attachmentCount = 1; // temporary
                        }
                       
                        for (int i = 0; i < attachmentCount; i++) {
                           
                            this.getTrace().addInfo("i = " + Integer.toString(i)  );
                            AttachmentID = (String) arrayObj[i];
                            this.getTrace().addInfo("AttachemntID = " + AttachmentID);
                            Attachment attachments = inputAttachments.getAttachment(AttachmentID);
                            this.getTrace().addInfo("Attachment ContentType = " + attachments.getContentType());
                            outputstream.write(attachments.getContent());
                        }
                    }
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
            this.getTrace().addDebugMessage(e2.toString());
        }

        }
}

Accepted Solutions (1)

Accepted Solutions (1)

markangelo_dihiansan
Active Contributor
0 Kudos

Hello Vladimir,

Not sure if this would help, but have you tried getting the base64 content first before writing to outputstream as byte? e.g

String tmp = attachments.getBase64EncodedContent();

outputstream.write(tmp.getBytes("UTF-8"));

Regards,

Mark

Former Member
0 Kudos

Hello Mark,

I have already tried this

outputstream.write(attachments.getBase64EncodedContent().getBytes("UTF-8"));

It returnes binary file, which does not like jpg at all. And its size is much more than the original.

If you compare two files in my post(Badpayload.jpg and GoodPayload.jpg) in binary mode, you can see difference only in 628 bytes. It seems like carriage return. Maybe I need to getContent by Lines?

Regards,

Vladimir

markangelo_dihiansan
Active Contributor
0 Kudos

Hello Vladimir,

I tried your code, it is working fine on my PI (7.11) so I can't replicate your issue. Maybe you can try using payloadSwapBean and not use Java Mapping at all.

File Type is Binary.

You notice that my parameter value is C_MIMETYPE_JPEG, but that's because I am using an ABAP proxy to attach your JPEG. Replace this with the content type you find in sxi_monitor

Hope this helps,

Mark

Former Member
0 Kudos

Hello, Mark,

I think  I have found the answer. It's not problem of the PI. I use ftp-client FileZilla to get files from ftp-server, on which PI puts messsages. And there are some settings in this ftp-client, which change mode of transferring files according to the file type. In my scenarion I put files without extension to the ftp-server. And when I get a file from ftp-server without extension my ftp-client FileZilla transfers the file to my computer in ASCII mode and appends some bytes inside the file. In the screenshot below you can see the setting of ftp-client depending on the file type. On the right side you can see remote ftp-server. And on the left side you can see local machine. There is difference in some bytes in file without extension on the left side and on the right side. And there is no difference in jpg-files.

And here are settings for choosing transfering mode for different types of files in ftp-client:

So, it's a problem of incorrect settings of local ftp-client, not PI.

Mark, thanks a lot for ideas.

Best Regards,

Vladimir

Answers (0)