com.sapportals.portal.prt.util.http
Class MultiPartParser

java.lang.Object
  |
  +--com.sapportals.portal.prt.util.http.MultiPartParser

public class MultiPartParser
extends java.lang.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

Version:
$Revision: #3 $

Constructor Summary
MultiPartParser(java.io.InputStream in, java.lang.String boundary)
          Constructs a parser from an InputStream and a boundary.
 
Method Summary
 java.util.Hashtable getFileParts()
           
 java.util.Enumeration getHeaderFields()
          Gets an enumeration of all header fields of the current header.
 java.lang.String getHeaderValue(java.lang.String header)
          Gets the value of a specified part header field.
 java.util.Hashtable getParameterParts()
           
 java.io.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(java.io.InputStream in,
                       java.lang.String boundary)
                throws java.io.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
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 java.io.IOException
Proceeds to the header of the next part.

getHeaderFields

public java.util.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 java.lang.String getHeaderValue(java.lang.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 java.io.InputStream getPartInputStream()
Returns:
An InputStream to the Parts content

getParameterParts

public java.util.Hashtable getParameterParts()

getFileParts

public java.util.Hashtable getFileParts()