In struts2, there are two restrictions on file upload. One is struts. multipart. maxSize. If this parameter is not set, the default value under the core package of struts2. the properties file has the default size setting struts. multipart. maxSize = 2097152, that is, 2 M. this is the first line of struts2 file upload.
The second level is maximumSize in inteceptor. When the actual file size can pass the first level, maximumSize can intercept the inteceptor configured in different actions.
For example, maximumSize of inteceptorA in struts. multipart. maxSize = 50 M. actionA = 30 M. maximumSize of inteceptorB in actionB = 10 M.
Struts. multipart. maxSize = 50 m for inteceptorA, B will play the first role. And inteceptorA and inteceptorB can customize their respective maximumSize for interceptor based on their own business after passing the first level.
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.
Struts2.2 org. apache. commons. fileupload. FileUploadBase. java
/**
* Creates a new instance.
* @ Param ctx The request context.
* @ Throws FileUploadException An error occurred while
* Parsing the request.
* @ Throws IOException An I/O error occurred.
*/
FileItemIteratorImpl (RequestContext ctx)
Throws FileUploadException, IOException {
If (ctx = null ){
Throw new NullPointerException ("ctx parameter ");
}
String contentType = ctx. getContentType ();
If (null = contentType)
| (! ContentType. toLowerCase (). startsWith (MULTIPART ))){
Throw new InvalidContentTypeException (
"The request doesn' t contain"
+ MULTIPART_FORM_DATA
+ "Or"
+ MULTIPART_MIXED
+ "Stream, content type header is"
+ ContentType );
}
InputStream input = ctx. getInputStream ();
If (sizeMax> = 0 ){
Int requestSize = ctx. getContentLength ();
If (requestSize =-1 ){
Input = new LimitedInputStream (input, sizeMax ){
Protected void raiseError (long pSizeMax, long pCount)
Throws IOException {
FileUploadException ex =
New SizeLimitExceededException (
"The request was rejected because"
+ "Its size (" + pCount
+ ") Exceeds the configured maximum"
+ "(" + PSizeMax + ")",
PCount, pSizeMax );
Throw new FileUploadIOException (ex );
}
};
} Else {
/// Here is the problem ///////////////////////////////// ///
If (sizeMax> = 0 & requestSize> sizeMax ){
Throw new SizeLimitExceededException (
"The request was rejected because its size ("
+ RequestSize
+ ") Exceeds the configured maximum ("
+ SizeMax + ")",
RequestSize, sizeMax );
}
}
}
String charEncoding = headerEncoding;
If (charEncoding = null ){
CharEncoding = ctx. getCharacterEncoding ();
}
Boundary = getBoundary (contentType );
If (boundary = null ){
Throw new FileUploadException (
"The request was rejected because"
+ "No multipart boundary was found ");
}
Notifier = new MultipartStream. ProgressNotifier (listener,
Ctx. getContentLength ());
Multi = new MultipartStream (input, boundary, notifier );
Multi. setHeaderEncoding (charEncoding );
SkipPreamble = true;
FindNextItem ();
If InteceptorA uploads a 40 m real file, the interceptor InteceptorA will access the international information: struts. messages. error. file. too. 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 a runtime exception. 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 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.
This is not feasible, because this exception was captured before the file was uploaded, the file was not uploaded, and params is also injected, so it is best to redirect to a jsp tutorial file, prompting that the upload failed, then rewrite and fill in the corresponding information.
Solution: You 'd better jump to a page with a special error not to return to the operation page.
Bytes -------------------------------------------------------------------------------------------
Note that the interceptor's so-called overwrite with the same name is executed repeatedly. For example, defaultStack contains fileUpload and token. if <interceptor-ref name = "defaultStack"/> is placed on the display defined interceptor, the display defined interceptor is overwritten.
The following is the correct interceptor sequence:
<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>