Example usage for org.apache.commons.fileupload FileUpload setSizeMax

List of usage examples for org.apache.commons.fileupload FileUpload setSizeMax

Introduction

In this page you can find the example usage for org.apache.commons.fileupload FileUpload setSizeMax.

Prototype

public void setSizeMax(long sizeMax) 

Source Link

Document

Sets the maximum allowed upload size.

Usage

From source file:com.asual.summer.core.spring.RequestMultipartResolver.java

protected FileUpload prepareFileUpload(String encoding) {
    FileUpload fileUpload = getFileUpload();
    FileUpload actualFileUpload = fileUpload;
    if (encoding != null && !encoding.equals(fileUpload.getHeaderEncoding())) {
        actualFileUpload = newFileUpload(new StreamFileItemFactory());
        actualFileUpload.setSizeMax(fileUpload.getSizeMax());
        actualFileUpload.setHeaderEncoding(encoding);
    }//www .j ava 2  s . c o m
    return actualFileUpload;
}

From source file:net.formio.upload.AbstractMultipartRequestParser.java

/**
 * Convenience method for common configuration of {@link FileUpload}.
 * Can be called from {@link #parseRequest(FileItemFactory, long, long, String)} method
 * that must be implemented by subclasses.
 * @param upload//from  w  ww. j  a  v  a2  s .c o  m
 * @param singleFileSizeMax
 * @param totalSizeMax
 * @param defaultEncoding
 */
protected void configureUpload(final FileUpload upload, long singleFileSizeMax, long totalSizeMax,
        String defaultEncoding) {
    // set overall request size constraint
    // maximum allowed size of a single uploaded file
    upload.setFileSizeMax(singleFileSizeMax);
    // maximum allowed size of the whole request
    upload.setSizeMax(totalSizeMax);
    if (defaultEncoding != null) {
        upload.setHeaderEncoding(defaultEncoding);
    }
}

From source file:de.uhh.l2g.upload.MyCommonsFileUploadSupport.java

/**
 * Prepare file upload.//from ww w. jav  a 2 s  .  c o m
 *
 * @param encoding the encoding
 * @return the file upload
 */
@Override
protected FileUpload prepareFileUpload(String encoding) {
    FileUpload fileUpload = getFileUpload();
    FileUpload actualFileUpload = fileUpload;

    // Use new temporary FileUpload instance if the request specifies
    // its own encoding that does not match the default encoding.
    if (encoding != null && !encoding.equals(fileUpload.getHeaderEncoding())) {
        actualFileUpload = newFileUpload(getFileItemFactory());
        actualFileUpload.setSizeMax(fileUpload.getSizeMax());
        actualFileUpload.setHeaderEncoding(encoding);
    }

    return actualFileUpload;
}

From source file:com.aspectran.web.support.multipart.inmemory.MemoryMultipartFormDataParser.java

@Override
public void parse(RequestAdapter requestAdapter) {
    try {//w w  w. j a  va 2s  .c o  m
        MemoryFileItemFactory factory = new MemoryFileItemFactory();
        if (maxInMemorySize > -1) {
            factory.setSizeThreshold(maxInMemorySize);
        }

        FileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding(requestAdapter.getEncoding());
        if (maxRequestSize > -1) {
            upload.setSizeMax(maxRequestSize);
        }
        if (maxFileSize > -1) {
            upload.setFileSizeMax(maxFileSize);
        }

        Map<String, List<FileItem>> fileItemListMap;
        try {
            RequestContext requestContext = createRequestContext(requestAdapter.getAdaptee());
            fileItemListMap = upload.parseParameterMap(requestContext);
        } catch (SizeLimitExceededException e) {
            log.warn("Maximum request length exceeded; multipart.maxRequestSize: " + maxRequestSize);
            requestAdapter.setMaxLengthExceeded(true);
            return;
        } catch (FileSizeLimitExceededException e) {
            log.warn("Maximum file length exceeded; multipart.maxFileSize: " + maxFileSize);
            requestAdapter.setMaxLengthExceeded(true);
            return;
        }
        parseMultipartParameters(fileItemListMap, requestAdapter);
    } catch (Exception e) {
        throw new MultipartRequestParseException("Failed to parse multipart request; nested exception is " + e,
                e);
    }
}

From source file:com.aspectran.web.support.multipart.commons.CommonsMultipartFormDataParser.java

