Example usage for org.springframework.web.multipart MultipartException getRootCause

List of usage examples for org.springframework.web.multipart MultipartException getRootCause

Introduction

In this page you can find the example usage for org.springframework.web.multipart MultipartException getRootCause.

Prototype

@Nullable
public Throwable getRootCause() 

Source Link

Document

Retrieve the innermost cause of this exception, if any.

Usage

From source file:com.devnexus.ting.web.controller.SiteController.java

@RequestMapping("/s/handleGlobaleErrors")
public String onUploadError(HttpServletRequest request, final Model model,
        RedirectAttributes redirectAttributes) {

    final Object errorExceptionObject = request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE);

    if (errorExceptionObject instanceof MultipartException) {
        final MultipartException multipartException = (MultipartException) errorExceptionObject;
        final Throwable rootCause = multipartException.getRootCause();
        String errorMessage = "";
        if (rootCause instanceof SizeLimitExceededException
                || rootCause instanceof FileSizeLimitExceededException) {
            errorMessage = String.format(
                    "The file that you are trying to upload is unfortunately too big. Uploaded files cannot be bigger than <strong>%s</strong>."
                            + " Please go back and upload a smaller image file.",
                    multipartProperties.getMaxFileSize());
        } else {//from  w  ww .  ja  va2  s  .  c  om
            errorMessage = (String) request.getAttribute(WebUtils.ERROR_MESSAGE_ATTRIBUTE);
        }
        redirectAttributes.addFlashAttribute("error", errorMessage);
        return "redirect:/s/uploadError";
    } else {
        return "error/error";
    }
}