FileDocCategorySizeDatePackage
JakartaMultiPartRequest.javaAPI DocExample10924Mon Jul 23 13:26:36 BST 2007org.apache.struts2.dispatcher.multipart

JakartaMultiPartRequest

public class JakartaMultiPartRequest extends Object implements MultiPartRequest
Multipart form data request adapter for Jakarta Commons Fileupload package.

Fields Summary
static final Log
log
private Map
files
private Map
params
private List
errors
private long
maxSize
Constructors Summary
Methods Summary
private org.apache.commons.fileupload.RequestContextcreateRequestContext(javax.servlet.http.HttpServletRequest req)
Creates a RequestContext needed by Jakarta Commons Upload.

param
req the request.
return
a new request context.

        return new RequestContext() {
            public String getCharacterEncoding() {
                return req.getCharacterEncoding();
            }

            public String getContentType() {
                return req.getContentType();
            }

            public int getContentLength() {
                return req.getContentLength();
            }

            public InputStream getInputStream() throws IOException {
                return req.getInputStream();
            }
        };
    
private java.lang.StringgetCanonicalName(java.lang.String filename)
Returns the canonical name of the given file.

param
filename the given file
return
the canonical name of the given file

        int forwardSlash = filename.lastIndexOf("/");
        int backwardSlash = filename.lastIndexOf("\\");
        if (forwardSlash != -1 && forwardSlash > backwardSlash) {
            filename = filename.substring(forwardSlash + 1, filename.length());
        } else if (backwardSlash != -1 && backwardSlash >= forwardSlash) {
            filename = filename.substring(backwardSlash + 1, filename.length());
        }

        return filename;
    
public java.lang.String[]getContentType(java.lang.String fieldName)

        List items = (List) files.get(fieldName);

        if (items == null) {
            return null;
        }

        List<String> contentTypes = new ArrayList<String>(items.size());
        for (int i = 0; i < items.size(); i++) {
            FileItem fileItem = (FileItem) items.get(i);
            contentTypes.add(fileItem.getContentType());
        }

        return (String[]) contentTypes.toArray(new String[contentTypes.size()]);
    
public java.util.ListgetErrors()

        return errors;
    
public java.io.File[]getFile(java.lang.String fieldName)

        List items = (List) files.get(fieldName);

        if (items == null) {
            return null;
        }

        List<File> fileList = new ArrayList<File>(items.size());
        for (int i = 0; i < items.size(); i++) {
            DiskFileItem fileItem = (DiskFileItem) items.get(i);
            fileList.add(fileItem.getStoreLocation());
        }

        return (File[]) fileList.toArray(new File[fileList.size()]);
    
public java.lang.String[]getFileNames(java.lang.String fieldName)

        List<FileItem> items = files.get(fieldName);

        if (items == null) {
            return null;
        }

        List<String> fileNames = new ArrayList<String>(items.size());
        for (int i = 0; i < items.size(); i++) {
            DiskFileItem fileItem = (DiskFileItem) items.get(i);
            fileNames.add(getCanonicalName(fileItem.getName()));
        }

        return (String[]) fileNames.toArray(new String[fileNames.size()]);
    
public java.util.EnumerationgetFileParameterNames()

        return Collections.enumeration(files.keySet());
    
public java.lang.String[]getFilesystemName(java.lang.String fieldName)

        List items = (List) files.get(fieldName);

        if (items == null) {
            return null;
        }

        List<String> fileNames = new ArrayList<String>(items.size());
        for (int i = 0; i < items.size(); i++) {
            DiskFileItem fileItem = (DiskFileItem) items.get(i);
            fileNames.add(fileItem.getStoreLocation().getName());
        }

        return (String[]) fileNames.toArray(new String[fileNames.size()]);
    
public java.lang.StringgetParameter(java.lang.String name)

        List v = (List) params.get(name);
        if (v != null && v.size() > 0) {
            return (String) v.get(0);
        }

        return null;
    
public java.util.EnumerationgetParameterNames()

        return Collections.enumeration(params.keySet());
    
public java.lang.String[]getParameterValues(java.lang.String name)

        List<String> v = params.get(name);
        if (v != null && v.size() > 0) {
            return (String[]) v.toArray(new String[v.size()]);
        }

        return null;
    
public voidparse(javax.servlet.http.HttpServletRequest servletRequest, java.lang.String saveDir)
Creates a new request wrapper to handle multi-part data using methods adapted from Jason Pell's multipart classes (see class description).

param
saveDir the directory to save off the file
param
servletRequest the request containing the multipart
throws
java.io.IOException is thrown if encoding fails.

        DiskFileItemFactory fac = new DiskFileItemFactory();
        // Make sure that the data is written to file
        fac.setSizeThreshold(0);
        if (saveDir != null) {
            fac.setRepository(new File(saveDir));
        }

        // Parse the request
        try {
            ServletFileUpload upload = new ServletFileUpload(fac);
            upload.setSizeMax(maxSize);
            List items = upload.parseRequest(createRequestContext(servletRequest));

            for (Object item1 : items) {
                FileItem item = (FileItem) item1;
                if (log.isDebugEnabled()) log.debug("Found item " + item.getFieldName());
                if (item.isFormField()) {
                    log.debug("Item is a normal form field");
                    List<String> values;
                    if (params.get(item.getFieldName()) != null) {
                        values = params.get(item.getFieldName());
                    } else {
                        values = new ArrayList<String>();
                    }

                    // note: see http://jira.opensymphony.com/browse/WW-633
                    // basically, in some cases the charset may be null, so
                    // we're just going to try to "other" method (no idea if this
                    // will work)
                    String charset = servletRequest.getCharacterEncoding();
                    if (charset != null) {
                        values.add(item.getString(charset));
                    } else {
                        values.add(item.getString());
                    }
                    params.put(item.getFieldName(), values);
                } else {
                    log.debug("Item is a file upload");

                    List<FileItem> values;
                    if (files.get(item.getFieldName()) != null) {
                        values = files.get(item.getFieldName());
                    } else {
                        values = new ArrayList<FileItem>();
                    }

                    values.add(item);
                    files.put(item.getFieldName(), values);
                }
            }
        } catch (FileUploadException e) {
            log.error(e);
            errors.add(e.getMessage());
        }
    
public voidsetMaxSize(java.lang.String maxSize)

    
    
        
        this.maxSize = Long.parseLong(maxSize);