Struts2 File upload--Limit file size and type

Source: Internet
Author: User

When files are uploaded, it is possible to limit the size and type of files.

Struts 2 supports the restriction of setting parameters directly on the FileUpload interceptor.

When referencing the FileUpload interceptor, you can specify 3 parameters (specify <param> child elements):

Allowedtypes

Specifies the type of file that is allowed to be uploaded, separated by commas if there are multiple types. Note: This is not an extension of the file, but a corresponding contenttype.

MaximumSize

Specifies the maximum number of bytes allowed to upload files.

Allowedextensions

Specifies the extension of the file that is allowed to be uploaded.

If the uploaded file does not meet the above parameters specified conditions, it will jump to a call input <result>, general input will refer back to the page before submission, that is, File upload page.

<packagename= "Default" namespace= "/" extends= "Struts-default" >

<actionname= "Upload" class= "cn.itcast.user.action.FileUploadAction"

method= "Upload" >

<!--The parameter value of the directory in which the uploaded files are passed to the Fileuploadaction-->

<paramname= "Uploadpath" >D:\</param>

<!--reference FileUpload Interceptor-->

<interceptor-refname= "FileUpload" >

<!--set the size of a single upload file, unit bytes-->

<paramname= "MaximumSize" >20000000</param>

<!--set a single upload file with the extension "JPG" and multiple extensions separated by ","-->

<paramname= "Allowedextensions" >.jpg</param>

<!--set the MIME type of a single upload file, separated by "," between multiple types-->

<paramname= "Allowedtypes" >image/jpeg</param>

</interceptor-ref>

<!--when the display declaration uses a specific interceptor, the 18 interceptors in the default interceptor stack do not work.

If you want the interceptor in the default interceptor stack to work, you need to display a declaration using the default interceptor stack-->

<interceptor-refname= "Defaultstack"/>

<resultname= "Input" >/jsp/upload.jsp</result>

<result>/jsp/success/upload_success.jsp</result>

</action>

</package>

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.