【问题标题】:Java/Spring MaxUploadSizeExceededException post parametersJava/Spring MaxUploadSizeExceededException 发布参数
【发布时间】:2015-09-22 07:42:45
【问题描述】:

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

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

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

我还有一个“验证器”,它可以验证其他字段并且它可以工作,但我不知道如何将它与 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");
  }
}

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

谢谢。

【问题讨论】:

  • 为什么不想从HttpServlerRequest获取参数?
  • 我试过了,但什么也看不到。使用 request.getParameterMap() 我得到一个空地图。
  • 请添加您尝试过的代码。
  • 我把代码贴下来了
  • 请编辑您的问题并将其添加到那里。

标签: java spring validation exception


【解决方案1】:

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

春天: 小服务程序: 多部分: 最大文件大小:-1

春天: 小服务程序: 多部分: 最大请求大小:-1

【讨论】:

    猜你喜欢
    • 2012-02-18
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 2015-04-25
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    相关资源
    最近更新 更多