Set the maximum size of the File Uploaded By struts2

Source: Internet
Author: User

Two restrictions on file upload in struts2: one isStruts. multipart. maxsizeIf this parameter is not set, the default. properties file under the core package of struts2 containsDefaultSet struts. multipart. maxsize = 2097152, that is, 2 m. This is uploaded by the struts2 file.The first pass.

 

Level 2Is In inteceptorMaximumsize. When the actual file size can pass the first pass, maximumsize can intercept the inteceptor configured in different actions.

 

For example, Struts. multipart. maxsize = 50 m

Maximumsize of inteceptora in Actiona = 30 m

Maximumsize of inteceptorb in Actionb = 10 m

 

Struts. multipart. maxsize = 50 m for inteceptora, B will play the first role.

Inteceptora and inteceptorb can use the first level to customize the maximumsize for each interceptor based on their own business.

 

If the actual file is larger than 50 MB. throw the request was rejected because its size (XXXX) exceeds the configured maximum (XXXX) exception, which cannot be internationalized because the message is thrown by the commons-fileupload component, it does not support internationalization.

 

For the source code, see struts2.2 org. Apache. commons. fileupload. fileuploadbase. java.

 

If inteceptora uploads a 40 m real file

At this time, the interceptor inteceptora will access the international information: struts. messages. error. file. too. the value of larges. inteceptora is successfully uploaded only when the file is uploaded <= 30 m.

 

 

The following is a solution to the problem of unfriendly information prompted by struts. multipart. maxsize.

When the value exceeds 50 MB, The. commons-fileupload throws an exception during running.

Struts2 will view this exception as an action-level exception.

The request was rejected because its size (XXXX) exceeds the configured maximum (XXXX) is written into actionerror.

What we need to do is to overwrite the addactionerror method in the action.

 

@ Override
Public void addactionerror (string anerrormessage)

{
// Change the value from Internationalization
If (anerrormessage. startswith ("the request was rejected because its size "))

{
Super. addactionerror (gettext ("struts. multipart. maxsize. Limit "));
}

Else

{
Super. addactionerror (anerrormessage );
}
}

 

Corresponding configuration file

 

Struts. multipart. maxsize. Limit = the maximum size of files uploaded by the system is 50 MB.
Struts. Messages. Error. file. Too. larges = the maximum size of files uploaded by batch for new advertisements is 5 MB.

Struts. Messages. Error. content. type. Not. Allowed = the format of the uploaded file currently only supports the xls Format
Struts. Messages. Error. Uploading = File Upload Failed
Struts. Messages. invalid. Token = You have already submitted the form. Please do not submit it again.
Fileupload. filenums. exceed = more than five files are running. Please try again later.
Filedownload. Rows. exceed = because there are too many ads in the AD group you selected, please download them by group
Accountnotexist = Customer does not exist
Invalidtask = Invalid task

 

Note: Because the inteceptor returns midway through, other text content entered on the original page is also lost, that is, Params injection fails.

Because this exception is captured before the file is uploaded, the file is not uploaded, and Params is injected, it is recommended to redirect to a JSP file, prompting that the upload fails, and then rewrite and enter the relevant information.

Solution: It is best to jump to a page that specifically displays errors without returning the operation page.

 

Note,The interceptor uses the same name to overwrite the configurations.For example, defaultstack contains fileupload and Token. If you put <Interceptor-ref name = "defaultstack"/> to the display definition interceptor, the display definition interceptor will be overwritten ..

 

<Action name = "batchmiadoperation! * "Method =" {1} "class =" com. *****. ****. Action. multiidea. batchad. batchmiadoperationaction ">
<Interceptor-ref name = "defaultstack"/>
<Interceptor-ref name = "fileupload">
<Param name = "maximumsize"> 5242880 </param>
<! --
<Param name = "allowedtypes">
Application/vnd. MS-Excel
</Param>
-->
</Interceptor-ref>
<Interceptor-ref name = "token">
<Param name = "excludemethods">
Init, search, updatebatchcpcmatch, batchexportmiad, downloadwhenerror
</Param>
</Interceptor-ref>
<Result name = "input">
/WEB-INF/JSP/multiidea/batchad/batchmiad. jsp
</Result>
<Result name = "success">
/WEB-INF/JSP/multiidea/batchad/batchmiad. jsp
</Result>
<Result name = "invalid. Token">
/WEB-INF/JSP/multiidea/batchad/batchmiad. jsp
</Result>
</Action>

 

 

The Struts. xml configuration is as follows:

<Constant name = "struts. multipart. maxsize" value = "9000000"/>

<Action name = "fileupload" class = "cn. timefly. strutstest. fileuploadaction">
<Result name = "success">/fileuploadresult. jsp </result>
<Result name = "input">/fileupload. jsp </result>
<Interceptor-ref name = "fileupload">
<Param name = "maximumsize"> 500000 </param>
<Param name = "allowedtypes"> application/vnd. MS-PowerPoint </param>
</Interceptor-ref>
<Interceptor-ref name = "defaultstack"/>
</Action>

 

<Action name = "fileupload" class = "cn. timefly. strutstest. fileuploadaction2">
<Result name = "success">/fileuploadresult. jsp </result>
<Result name = "input">/fileupload. jsp </result>
<Interceptor-ref name = "fileupload">
<Param name = "maximumsize"> 300000 </param>
<Param name = "allowedtypes"> application/vnd. MS-PowerPoint </param>
</Interceptor-ref>
<Interceptor-ref name = "defaultstack"/>
</Action>

 

Original post address: http://www.cnblogs.com/highriver/archive/2011/06/01/2065557.html

 

 

 

 

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.