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
- <? Xml version = "1.0" encoding = "GBK"?>
- <! DOCTYPE struts PUBLIC
- "-// Apache Software Foundation // DTD Struts Configuration 2.1.7 // EN"
- Http://struts.apache.org/dtds/struts-2.1.7.dtd>
- <Struts>
- <Constant name = "struts. custom. i18n. resources" value = "mess"/>
- <! -- Set the decoding set used by the Application -->
- <Constant name = "struts. i18n. encoding" value = "GBK"/>
- <Package name = "lee" extends = "struts-default">
- <! -- Configure the Action for processing file uploads -->
- <Action name = "uploadPro" class = "org. crazyit. app. action. UploadAction">
- <! -- Configure the interceptor of fileUpload -->
- <Interceptor-ref name = "fileUpload">
- <! -- Configure the file types that can be uploaded -->
- <Param name = "allowedTypes"> image/png
- , Image/gif, image/jpeg </param>
- <! -- Configure the size of the file that can be uploaded -->
- <Param name = "maximumSize"> 2000 </param>
- </Interceptor-ref>
- <! -- Configure the default interceptor -->
- <Interceptor-ref name = "defaultStack"/>
- <! -- Dynamically set the attribute value of an Action -->
- <Param name = "savePath">/uploadFiles </param>
- <! -- Configure the default view page of Struts 2 -->
- <Result>/WEB-INF/content/succ. jsp </result>
- <Result name = "input">/WEB-INF/content/upload. jsp </result>
- </Action>
- <Action name = "*">
- <Result>/WEB-INF/content/{1}. jsp </result>
- </Action>
- </Package>
- </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