![](/img/trans.png)
[英]Handling MaxUploadSizeExceededException in java spring boot
[英]Java/Spring MaxUploadSizeExceededException post parameters
我有一個帶有三個文本字段和一個文件上傳字段的表單。 當我遇到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 --------------------
我嘗試了這里建議的方法
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:多部分:max-request-size:-1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.