FileDocCategorySizeDatePackage
FileUploadInterceptor.javaAPI DocExample14675Mon Jul 23 13:26:52 BST 2007org.apache.struts2.interceptor

FileUploadInterceptor

public class FileUploadInterceptor extends com.opensymphony.xwork2.interceptor.AbstractInterceptor
Interceptor that is based off of {@link MultiPartRequestWrapper}, which is automatically applied for any request that includes a file. It adds the following parameters, where [File Name] is the name given to the file uploaded by the HTML form:
  • [File Name] : File - the actual File
  • [File Name]ContentType : String - the content type of the file
  • [File Name]FileName : String - the actual name of the file uploaded (not the HTML name)

You can get access to these files by merely providing setters in your action that correspond to any of the three patterns above, such as setDocument(File document), setDocumentContentType(String contentType), etc.
See the example code section.

This interceptor will add several field errors, assuming that the action implements {@link ValidationAware}. These error messages are based on several i18n values stored in struts-messages.properties, a default i18n file processed for all i18n requests. You can override the text of these messages by providing text for the following keys:

  • struts.messages.error.uploading - a general error that occurs when the file could not be uploaded
  • struts.messages.error.file.too.large - occurs when the uploaded file is too large
  • struts.messages.error.content.type.not.allowed - occurs when the uploaded file does not match the expected content types specified

Interceptor parameters:

  • maximumSize (optional) - the maximum size (in bytes) that the interceptor will allow a file reference to be set on the action. Note, this is not related to the various properties found in struts.properties. Default to approximately 2MB.
  • allowedTypes (optional) - a comma separated list of content types (ie: text/html) that the interceptor will allow a file reference to be set on the action. If none is specified allow all types to be uploaded.

Extending the interceptor:

