Document directory
I believe many people will use the allowedtypes parameter to configure the file types allowed to be uploaded.
<param name="allowedTypes">image/png,image/bmp,image/jpg</param>
However, anyone who has used this parameter knows that allowedtypes is a "file type" instead of a "file extension name". What is the difference between the file type and the file extension name?
For example, the file type of an image with a suffix of BMP is image/BMP, And the Excel file with a suffix of XLS is application/vnd. MS-Excel ....
This variety of "file types" is annoying ....
I wonder whether the last file can be filtered out based on the suffix. Struts should be able to think of this when it is so popular.
Then open the struts file's last interceptor org. Apache. struts2.interceptor. fileuploadinterceptor and check the following code:
protected Set<String> allowedTypesSet = Collections.emptySet();protected Set<String> allowedExtensionsSet = Collections.emptySet();
When we see a allowedtypesset and an allowedextensionsset, it is easy to think that the former is used to store the parameter allowedtypes,
The latter is used to store the allowedextensions parameter. extension is used to extend and extend the parameters...
Therefore, we can boldly guess that the allowedextensions parameter is used to configure the "extension name of the file to be uploaded".
Let's take a look at the method acceptfile () in fileuploadinterceptor. This method is used to check whether the file can be uploaded according to the current configuration.
protected boolean acceptFile(Object action, File file, String filename, String contentType, String inputName, ValidationAware validation, Locale locale) { boolean fileIsAcceptable = false; // If it's null the upload failed if (file == null) { String errMsg = getTextMessage(action, "struts.messages.error.uploading", new Object[]{inputName}, locale); if (validation != null) { validation.addFieldError(inputName, errMsg); } LOG.warn(errMsg); } else if (maximumSize != null && maximumSize < file.length()) { String errMsg = getTextMessage(action, "struts.messages.error.file.too.large", new Object[]{inputName, filename, file.getName(), "" + file.length()}, locale); if (validation != null) { validation.addFieldError(inputName, errMsg); } LOG.warn(errMsg); } else if ((!allowedTypesSet.isEmpty()) && (!containsItem(allowedTypesSet, contentType))) { String errMsg = getTextMessage(action, "struts.messages.error.content.type.not.allowed", new Object[]{inputName, filename, file.getName(), contentType}, locale); if (validation != null) { validation.addFieldError(inputName, errMsg); } LOG.warn(errMsg); } else if ((!allowedExtensionsSet.isEmpty()) && (!hasAllowedExtension(allowedExtensionsSet, filename))) { String errMsg = getTextMessage(action, "struts.messages.error.file.extension.not.allowed", new Object[]{inputName, filename, file.getName(), contentType}, locale); if (validation != null) { validation.addFieldError(inputName, errMsg); } LOG.warn(errMsg); } else { fileIsAcceptable = true; } return fileIsAcceptable;}
Ignore the other items first. Check whether the second and second else if nodes use allowedtypesset and allowedextensionsset, as shown below:
} Else if (allowedtypesset is not empty & allowedtypesset does not contain the file type) {// Add error message ....} else if (allowedextensionsset is not blank & allowedextensionsset does not contain the extension name of the file) {// Add error message}
As shown in the code above, if we want to use the allowedextensions parameter to control the extension name of the uploaded file, the allowedtypes parameter cannot be configured.
Otherwise, if the allowedtypes parameter is configured, The allowedextensions parameter will no longer take effect.
Summary
With the struts file last function, we can use one of the "file type" and "file suffix" to control the type/Suffix of the uploaded file. However, allowedtypes has a higher priority than allowedextensions. If allowedtypes is configured, allowedextensions will no longer take effect. The following is a simple configuration of allowedextensions:
<! -- Upload files with the suffix PNG, BMP, JPG, Doc, and XLS --> <Param name = "allowedextensions"> PNG, BMP, JPG, Doc, and XLS </param>