Struts file Upload allowedtypes problem, annoying "Allow uploaded file type" _ File Upload allowedtypes problem

Source: Internet
Author: User
Tags locale
Struts file upload problem, I believe many people will use the Allowedtypes parameter to configure the file types allowed to upload, as follows.
<param name= "Allowedtypes" >  
    image/png,image/bmp,image/jpg  
</param>  
However, people who have used this parameter know that Allowedtypes is "file type", not "file suffix name," and what is the difference between file type and file suffix name.
The file type for a picture with a suffix named BMP is image/bmp, the Excel file type named XLS is Application/vnd.ms-excel, and so on ....
The various "file types" are annoying ....
Suppose it is possible to filter for the last file based on the suffix name, a framework such as struts should be able to think of.
So he opened the struts file, the last interceptor org.apache.struts2.interceptor.FileUploadInterceptor a look, found the following code:
protected set<string> Allowedtypesset = Collections.emptyset ();  
protected set<string> Allowedextensionsset = Collections.emptyset ();  
To see a allowedtypesset and a allowedextensionsset, it is easy to think that the former is used for storing parameter allowedtypes,
And the latter, nature is used to store parameters allowedextensions, extension turn to: extension, expansion ...

So, we can boldly guess that the allowedextensions parameter is used to configure "Allow uploaded file suffix name".
Let's look at a method in Fileuploadinterceptor. Acceptfile (), which is used to check whether the file is allowed to be uploaded based on the current configuration
Protected Boolean acceptfile (Object action, file file, string filename, String contentType, String inputname, Validationaw  
  
    are validation, Locale Locale) {Boolean fileisacceptable = false; If it ' s null the upload failed if (file = = null) {String errmsg = Gettextmessage (Action, "Struts.messa  
        Ges.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 (actio N, "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 Obje  
        Ct[]{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;   }

Otherwise, first take a look at the 2nd, 3 else if nodes, respectively, using Allowedtypesset and Allowedextensionsset, as follows
else if (Allowedtypesset is not empty && Allowedtypesset does not contain the type of the file) {  
//Add error message ...  
} else if ( Allowedextensionsset is not empty && Allowedextensionsset does not contain a suffix name for this file) {  
//Add error message  
}  

As you can see from the code above, you cannot configure the Allowedtypes parameter if we want to use the Allowedextensions parameter to control the suffix name of the uploaded file.
Otherwise, if the Allowedtypes parameter is configured, then the Allowedextensions parameter will not be effective.
Summarize
Using the last feature of the struts file, we can use one of the "File types" and "file suffix names" to control the type/suffix name of the uploaded file. However, the priority level of allowedtypes is higher than allowedextensions, and if Allowedtypes is configured, Allowedextensions will no longer be effective. Finally, attach a simple configuration of allowedextensions:
<!--allow files with suffixes named Png,bmp,jpg,doc,xls to be uploaded-->     
<param name= "allowedextensions" >  
    Png,bmp,jpg,doc,xls  
</param>  

Original: http://blog.csdn.net/smcwwh/article/details/7349449


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.