Java/Spring MaxUploadSizeExceededException post 参数

标签 java spring validation exception

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

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

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

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

我的 Controller 实现了 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");
  }
}

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

谢谢。

最佳答案

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

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

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

关于Java/Spring MaxUploadSizeExceededException post 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32711147/

相关文章:

java - 如何使用进程 ID 获取有关 linux 进程的更多详细信息?

Java 8 CompletableFuture 惰性计算控制

java - 密码随机生成器 : creates the same password

java - 如何为整个项目配置单个属性占位符

javascript - 将客户端特定的日期格式返回到服务器 MVC4

java - ElasticSearch SpringBoot+Spring Data : java. lang.IllegalStateException:在接口(interface)上找不到合适的构造函数

java - 黑客可以更改模型属性吗? Spring , java

spring - Tomcat - PermGen 空间异常

javascript - 如何在 Javascript 中验证日期期间是否与其他日期期间交叉?

javascript - 验证特定网址