@Override
public void parse(RequestAdapter requestAdapter) {
    try {/* w ww.j a  va2s .  c o  m*/
        DiskFileItemFactory factory = new DiskFileItemFactory();
        if (maxInMemorySize > -1) {
            factory.setSizeThreshold(maxInMemorySize);
        }

        if (tempDirectoryPath != null) {
            File repository = new File(tempDirectoryPath);
            if (!repository.exists() && !repository.mkdirs()) {
                throw new IllegalArgumentException(
                        "Given tempDirectoryPath [" + tempDirectoryPath + "] could not be created");
            }
            factory.setRepository(repository);
        }

        FileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding(requestAdapter.getEncoding());
        if (maxRequestSize > -1L) {
            upload.setSizeMax(maxRequestSize);
        }
        if (maxFileSize > -1L) {
            upload.setFileSizeMax(maxFileSize);
        }

        Map<String, List<FileItem>> fileItemListMap;

        try {
            RequestContext requestContext = createRequestContext(requestAdapter.getAdaptee());
            fileItemListMap = upload.parseParameterMap(requestContext);
        } catch (FileUploadBase.SizeLimitExceededException e) {
            log.warn("Maximum request length exceeded; multipart.maxRequestSize: " + maxRequestSize);
            requestAdapter.setMaxLengthExceeded(true);
            return;
        } catch (FileUploadBase.FileSizeLimitExceededException e) {
            log.warn("Maximum file length exceeded; multipart.maxFileSize: " + maxFileSize);
            requestAdapter.setMaxLengthExceeded(true);
            return;
        }

        parseMultipartParameters(fileItemListMap, requestAdapter);
    } catch (Exception e) {
        throw new MultipartRequestParseException("Could not parse multipart servlet request", e);
    }
}

From source file:de.uhh.l2g.upload.MyCommonsPortletMultipartResolver.java

/**
 * Prepare file upload./* w  w w  .j ava  2  s  . c o  m*/
 *
 * @param encoding the encoding
 * @return the file upload
 */
@Override
protected FileUpload prepareFileUpload(String encoding) {
    FileUpload fileUpload = getFileUpload();
    FileUpload actualFileUpload = fileUpload;
    // Use new temporary FileUpload instance if the request specifies
    // its own encoding that does not match the default encoding.
    if (encoding != null && !encoding.equals(fileUpload.getHeaderEncoding())) {
        actualFileUpload = newFileUpload(getFileItemFactory());
        actualFileUpload.setSizeMax(fileUpload.getSizeMax());
        actualFileUpload.setHeaderEncoding(encoding);
    }

    return actualFileUpload;
}

From source file:cn.clxy.studio.common.web.multipart.GFileUploadSupport.java

/**
 * Determine an appropriate FileUpload instance for the given encoding.
 * <p>//from  w w w . j a  v  a 2  s. co m
 * Default implementation returns the shared FileUpload instance if the encoding matches, else creates a new
 * FileUpload instance with the same configuration other than the desired encoding.
 *
 * @param encoding the character encoding to use
 * @return an appropriate FileUpload instance.
 */
protected FileUpload prepareFileUpload(String encoding) {
    FileUpload fileUpload = getFileUpload();
    FileUpload actualFileUpload = fileUpload;

    // Use new temporary FileUpload instance if the request specifies
    // its own encoding that does not match the default encoding.
    if (encoding != null && !encoding.equals(fileUpload.getHeaderEncoding())) {
        actualFileUpload = newFileUpload(getFileItemFactory());
        actualFileUpload.setSizeMax(fileUpload.getSizeMax());
        actualFileUpload.setHeaderEncoding(encoding);
    }

    return actualFileUpload;
}

From source file:com.xpn.xwiki.plugin.fileupload.FileUploadPlugin.java

/**
 * Loads the list of uploaded files in the context if there are any uploaded files.
 * //ww w  .  j ava2  s.c o  m
 * @param uploadMaxSize Maximum size of the uploaded files.
 * @param uploadSizeThreashold Threashold over which the file data should be stored on disk, and not in memory.
 * @param tempdir Temporary directory to store the uploaded files that are not kept in memory.
 * @param context Context of the request.
 * @throws XWikiException if the request could not be parsed, or the maximum file size was reached.
 * @see FileUploadPluginApi#loadFileList(long, int, String)
 */
