{
byte[] boundary = contentType.substring(
contentType.indexOf("boundary=")+9).getBytes();
InputStream input = (InputStream)req.getInputStream();
MultipartStream multi = new MultipartStream(input, boundary);
boolean nextPart = multi.skipPreamble();
while(nextPart)
{
Map headers = parseHeaders(multi.readHeaders());
String fieldName = getFieldName(headers);
if (fieldName != null)
{
String subContentType = getHeader(headers, CONTENT_TYPE);
if (subContentType != null && subContentType
.startsWith(MULTIPART_MIXED))
{
// Multiple files.
byte[] subBoundary =
subContentType.substring(
subContentType
.indexOf("boundary=")+9).getBytes();
multi.setBoundary(subBoundary);
boolean nextSubPart = multi.skipPreamble();
while (nextSubPart)
{
headers = parseHeaders(multi.readHeaders());
if (getFileName(headers) != null)
{
FileItem item = createItem(path, headers,
requestSize);
OutputStream os = item.getOutputStream();
try
{
multi.readBodyData(os);
}
finally
{
os.close();
}
params.append(getFieldName(headers), item);
}
else
{
// Ignore anything but files inside
// multipart/mixed.
multi.discardBodyData();
}
nextSubPart = multi.readBoundary();
}
multi.setBoundary(boundary);
}
else
{
if (getFileName(headers) != null)
{
// A single file.
FileItem item = createItem(path, headers,
requestSize);
OutputStream os = item.getOutputStream();
try
{
multi.readBodyData(os);
}
finally
{
os.close();
}
params.append(getFieldName(headers), item);
}
else
{
// A form field.
FileItem item = createItem(path, headers,
requestSize);
OutputStream os = item.getOutputStream();
try
{
multi.readBodyData(os);
}
finally
{
os.close();
}
params.append(getFieldName(headers),
new String(item.get()));
}
}
}
else
{
// Skip this part.
multi.discardBodyData();
}
nextPart = multi.readBoundary();
}
}
catch(IOException e)
{
throw new TurbineException("Processing of " + MULTIPART_FORM_DATA