The interceptor implements file filtering.

Source: Internet
Author: User

The preceding method of manual file filtering is simple, but after all, a large amount of filtering code needs to be written, which is not conducive to high-level deconstruction of the program and complicated development.

Struts 2 provides a file upload Interceptor. By configuring this interceptor, you can easily filter files. In Struts 2, the file upload interceptor is fileUpload. To enable this Interceptor to take effect, you only need to configure the interceptor reference in this Action.

You can specify two parameters for the fileUpload interceptor.

AllowedTypes: Specifies the file types that can be uploaded. Multiple file types are separated by commas.

Ø maximumSize: Specifies the size of the file to be uploaded, in bytes.

By configuring the fileUpload interceptor, you can easily implement file filtering. When file filtering fails, the system automatically transfers the file to the input logic view. Therefore, you must configure the logic view named input for this Action. In addition, you must explicitly configure the ultstack interceptor reference for this Action.

The configuration file used by the Interceptor to implement file filtering is as follows.

Program list: codes \ 04 \ 4.3 \ autoFilter \ WEB-INF \ src \ struts. xml

 
 
  1. <? Xml version = "1.0" encoding = "GBK"?>
  2. <! DOCTYPE struts PUBLIC
  3. "-// Apache Software Foundation // DTD Struts Configuration 2.1.7 // EN"
  4. Http://struts.apache.org/dtds/struts-2.1.7.dtd>
  5. <Struts>
  6. <Constant name = "struts. custom. i18n. resources" value = "mess"/>
  7. <! -- Set the decoding set used by the Application -->
  8. <Constant name = "struts. i18n. encoding" value = "GBK"/>
  9. <Package name = "lee" extends = "struts-default">
  10. <! -- Configure the Action for processing file uploads -->
  11. <Action name = "uploadPro" class = "org. crazyit. app. action. UploadAction">
  12. <! -- Configure the interceptor of fileUpload -->
  13. <Interceptor-ref name = "fileUpload">
  14. <! -- Configure the file types that can be uploaded -->
  15. <Param name = "allowedTypes"> image/png
  16. , Image/gif, image/jpeg </param>
  17. <! -- Configure the size of the file that can be uploaded -->
  18. <Param name = "maximumSize"> 2000 </param>
  19. </Interceptor-ref>
  20. <! -- Configure the default interceptor -->
  21. <Interceptor-ref name = "defaultStack"/>
  22. <! -- Dynamically set the attribute value of an Action -->
  23. <Param name = "savePath">/uploadFiles </param>
  24. <! -- Configure the default view page of Struts 2 -->
  25. <Result>/WEB-INF/content/succ. jsp </result>
  26. <Result name = "input">/WEB-INF/content/upload. jsp </result>
  27. </Action>
  28. <Action name = "*">
  29. <Result>/WEB-INF/content/{1}. jsp </result>
  30. </Action>
  31. </Package>
  32. </Struts>

The above interceptor filters not only the file type, but also the file size. The type of the uploaded file can only be an image file, and the file size cannot exceed 2000 bytes. Of course, we can change it to a larger size at any time ). If the file we upload is too large, the system will go to the input logic view, that is, the/WEB-INF/content/upload. jsp page.

Note: If you need to use the file upload Interceptor to filter the file size or content, you must display the default interceptor Stack: defaultStack referenced by Struts. The fileUpload interceptor must be configured before the ultstack interceptor stack.

This article from the "crazy Java Li Gang" blog, please be sure to keep this source http://javaligang.blog.51cto.com/5026500/890915

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.