最近在学struts2 ,在上传文件时发现有个异常不好处理,经过google终于有点小眉目,现浅谈下这个异常处理。有不妥当的地方还请指教
http://blog.csdn.net/dxswzj
在struts2中我们上传文件大于struts.multipart.maxSize设置的值时会抛出the request was rejected because its size (XXXX) exceeds the configured maximum (XXXX)异常,他是不能被国际化的,这信息对应用户来说是非常不友好的,那任何处理?
在struts2.2 中有两个地方设置上传文件大小:
一个是在拦截器fileUpload中设置
- <interceptor-ref name="fileUpload">
- <param name="maximumSize">1000000</param>
- </interceptor-ref>
一个是在struts2 自带的文件default.properties中设置的(系统默认大小2M)
struts.multipart.maxSize=2097152
这个可以在struts.propertise 文件中修改
那这两个有什么区别呢?
Struts2框架底层默认用的是apache的commons-fileupload组件对上传文件进行接受处理。struts.multipart.maxSize设置的大小就是该处理时取用的值,在上传文件之前系统会去比较文件的大小是否超过了该值,如果超过将抛出上述异常,commons-fileupload组件是不支持国际化的,所以我们看到的异常都是默认的。
fileUpload拦截器只是当文件上传到服务器上之后,才进行的文件类型和大小判断。 如果上传的文件大小刚好这 struts.multipart.maxSize与maximumSize 之间会抛出 key struts.messages.error.file.too.large 对应的异常信息,这个才支持国际化。那不是把struts.multipart.maxSize设很大,不就解决问题了吗?非也! fileUpload拦截器只是当文件上传到服务器上之后,才进行的文件类型和大小判断,这样会造成系统产生多余没用的文件。
那该如何是好?
其实解决的办法有多种,我这里介绍种最简单的方法:
在struts2.2 org.apache.commons.fileupload.FileUploadBase.java 中我们看到
-
-
-
-
-
-
-
- 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 a "
- + 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 {
-
- 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();
- }
实际上他是把该异常信息设置为Action级别的错误信息。
了解完这个就好办了,我们可以在action中直接重写ActionSupport的addActionError()方法,
-
-
-
-
-
-
-
-
- @Override
-
- public void addActionError(String anErrorMessage) {
-
-
-
- if (anErrorMessage.startsWith("the request was rejected because its size")) {
-
- Matcher m = Pattern.compile("//d+").matcher(anErrorMessage);
-
- String s1 = "";
-
- if (m.find()) s1 = m.group();
-
- String s2 = "";
-
- if (m.find()) s2 = m.group();
-
-
- super.addActionError("你上传的文件大小(" + s1 + ")超过允许的大小(" + s2 + ")");
-
-
-
- } else {
-
- super.addActionError(anErrorMessage);
- }
-
- }
这时这页面增加
就可以了。
做到这里还有个问题,就是原来页面上输入的其他文本内容也都不见了,也就是说params注入失败。
这个是没办法的,因为这个异常是在文件上传之前捕获的,文件未上传,同时params也为注入,所以这时最好重定向到一个jsp文件,提示上传失败,然后重写填写相应信息。
分享到:
相关推荐
the request was rejected because its size (16272982) exceeds the configured maximum (9000000) 2.fileUpload拦截器的maximumSize属性必须小于struts.multipart.maxSize的值。 struts.multipart.maxSize默认2M...
Git-2.37.1,windows64位用。好用的版本。
if (anErrorMessage.startsWith("the request was rejected because its size")) { Matcher m = Pattern.compile("\\d+").matcher(anErrorMessage); String s1 = ""; if (m.find()) { s1 = m.group(); } ...
org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException:the request was rejected because its size (66704562) exceeds the configured maximum (52428800) ``` 此问题的发生是由于...
在使用IntelliJ IDEA(简称Idea)上传代码到GitHub时,可能会遇到“Push rejected: Push to origin/master was rejected”的错误。这个问题通常发生在你尝试将一个新的项目推送到一个已存在文件的GitHub仓库时。以下...
The vision is “To produce the best UNIX-like operating system package possible, with due respect to the original software tools ideology as well as usability, performance and stability.” The ...
version of Delphi and C++ Builder has its own package; DragDropD6.dpk for Delphi 6, DragDropD5.dpk for Delphi 5, DragDropC5.bpk for C++ Builder 5, etc. 4) Add the Drag and Drop Component Suite ...
The training was fantastic, and all the concepts that were swirling around in their minds prior to the trip suddenly became very real. However, they were the only two ...
The Server is not licensed for this operation.Connection rejected, the server license allows connections from only 5 unique IP addresses. 没有授权 提示5个IP访问的限制 解决办法: 从licensecodes.oracle...
4. A) The woman rejected the man's apology. B) The woman appreciated the man's offer. C) The man had forgotten the whole thing. D) The man had hurt the woman's feelings. 5. A) The woman is ...
Q: Error while loading Word document: “Call was rejected by callee”, “Command failed”, “The RPC server is unavailable”, etc. A: Connection to Microsoft Word may be blocked by the anti-virus ...
The following is a detailed exploration of the key concepts covered in the document, including the history and significance of decompilers, the vulnerabilities of virtual machines, and strategies for...
3. "He is very likely to be rejected because of his bad eyesight." 这句话展示了条件句的表达,"be likely to do"(可能做某事)和原因状语。 4. "The committee members have conflicting opinions as to the ...
3. "It is very likely that he will be rejected by the army because of his bad eyesight." "It is likely that..."的句型用于表达可能性,"because of"引导原因。 4. "The mittee members have conflicting ...
通过监听`Intent.SMS_REJECTED`,它可以提供一种方式来对短信发送失败的情况做出反应,从而增强应用的功能性和用户体验。在实际应用中,我们可以根据需求定制具体的响应行为,比如提醒用户重新发送短信或提供其他...
1、依赖工具去官网下载node2、安装和构建如果报The operation was rejected by your operating system.使用安
例如:The absurd idea was rejected by the committee.(荒谬的想法被委员会否决了。) 3. abundant 丰富的 abundant是一个非常重要的词汇,它可以用来形容一些丰富或充足的情况。例如:The abundant rainfall ...