MaxUploadSizeExceededException

MaxUploadSizeExceededException

我有一个包含三个文本字段和一个文件上传字段的表单。
当我遇到MaxUploadSizeExceededException异常时,可以使用实现HandlerExceptionResolver的类进行处理。
我有我的自定义处理程序类

resolveException(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception exception){ ... }


我的问题是,我需要一种将一些变量传递给Exception处理程序(表单中其他字段的值)的方法,以便可以返回包含这些变量的ModelAndView。我不想重定向到错误页面,我想返回到表单,而不会丢失插入的值。

我还有一个“ Validator”,它可以验证其他字段并且可以工作,但是我不知道如何将其与MaxUploadSizeExceededException异常集成。

我的控制器实现HandlerExceptionResolver

@Override
public ModelAndView resolveException(HttpServletRequest request,
        HttpServletResponse response, Object handler, Exception exception)
{
    Map<String, Object> model = new HashMap<String, Object>();

    if (exception instanceof MaxUploadSizeExceededException)
    {
        // this is empty!
        Map<String,String[]> paramMap = request.getParameterMap();

        // model.put("ticketForm", new TicketForm());
        // ticketForm.setId();

        model.put("err", exception.getMessage());
        return new ModelAndView(inserisciticket", model);
    } else
    {
        model.put("err", "Unexpected error: " + exception.getMessage());
        return new ModelAndView("error", model);
    }
}


这是从窗体调用的函数:

@RequestMapping(value = "/inseriscinuovoticket", method = RequestMethod.POST)
@ResponseBody
public String inseriscinuovoticket(
        @RequestParam(value = "idArgomento", required = true, defaultValue = "") String idArgomento,
        @RequestParam(value = "oggetto", required = true, defaultValue = "") String oggetto,
        @RequestParam(value = "descrizione", required = true, defaultValue = "") String descrizione,
        @RequestParam(value = "fileuploaded", required = false) MultipartFile fileuploaded,
        @ModelAttribute("ticket") TicketForm ticketForm, BindingResult result, Model model, HttpServletRequest request,
        Locale locale) throws IOException  { .... }


你能帮助我吗?

-------------编辑2 --------------------

我尝试了建议的方法here

public class MultipartExceptionHandler extends OncePerRequestFilter {

@Override
protected void doFilterInternal(HttpServletRequest request,
        HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {
    try {
        filterChain.doFilter(request, response);
    } catch (MaxUploadSizeExceededException e) {
        handle(request, response, e);
    } catch (ServletException e) {
        if(e.getRootCause() instanceof MaxUploadSizeExceededException) {
            handle(request, response, (MaxUploadSizeExceededException) e.getRootCause());
        } else {
            throw e;
        }
    }
}

private void handle(HttpServletRequest request,
        HttpServletResponse response, MaxUploadSizeExceededException e) throws ServletException, IOException {

    // null
    TicketForm t = (TicketForm)request.getAttribute("ticket");
    // null
    String idArgomento = (String)request.getAttribute("idArgomento");

    response.sendRedirect("inserisciticket");
  }
}


但是在句柄和过滤器中,我也无法读取表单参数(发布数据)。
我能怎么做???

谢谢。

最佳答案

以下更改解决了我的应用程序中的fileUpload大小问题(MaxUploadSizeExceededException)。
在“ application.yml”中进行以下更改以解决此问题。设置-1表示允许大小不受限制。

弹簧:
   Servlet:
      多部分:
          最大文件大小:-1

弹簧:
   Servlet:
      多部分:
          最大请求大小:-1

10-08 09:10