You can extend this interceptor and override the {@link #acceptFile} method to provide more control over which files are supported and which are not.

Example code:


<action name="doUpload" class="com.examples.UploadAction">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.ftl</result>
</action>
And then you need to set encoding multipart/form-data in the form where the user selects the file to upload.
<a:form action="doUpload" method="post" enctype="multipart/form-data">
<a:file name="upload" label="File"/>
<a:submit/>
</a:form>
And then in your action code you'll have access to the File object if you provide setters according to the naming convention documented in the start.
public com.examples.UploadAction implemements Action {
private File file;
private String contentType;
private String filename;

public void setUpload(File file) {
this.file = file;
}

public void setUploadContentType(String contentType) {
this.contentType = contentType;
}

public void setUploadFileName(String filename) {
this.filename = filename;
}

...
}

Fields Summary
private static final long
serialVersionUID
protected static final Log
log
private static final String
DEFAULT_DELIMITER
private static final String
DEFAULT_MESSAGE
protected Long
maximumSize
protected String
allowedTypes
protected Set
allowedTypesSet
Constructors Summary
Methods Summary
protected booleanacceptFile(java.io.File file, java.lang.String contentType, java.lang.String inputName, com.opensymphony.xwork2.ValidationAware validation, java.util.Locale locale)
Override for added functionality. Checks if the proposed file is acceptable based on contentType and size.

param
file - proposed upload file.
param
contentType - contentType of the file.
param
inputName - inputName of the file.
param
validation - Non-null ValidationAware if the action implements ValidationAware, allowing for better logging.
param
locale
return
true if the proposed file is acceptable by contentType and size.

        boolean fileIsAcceptable = false;

        // If it's null the upload failed
        if (file == null) {
            String errMsg = getTextMessage("struts.messages.error.uploading", new Object[]{inputName}, locale);
            if (validation != null) {
                validation.addFieldError(inputName, errMsg);
            }

            log.error(errMsg);
        } else if (maximumSize != null && maximumSize.longValue() < file.length()) {
            String errMsg = getTextMessage("struts.messages.error.file.too.large", new Object[]{inputName, file.getName(), "" + file.length()}, locale);
            if (validation != null) {
                validation.addFieldError(inputName, errMsg);
            }

            log.error(errMsg);
        } else if ((! allowedTypesSet.isEmpty()) && (!containsItem(allowedTypesSet, contentType))) {
            String errMsg = getTextMessage("struts.messages.error.content.type.not.allowed", new Object[]{inputName, file.getName(), contentType}, locale);
            if (validation != null) {
                validation.addFieldError(inputName, errMsg);
            }

            log.error(errMsg);
        } else {
            fileIsAcceptable = true;
        }

        return fileIsAcceptable;
    
private static booleancontainsItem(java.util.Collection itemCollection, java.lang.String key)

param
itemCollection - Collection of string items (all lowercase).
param
key - Key to search for.
return
true if itemCollection contains the key, false otherwise.

        return itemCollection.contains(key.toLowerCase());
    
private static java.util.SetgetDelimitedValues(java.lang.String delimitedString)

        Set<String> delimitedValues = new HashSet<String>();
        if (delimitedString != null) {
            StringTokenizer stringTokenizer = new StringTokenizer(delimitedString, DEFAULT_DELIMITER);
            while (stringTokenizer.hasMoreTokens()) {
                String nextToken = stringTokenizer.nextToken().toLowerCase().trim();
                if (nextToken.length() > 0) {
                    delimitedValues.add(nextToken);
                }
            }
        }
        return delimitedValues;
    
private java.lang.StringgetTextMessage(java.lang.String messageKey, java.lang.Object[] args, java.util.Locale locale)

        if (args == null || args.length == 0) {
            return LocalizedTextUtil.findText(this.getClass(), messageKey, locale);
        } else {
            return LocalizedTextUtil.findText(this.getClass(), messageKey, locale, DEFAULT_MESSAGE, args);
        }
    
public java.lang.Stringintercept(com.opensymphony.xwork2.ActionInvocation invocation)

        ActionContext ac = invocation.getInvocationContext();
        HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);

        if (!(request instanceof MultiPartRequestWrapper)) {
            if (log.isDebugEnabled()) {
                ActionProxy proxy = invocation.getProxy();
                log.debug(getTextMessage("struts.messages.bypass.request", new Object[]{proxy.getNamespace(), proxy.getActionName()}, ActionContext.getContext().getLocale()));
            }

            return invocation.invoke();
        }

        final Object action = invocation.getAction();
        ValidationAware validation = null;

        if (action instanceof ValidationAware) {
            validation = (ValidationAware) action;
        }

        MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) request;

        if (multiWrapper.hasErrors()) {
            for (Iterator errorIter = multiWrapper.getErrors().iterator(); errorIter.hasNext();) {
                String error = (String) errorIter.next();

                if (validation != null) {
                    validation.addActionError(error);
                }

                log.error(error);
            }
        }

        Map parameters = ac.getParameters();

        // Bind allowed Files
        Enumeration fileParameterNames = multiWrapper.getFileParameterNames();
        while (fileParameterNames != null && fileParameterNames.hasMoreElements()) {
            // get the value of this input tag
            String inputName = (String) fileParameterNames.nextElement();

            // get the content type
            String[] contentType = multiWrapper.getContentTypes(inputName);

            if (isNonEmpty(contentType)) {
                // get the name of the file from the input tag
                String[] fileName = multiWrapper.getFileNames(inputName);

                if (isNonEmpty(fileName)) {
                    // Get a File object for the uploaded File
                    File[] files = multiWrapper.getFiles(inputName);
                    if (files != null) {
                        for (int index = 0; index < files.length; index++) {

                            if (acceptFile(files[index], contentType[index], inputName, validation, ac.getLocale())) {
                                parameters.put(inputName, files);
                                parameters.put(inputName + "ContentType", contentType);
                                parameters.put(inputName + "FileName", fileName);
                            }
                        }
                    }
                } else {
                    log.error(getTextMessage("struts.messages.invalid.file", new Object[]{inputName}, ActionContext.getContext().getLocale()));
                }
            } else {
                log.error(getTextMessage("struts.messages.invalid.content.type", new Object[]{inputName}, ActionContext.getContext().getLocale()));
            }
        }

        // invoke action
        String result = invocation.invoke();

        // cleanup
        fileParameterNames = multiWrapper.getFileParameterNames();
        while (fileParameterNames != null && fileParameterNames.hasMoreElements()) {
            String inputValue = (String) fileParameterNames.nextElement();
            File[] file = multiWrapper.getFiles(inputValue);
            for (int index = 0; index < file.length; index++) {
                File currentFile = file[index];
                log.info(getTextMessage("struts.messages.removing.file", new Object[]{inputValue, currentFile}, ActionContext.getContext().getLocale()));

                if ((currentFile != null) && currentFile.isFile()) {
                    currentFile.delete();
                }
            }
        }

        return result;
    
private static booleanisNonEmpty(java.lang.Object[] objArray)

        boolean result = false;
        for (int index = 0; index < objArray.length && !result; index++) {
            if (objArray[index] != null) {
                result = true;
            }
        }
        return result;
    
public voidsetAllowedTypes(java.lang.String allowedTypes)
Sets the allowed mimetypes

param
allowedTypes A comma-delimited list of types


                    
        
        this.allowedTypes = allowedTypes;

        // set the allowedTypes as a collection for easier access later
        allowedTypesSet = getDelimitedValues(allowedTypes);
    
public voidsetMaximumSize(java.lang.Long maximumSize)
Sets the maximum size of an uploaded file

param
maximumSize The maximum size in bytes

        this.maximumSize = maximumSize;