checkMaxSize = true;
}
while (iter.hasNext())
{
final FileItemStream item = iter.next();
FileItem fileItem = fac.createItem(item.getFieldName(), item
.getContentType(), item.isFormField(), item.getName());
long allowedLimit = 0L;
try
{
if (maxFileSize != -1L || checkMaxSize)
{
if (checkMaxSize)
{
allowedLimit = maxSize > maxFileSize ? maxFileSize : maxSize;
}
else
{
//Just put the limit
allowedLimit = maxFileSize;
}
long contentLength = getContentLength(item.getHeaders());
//If we have a content length in the header we can use it
if (contentLength != -1L && contentLength > allowedLimit)
{
throw new FileUploadIOException(
new FileSizeLimitExceededException(
"The field "
+ item.getFieldName()
+ " exceeds its maximum permitted "
+ " size of " + allowedLimit
+ " characters.",
contentLength, allowedLimit));
}
//Otherwise we must limit the input as it arrives (NOTE: we cannot rely
//on commons upload to throw this exception as it will close the
//underlying stream
final InputStream itemInputStream = item.openStream();
InputStream limitedInputStream = new LimitedInputStream(
itemInputStream, allowedLimit)
{
protected void raiseError(long pSizeMax, long pCount)
throws IOException
{
throw new FileUploadIOException(
new FileSizeLimitExceededException(
"The field "
+ item.getFieldName()
+ " exceeds its maximum permitted "
+ " size of "
+ pSizeMax
+ " characters.",
pCount, pSizeMax));
}
};
//Copy from the limited stream
long bytesCopied = Streams.copy(limitedInputStream, fileItem
.getOutputStream(), true);
// Decrement the bytesCopied values from maxSize, so the next file copied
// takes into account this value when allowedLimit var is calculated
// Note the invariant before the line is maxSize >= bytesCopied, since if this
// is not true a FileUploadIOException is thrown first.
maxSize -= bytesCopied;
}
else
{
//We can just copy the data
Streams.copy(item.openStream(), fileItem
.getOutputStream(), true);
}
}
catch (FileUploadIOException e)
{
try
{
throw (FileUploadException) e.getCause();
}
catch (FileUploadBase.FileSizeLimitExceededException se)
{
request
.setAttribute(
"org.apache.myfaces.custom.fileupload.exception",
"fileSizeLimitExceeded");
String fieldName = fileItem.getFieldName();
request.setAttribute(
"org.apache.myfaces.custom.fileupload."+fieldName+".maxSize",
new Integer((int)allowedLimit));
}
}
catch (IOException e)
{
throw new IOFileUploadException("Processing of "
+ FileUploadBase.MULTIPART_FORM_DATA
+ " request failed. " + e.getMessage(), e);
}
if (fileItem instanceof FileItemHeadersSupport)
{
final FileItemHeaders fih = item.getHeaders();
((FileItemHeadersSupport) fileItem).setHeaders(fih);
}
if (fileItem != null)
{
items.add(fileItem);