Struts file Upload allowedtypes problem, annoying "Allow uploaded file types"

Source: Internet
Author: User

Struts file upload problem, I believe many people will use the Allowedtypes parameter to configure the file types allowed to upload, as follows.

[HTML]View Plaincopy
    1. <param name="Allowedtypes">
    2. Image/png,image/bmp,image/jpg
    3. </param>

However, people who have used this parameter know that Allowedtypes is " file type" instead of " file suffix name", what is the difference between file type and file suffix name?

Just as the file type of the picture with the suffix named BMP is image/bmp, the Excel file type with the suffix named xls is application/vnd.ms-excel, and so on ....

This variety of " file types", annoying people annoying ....

Guess if you can filter the last file based on the suffix name, the framework that struts is so prosperous should be able to think of this.

Then open the struts file the last time the Interceptor Org.apache.struts2.interceptor.FileUploadInterceptor look, found the following code:

[Java]View Plaincopy
    1. protected set<string> Allowedtypesset = Collections.emptyset ();
    2. protected set<string> Allowedextensionsset = Collections.emptyset ();


See a Allowedtypesset and a allowedextensionsset, it is easy to think that the former is used for storing parameters allowedtypes,

And the latter, nature is used for storing parameters allowedextensions, extension: Extended, extended ...

So, we can boldly guess that the allowedextensions parameter is used to configure the "Allow uploaded file suffix name".

Let's look at a method in Fileuploadinterceptor. Acceptfile (), this method is used to check whether the file is allowed to be uploaded according to the current configuration

[Java]View Plaincopy
  1. Protected Boolean acceptfile (Object action, file file, string filename, String contentType, String inputname, Validati Onaware validation, locale locale) {
  2. Boolean fileisacceptable = false;
  3. //If It ' s null the upload failed
  4. if (file = = null) {
  5. String errmsg = gettextmessage (action, "Struts.messages.error.uploading", new Object[]{inputname}, locale);
  6. if (validation! = null) {
  7. Validation.addfielderror (InputName, errmsg);
  8. }
  9. Log.warn (errmsg);
  10. } Else if (maximumsize! = null && maximumsize < File.length ()) {
  11. String errmsg = gettextmessage (action, "Struts.messages.error.file.too.large", new Object[]{inputname,  FileName, File.getname (), "" "+ File.length ()}, locale);
  12. if (validation! = null) {
  13. Validation.addfielderror (InputName, errmsg);
  14. }
  15. Log.warn (errmsg);
  16. } Else if ((!allowedtypesset.isempty ()) && (!containsitem (Allowedtypesset, ContentType))) {
  17. String errmsg = gettextmessage (action, "struts.messages.error.content.type.not.allowed", new object[]{  InputName, filename, File.getname (), contentType}, Locale);
  18. if (validation! = null) {
  19. Validation.addfielderror (InputName, errmsg);
  20. }
  21. Log.warn (errmsg);
  22. } Else if ((!allowedextensionsset.isempty ()) && (!hasallowedextension (allowedextensionsset, filename ))) {  
  23. String errmsg = gettextmessage (action, "struts.messages.error.file.extension.not.allowed", new object[]{  InputName, filename, File.getname (), contentType}, Locale);
  24. if (validation! = null) {
  25. Validation.addfielderror (InputName, errmsg);
  26. }
  27. Log.warn (errmsg);
  28. } Else {
  29. Fileisacceptable = true;
  30. }
  31. return fileisacceptable;
  32. }


Other first, first look at the 2nd, 3 else if node, respectively, is the use of Allowedtypesset and Allowedextensionsset, as follows

[Java]View Plaincopy
    1. } Else if (Allowedtypesset is not empty && Allowedtypesset does not contain the type of the file) {
    2. Add error message ....
    3. } Else if (Allowedextensionsset is not empty && Allowedextensionsset does not contain the suffix name of the file) {
    4. Add error message
    5. }

As you can see from the code above, if we want to use the Allowedextensions parameter to control the suffix name of the uploaded file, we cannot configure the Allowedtypes parameter.

Otherwise, if the Allowedtypes parameter is configured, then the Allowedextensions parameter will no longer work.

Summarize

Using the Struts file last feature, we can use one of the file type 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 the allowedextensions:

[HTML]View Plaincopy
      1. <!--allow files with the suffix named Png,bmp,jpg,doc,xls to be uploaded--
      2. <param name="allowedextensions">
      3. Png,bmp,jpg,doc,xls
      4. </param>

Struts file Upload allowedtypes problem, annoying "Allow uploaded file types"

Related Article

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.