我有这段代码(就像在 Spring 的引用中):
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
当用户尝试上传超过 100 kB 的文件时,将显示 HTTP 状态为 500 的服务器错误页面和堆栈跟踪。如何以最简单的方式避免这种情况?我想重定向到表单页面并显示我自己的错误消息。
最佳答案
假设它是 org.springframework.web.multipart.MaxUploadSizeExceededException
, 只需定义一个 error-page
在web.xml
如下:
<error-page>
<exception-type>org.springframework.web.multipart.MaxUploadSizeExceededException</exception-type>
<location>/upload-error.jsp</location>
</error-page>
请注意,这仅在您没有另一个 <error-page>
时有效其中包括ServletException
或其父类(super class)之一。否则你必须引入一些异常过滤器来解包并重新抛出 ServletException
的根本原因。 .
关于java - 如何使用 MaxUploadSizeExceededException 避免堆栈跟踪 HTTP 状态 500 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5090288/