Skip to Content
Author's profile photo Former Member

HTML MultiPart Form upload using HTTP Plain adapter with Java mapping

In this blog, I would like to generate an HTML Form based request from SAP PI HTTP adapters. In the new java based HTTP adapter, SAP has made it easier to create form based request using extra features in the communication channel. For example in HTTP_AAE adapter, you can now specify that the HTTP request is a multi-part form request and you can also send attachments for a file upload.


However in this blog, I will be solving this problem by manually creating Multipart request in JAVA Mapping. This way you can even use plain HTTP adapter if you are using lower versions of PI.

First thing we need to understand is how a multipart request actually looks like. You should visit this URL for official specification on multipart upload. In the section 13.4.2 you will find the specification for multipart request.


I will explain how the PI HTTP request should look like. Basically If your HTML request(for web page testing) is like this:

html request.JPG

Then multipart request(body) which you should generate from PI should be like this:(This should also be the output of java mapping)

multipart request.JPG

Here boundary is very important. I have used a constant boundary value. This boundary value is specified in java mapping while generating such request and also the same boundary is specified in HTTP Receiver communication channel in the content type field.


Now let’s look at the java mapping code which is used to generate such a request. The code, I have used is pretty simple.




public class CreateForm implements StreamTransformation {

     private String boundary;

     private static final String LINE_FEED = “\r\n”;

     private HttpURLConnection httpConn;

     private String charset;

     private String body;

     private OutputStream outputStream;

     private PrintWriter writer;

     public void execute(InputStream arg0, OutputStream arg1)

  throws StreamTransformationException {

  // TODO Auto-generated method stub

         // creates a unique boundary based on time stamp

        // boundary = “===” + System.currentTimeMillis() + “===”;

         boundary = “–ejjeeffe1”;

         body = “Content-Type: multipart/form-data; boundary=”;

         body = body + boundary + LINE_FEED + LINE_FEED;

//        body.concat(boundary);

         addFormField(“event”,”Import File” );

         addFormField(“Filename”,”” );


         body = body + boundary;


         body = body + LINE_FEED + “Content-Disposition: form-data; name=\”” + “content” +

         “\”” + “;” + “filename=\”” + “” + “\”” +

          LINE_FEED + “Content-Type: application/zip” + LINE_FEED +

          “Content-Transfer-Encoding: binary” + LINE_FEED + LINE_FEED;


         byte[] buffer = new byte[4096];

         byte[] message = body.getBytes();

     //    int bytesRead = -1;

         ByteArrayOutputStream outbody = new ByteArrayOutputStream();


         try {


  int len = -1;

  while ((len = != -1) {

  arg1.write(buffer, 0, len);


  } catch (IOException e) {




        String endchar = LINE_FEED + boundary;

  try {


  } catch (Exception e) {




  public void setParameter(Map arg0) {

  // TODO Auto-generated method stub



     * Addsa form field to the request

     * @param name field name

     * @param value field value


    public void addFormField(String name, String value) {

    body = body + boundary + LINE_FEED + “Content-Disposition: form-data; name=\”” + name + “\””

    + LINE_FEED + “Content-Type: text/plain” + LINE_FEED + LINE_FEED

    + value + LINE_FEED;



The contents of the file in this case are directly supplied in the input message. In my test case, sender is an FTP which picks up ‘zip’ file from FTP. However we can also use support scenarios where file is supplied as an attachment (for example if the sender is SOAP etc.). In that case java mapping code will be slightly changed to read file binary data from attachment.

Hopefully this gives you an idea of how to create multipart form request. I would still recommend to use standard feature in HTTP_AAE adapter to create form upload where ever possible. This way, system will generate request for you. Use this method of java mapping only when standard feature is not possible.

This blog tries to explain in simple way, how a multipart HTML request looks like, how it can be easily generated from XI and provides java code to achieve a simple scenario.



Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member


      very useful info. It provides insight to the capability of HTTP_AEE adapter. keep sharing the knowledge.


      Author's profile photo Vijender Gouda
      Vijender Gouda

      It doesnt mean to HTTP_AAE adapter ,  it may slide to  SOAP (xi3.0) with the client proxy sight to this .

      correct me if am wrong !!!

      Author's profile photo Former Member
      Former Member

      Very useful Info.

      Author's profile photo Vijender Gouda
      Vijender Gouda

      useful info.

      Author's profile photo Former Member
      Former Member

      Thanks for sharing!

      Author's profile photo Former Member
      Former Member

      Very Good, Ravi. Keep up good work!

      Author's profile photo Narendra Babu N
      Narendra Babu N

      Hi Ravi,


      Can you please check below URL and provide your valuable inputs.

      Thank you!