com.sapportals.portal.prt.util.http

Class MultiPartParser

java.lang.Object
  extended by com.sapportals.portal.prt.util.http.MultiPartParser

public class MultiPartParser
extends Object

This class offers parsing functionality for multipart/form-data content provided by an inputstream and a given boundary. See RFC 1341 for more details. Suppose you are given an InputStream in and a boundary String boundary. The following code reads all parts, their headers and their bodies, in a servlet style...

MultiPartParser parser = new MultiPartParser(in,boundary);
if (parser.onValidPart())
{
   while (parser.onValidPart())
   {
       out.println("-------- PART ----------<br>");
       Enumeration headers = parser.getHeaderFields();
       if (headers!=null)
        {
            out.println("<table>");
            while (headers.hasMoreElements())
            {
                String name = (String) headers.nextElement();
                String value = parser.getHeaderValue(name);
                out.println("<tr><td>"+name+"</td><td>"+value+"</td></tr>");
            }
            out.println("</table>");
        }
        // now read the parts data.
        InputStream partIn = parser.getPartInputStream();
        if (partIn!=null)
        {
            int c=0,count=0;
            while ((c=partIn.read())!=-1) count++;
            out.println("read "+count+" bytes from the part's data.<br>");
        }
        else
        {
            out.println("Failed to retrieve a part inputstream<br>");
        }
        parser.proceedToNextPart();
    }
}
Copyright (c) SAP Portals Europe GmbH 2001


Constructor Summary
MultiPartParser(InputStream in, String boundary)
          Constructs a parser from an InputStream and a boundary.
 
Method Summary
 Hashtable getFileParts()
           
 Enumeration getHeaderFields()
          Gets an enumeration of all header fields of the current header.
 String getHeaderValue(String header)
          Gets the value of a specified part header field.
 Hashtable getParameterParts()
           
 InputStream getPartInputStream()
           
 boolean onValidPart()
          Returns whether we are currently in front of a part.
 void proceedToNextPart()
          Proceeds to the header of the next part.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultiPartParser

public MultiPartParser(InputStream in,
                       String boundary)
                throws IOException
Constructs a parser from an InputStream and a boundary.

Parameters:
in - The inputstream containing the multipart data
boundary - The boundary separating the individual parts
Throws:
IOException
Method Detail

onValidPart

public boolean onValidPart()
Returns whether we are currently in front of a part.

Returns:
true if the parser is ready to read a parts data. In that case, there is valid header information and the parts body available. false otherwise.

proceedToNextPart

public void proceedToNextPart()
                       throws IOException
Proceeds to the header of the next part.

Throws:
IOException

getHeaderFields

public Enumeration getHeaderFields()
Gets an enumeration of all header fields of the current header.

Returns:
An enumeration of Header Fields of the most-lately read header

getHeaderValue

public String getHeaderValue(String header)
Gets the value of a specified part header field.

Parameters:
header - The name of a part's header field
Returns:
The value of a specified Header field

getPartInputStream

public InputStream getPartInputStream()
Returns:
An InputStream to the Parts content

getParameterParts

public Hashtable getParameterParts()

getFileParts

public Hashtable getFileParts()
Access Rights

This class can be accessed from:


SC DC Public Part ACH
[sap.com] EP-BASIS-API [sap.com] epbc.prtapi._apideprecated default EP-PIN-PRT
[sap.com] EP-BASIS-API [sap.com] tc/epbc/prt/api api EP-PIN
[sap.com] EP-BASIS-API [sap.com] tc/epbc/prt/lib/api api EP-PIN


Copyright 2011 SAP AG Complete Copyright Notice