将 Spring 3 @ExceptionHandler 与公共 FileUpload 和 SizeLimitExceededException/MaxUploadSizeExceededException 结合使用
上传大文件时,我无法捕获并优雅地处理 commons fileupload 的 FileUploadBase.SizeLimitExceededException
或 spring 的 MaxUploadSizeExceededException
。
据我所知,这些异常是在数据绑定期间、实际到达控制器之前引发的,因此导致 500 并且不调用异常处理程序方法。以前有人遇到过这种情况吗?正确处理这些异常的最佳方法是什么?
I am having trouble with catching and gracefully handling commons fileupload's FileUploadBase.SizeLimitExceededException
or spring's MaxUploadSizeExceededException
when uploading large files.
From what I can tell these exceptions are thrown during data binding, before the controller is actually reached, therefore resulting in a 500 and no calling of the exception handler method. Has anyone come across this before, and what is the best way for handling these exceptions properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢 thetoolman 提供了这个简单的解决方案。我把它延长了一点。我想保持文件处理不变并将异常传输到控制器。
并且在控制器中,
弹簧配置保持不变。将异常传输到验证器真是太好了,但我还没有弄清楚如何做到这一点。
thanks to thetoolman for this simple solution. I extended it a bit. I wanted to leave the file handling untouched and transport the Exception to the Controller.
and in the controller
the spring config remains the same. It would be really nice to have the exception transported to the validator, but I haven't figured out how to do this yet.
我知道这已经很旧了,但我也在寻找解决方案,但找不到任何东西。我们正在使用 Spring 提供 RESTful 服务,并且正在进行文件上传,但不知道如何处理这个问题。我提出了以下内容,希望它对某人有用:
我们所有的异常都是通过注释处理的,因此我们的错误处理程序解析器设置如下:
然后是一个可以处理异常的公共类
然后我们对公共类进行子类化多部分解析器并实现 HandlerExceptionResolver 接口
I know this is old, but I was looking for a solution to this as well and could not find anything. We are providing RESTful services using Spring and we are doing file upload and were not sure how to handle this. I came up with the following and hopefully it will be useful to someone:
All our exceptions are handled with annotations, so we have our error handler resolver set-up like this:
Then a common class that can handle the exception
Then we subclass the commons multipart resolver and implement the HandlerExceptionResolver interface
这似乎是一个很常见的问题。我遇到过类似的问题,也有人提出过类似的问题,例如,请参阅 这个问题。我还没有看到一个很好的解决方案。您可以使用普通 Servlet 过滤器来处理这些异常,但这会重复您的错误处理,因为您已经有一个 ExceptionHandler。
This seems to be a quite common problem. I've had similar problems and similar questions have been asked, see for example this question. I have yet to see a nice solution to the problem. You could use a vanilla servlet filter to handle these exceptions, but that will duplicate your error handling since you already have an ExceptionHandler.