public void loadFileList(long uploadMaxSize, int uploadSizeThreashold, String tempdir, XWikiContext context)
        throws XWikiException {
    LOGGER.debug("Loading uploaded files");

    // If we already have a file list then loadFileList was already called
    // Continuing would empty the list.. We need to stop.
    if (context.get(FILE_LIST_KEY) != null) {
        LOGGER.debug("Called loadFileList twice");

        return;
    }

    // Get the FileUpload Data
    // Make sure the factory only ever creates file items which will be deleted when the jvm is stopped.
    DiskFileItemFactory factory = new DiskFileItemFactory() {
        public FileItem createItem(String fieldName, String contentType, boolean isFormField, String fileName) {
            try {
                final DiskFileItem item = (DiskFileItem) super.createItem(fieldName, contentType, isFormField,
                        fileName);
                // Needed to make sure the File object is created.
                item.getOutputStream();
                item.getStoreLocation().deleteOnExit();
                return item;
            } catch (IOException e) {
                String path = System.getProperty("java.io.tmpdir");
                if (super.getRepository() != null) {
                    path = super.getRepository().getPath();
                }
                throw new RuntimeException("Unable to create a temporary file for saving the attachment. "
                        + "Do you have write access on " + path + "?");
            }
        }
    };

    factory.setSizeThreshold(uploadSizeThreashold);

    if (tempdir != null) {
        File tempdirFile = new File(tempdir);
        if (tempdirFile.mkdirs() && tempdirFile.canWrite()) {
            factory.setRepository(tempdirFile);
        }
    }

    // TODO: Does this work in portlet mode, or we must use PortletFileUpload?
    FileUpload fileupload = new ServletFileUpload(factory);
    RequestContext reqContext = new ServletRequestContext(context.getRequest().getHttpServletRequest());
    fileupload.setSizeMax(uploadMaxSize);
    // context.put("fileupload", fileupload);

    try {
        @SuppressWarnings("unchecked")
        List<FileItem> list = fileupload.parseRequest(reqContext);
        if (list.size() > 0) {
            LOGGER.info("Loaded " + list.size() + " uploaded files");
        }
        // We store the file list in the context
        context.put(FILE_LIST_KEY, list);
    } catch (FileUploadBase.SizeLimitExceededException e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
                XWikiException.ERROR_XWIKI_APP_FILE_EXCEPTION_MAXSIZE, "Exception uploaded file");
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
                XWikiException.ERROR_XWIKI_APP_UPLOAD_PARSE_EXCEPTION, "Exception while parsing uploaded file",
                e);
    }
}

From source file:helma.servlet.AbstractServletClient.java

protected List parseUploads(ServletRequestContext reqcx, RequestTrans reqtrans, final UploadStatus uploadStatus,
        String encoding) throws FileUploadException, UnsupportedEncodingException {
    // handle file upload
    DiskFileItemFactory factory = new DiskFileItemFactory();
    FileUpload upload = new FileUpload(factory);
    // use upload limit for individual file size, but also set a limit on overall size
    upload.setFileSizeMax(uploadLimit * 1024);
    upload.setSizeMax(totalUploadLimit * 1024);

    // register upload tracker with user's session
    if (uploadStatus != null) {
        upload.setProgressListener(new ProgressListener() {
            public void update(long bytesRead, long contentLength, int itemsRead) {
                uploadStatus.update(bytesRead, contentLength, itemsRead);
            }/*from   w w  w . ja v  a2s  .c o  m*/
        });
    }

    List uploads = upload.parseRequest(reqcx);
    Iterator it = uploads.iterator();

    while (it.hasNext()) {
        FileItem item = (FileItem) it.next();
        String name = item.getFieldName();
        Object value;
        // check if this is an ordinary HTML form element or a file upload
        if (item.isFormField()) {
            value = item.getString(encoding);
        } else {
            value = new MimePart(item);
        }
        // if multiple values exist for this name, append to _array
        reqtrans.addPostParam(name, value);
    }
    return uploads;
}

From source file:axiom.servlet.AbstractServletClient.java

protected List parseUploads(ServletRequestContext reqcx, RequestTrans reqtrans, final UploadStatus uploadStatus,
        String encoding) throws FileUploadException, UnsupportedEncodingException {

    List uploads = null;/*www  .  j  a  v  a 2  s .c om*/
    Context cx = Context.enter();
    ImporterTopLevel scope = new ImporterTopLevel(cx, true);

    try {
        // handle file upload
        DiskFileItemFactory factory = new DiskFileItemFactory();
        FileUpload upload = new FileUpload(factory);
        // use upload limit for individual file size, but also set a limit on overall size
        upload.setFileSizeMax(uploadLimit * 1024);
        upload.setSizeMax(totalUploadLimit * 1024);

        // register upload tracker with user's session
        if (uploadStatus != null) {
            upload.setProgressListener(new ProgressListener() {
                public void update(long bytesRead, long contentLength, int itemsRead) {
                    uploadStatus.update(bytesRead, contentLength, itemsRead);
                }
            });
        }

        uploads = upload.parseRequest(reqcx);
        Iterator it = uploads.iterator();

        while (it.hasNext()) {
            FileItem item = (FileItem) it.next();
            String name = item.getFieldName();
            Object value = null;
            // check if this is an ordinary HTML form element or a file upload
            if (item.isFormField()) {
                value = item.getString(encoding);
            } else {
                String itemName = item.getName().trim();
                if (itemName == null || itemName.equals("")) {
                    continue;
                }
                value = new MimePart(itemName, item.get(), item.getContentType());
                value = new NativeJavaObject(scope, value, value.getClass());
            }
            item.delete();
            // if multiple values exist for this name, append to _array

            // reqtrans.addPostParam(name, value); ????

            Object ret = reqtrans.get(name);
            if (ret != null && ret != Scriptable.NOT_FOUND) {
                appendFormValue(reqtrans, name, value);
            } else {
                reqtrans.set(name, value);
            }
        }
    } finally {
        Context.exit();
    }

    return uploads;
}