How to handle MultipartException in Spring

人走茶凉 提交于 2019-12-04 09:35:58

It sounds like multiple requests. According to the log the MultipartExceptionHandler is called when a Exception occurs.

Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException:

Maybe you can add a logger after protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain), set an break point here and check the request. It's possible that the request is null or something else because of

Could not parse multipart servlet request; ... the request was rejected because its size ...

and in this case UrlUtils.buildFullRequestUrl(request)will also throw a exception which is not catched.

public final class UrlUtils {
    public static String buildFullRequestUrl(HttpServletRequest r) {
         return buildFullRequestUrl(r.getScheme(), r.getServerName(), r.getServerPort(), r.getRequestURI(),
                 r.getQueryString());
    }
...

Try to set the redirect uri manually like response.sendRedirect(uri + "&error=size-limit"). This implies that the response must not be null.

Try to write a test for your file upload with mock requests and mock exception and assert the expected redirect uri.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!