Example usage for org.apache.commons.fileupload DiskFileUpload parseRequest

List of usage examples for org.apache.commons.fileupload DiskFileUpload parseRequest

Introduction

In this page you can find the example usage for org.apache.commons.fileupload DiskFileUpload parseRequest.

Prototype

public List  parseRequest(HttpServletRequest req, int sizeThreshold, long sizeMax, String path)
        throws FileUploadException 

Source Link

Document

Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> compliant <code>multipart/form-data</code> stream.

Usage

From source file:org.apache.tapestry.multipart.DefaultMultipartDecoder.java

/**
 * Decodes the request, storing the part map (keyed on query parameter name, 
 * value is {@link IPart} into the request as an attribute.
 * /*from   w  w w. j ava 2  s.c  om*/
 * @throws ApplicationRuntimeException if decode fails, for instance the
 * request exceeds getMaxSize()
 * 
 **/

public void decode(HttpServletRequest request) {
    Map partMap = new HashMap();

    request.setAttribute(PART_MAP_ATTRIBUTE_NAME, partMap);

    // The encoding that will be used to decode the string parameters
    // It should NOT be null at this point, but it may be 
    // if the older Servlet API 2.2 is used
    String encoding = request.getCharacterEncoding();

    // DiskFileUpload is not quite threadsafe, so we create a new instance
    // for each request.

    DiskFileUpload upload = new DiskFileUpload();

    List parts = null;

    try {
        if (encoding != null)
            upload.setHeaderEncoding(encoding);
        parts = upload.parseRequest(request, _thresholdSize, _maxSize, _repositoryPath);
    } catch (FileUploadException ex) {
        throw new ApplicationRuntimeException(
                Tapestry.format("DefaultMultipartDecoder.unable-to-decode", ex.getMessage()), ex);
    }

    int count = Tapestry.size(parts);

    for (int i = 0; i < count; i++) {
        FileItem uploadItem = (FileItem) parts.get(i);

        if (uploadItem.isFormField()) {
            try {
                String name = uploadItem.getFieldName();
                String value;
                if (encoding == null)
                    value = uploadItem.getString();
                else
                    value = uploadItem.getString(encoding);

                ValuePart valuePart = (ValuePart) partMap.get(name);
                if (valuePart != null) {
                    valuePart.add(value);
                } else {
                    valuePart = new ValuePart(value);
                    partMap.put(name, valuePart);
                }
            } catch (UnsupportedEncodingException ex) {
                throw new ApplicationRuntimeException(Tapestry.format("illegal-encoding", encoding), ex);
            }
        } else {
            UploadPart uploadPart = new UploadPart(uploadItem);

            partMap.put(uploadItem.getFieldName(), uploadPart);
        }
    }

}