use of com.helger.web.fileupload.IFileItemIterator in project ph-web by phax.
the class IProgressListenerTest method _runTest.
private void _runTest(final int NUM_ITEMS, final long pContentLength, final MockHttpServletRequest request) throws FileUploadException, IOException {
final ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory(CGlobal.BYTES_PER_MEGABYTE, null));
final ProgressListenerImpl listener = new ProgressListenerImpl(pContentLength, NUM_ITEMS);
upload.setProgressListener(listener);
final IFileItemIterator iter = upload.getItemIterator(request);
for (int i = 0; i < NUM_ITEMS; i++) {
final IFileItemStream stream = iter.next();
final InputStream istream = stream.openStream();
for (int j = 0; j < 16384 + i; j++) {
/**
* This used to be assertEquals((byte) j, (byte) istream.read()); but
* this seems to trigger a bug in JRockit, so we express the same like
* this:
*/
final byte b1 = (byte) j;
final byte b2 = (byte) istream.read();
if (b1 != b2) {
fail("Expected " + b1 + ", got " + b2);
}
}
assertEquals(-1, istream.read());
}
assertFalse(iter.hasNext());
listener._checkFinished();
}
use of com.helger.web.fileupload.IFileItemIterator in project ph-web by phax.
the class AbstractFileUploadBase method parseRequest.
/**
* Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
* compliant <code>multipart/form-data</code> stream.
*
* @param aCtx
* The context for the request to be parsed.
* @return A list of <code>FileItem</code> instances parsed from the request,
* in the order that they were transmitted.
* @throws FileUploadException
* if there are problems reading/parsing the request or storing files.
*/
@Nonnull
@ReturnsMutableCopy
public ICommonsList<IFileItem> parseRequest(@Nonnull final IRequestContext aCtx) throws FileUploadException {
final ICommonsList<IFileItem> aItems = new CommonsArrayList<>();
boolean bSuccessful = false;
try {
final IFileItemIterator aItemIter = getItemIterator(aCtx);
final IFileItemFactory aFileItemFactory = getFileItemFactory();
if (aFileItemFactory == null)
throw new IllegalStateException("No FileItemFactory has been set.");
while (aItemIter.hasNext()) {
final IFileItemStream aFileItemStream = aItemIter.next();
// Don't use getName() here to prevent an InvalidFileNameException.
final IFileItem aFileItem = aFileItemFactory.createItem(aFileItemStream.getFieldName(), aFileItemStream.getContentType(), aFileItemStream.isFormField(), aFileItemStream.getNameUnchecked());
aItems.add(aFileItem);
try (final InputStream aIS = aFileItemStream.openStream();
final OutputStream aOS = aFileItem.getOutputStream()) {
final byte[] aBuffer = new byte[8192];
int nBytesRead;
// potentially blocking read
while ((nBytesRead = aIS.read(aBuffer, 0, aBuffer.length)) > -1) {
aOS.write(aBuffer, 0, nBytesRead);
}
} catch (final FileUploadIOException ex) {
throw (FileUploadException) ex.getCause();
} catch (final IOException ex) {
throw new IOFileUploadException("Processing of " + RequestHelper.MULTIPART_FORM_DATA + " request failed. " + ex.getMessage(), ex);
}
if (aFileItem instanceof IFileItemHeadersSupport) {
final IFileItemHeaders aFileItemHeaders = aFileItemStream.getHeaders();
((IFileItemHeadersSupport) aFileItem).setHeaders(aFileItemHeaders);
}
}
bSuccessful = true;
return aItems;
} catch (final FileUploadIOException ex) {
throw (FileUploadException) ex.getCause();
} catch (final IOException ex) {
throw new FileUploadException(ex.getMessage(), ex);
} finally {
if (!bSuccessful) {
// Delete all file items
for (final IFileItem aFileItem : aItems) {
try {
aFileItem.delete();
} catch (final Exception ex) {
// ignore it
if (LOGGER.isErrorEnabled())
LOGGER.error("Failed to delete fileItem " + aFileItem, ex);
}
}
}
}
}
Aggregations