// Sanity check that the request is a multi-part/form-data request. if (ServletFileUpload.isMultipartContent(request)) {
// Sanity check on request size. booleanrequestSizePermitted= isRequestSizePermitted(request);
// Interface with Commons FileUpload API // Using the Streaming API ServletFileUploadservletFileUpload=newServletFileUpload(); FileItemIteratori= servletFileUpload.getItemIterator(request);
// Iterate the file items while (i.hasNext()) { try { FileItemStreamitemStream= i.next();
// If the file item stream is a form field, delegate to the // field item stream handler if (itemStream.isFormField()) { processFileItemStreamAsFormField(itemStream); }
// Delegate the file item stream for a file field to the // file item stream handler, but delegation is skipped // if the requestSizePermitted check failed based on the // complete content-size of the request. else {
// prevent processing file field item if request size not allowed. // also warn user in the logs. if (!requestSizePermitted) { addFileSkippedError(itemStream.getName(), request); LOG.warn("Skipped stream '#0', request maximum size (#1) exceeded.", itemStream.getName(), maxSize); continue; }
privatevoidprocessFileItemStreamAsFileField(FileItemStream itemStream, String location) { // Skip file uploads that don't have a file name - meaning that no file was selected. if (itemStream.getName() == null || itemStream.getName().trim().length() < 1) { LOG.debug("No file has been uploaded for the field: {}", itemStream.getFieldName()); return; }