8

As I have set maximum file upload limit,I am getting

org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 2097152 bytes 

error while uploading file.It is giving 500 error to my api,I should I handle this error and return response in JSON format not an errorpage as provided in ErrorController

I want to catch that exception and give JSON response not ErrorPage.

@RequestMapping(value="/save",method=RequestMethod.POST)
    public ResponseDTO<String> save(@ModelAttribute @Valid FileUploadSingleDTO fileUploadSingleDTO,BindingResult bindingResult)throws MaxUploadSizeExceededException
    {
        ResponseDTO<String> result=documentDetailsService.saveDocumentSyn(fileUploadSingleDTO, bindingResult);

        return result;

    }

DTO that accepts document as follows

public class FileUploadSingleDTO {
@NotNull
    private Integer documentName;

    private Integer documentVersion;

    @NotNull
    private MultipartFile file;
}
3
  • I want to catch that file size exceed exception..after large file is uploaded.My code works fine when I upload file size less than MaxUploadSize Commented Dec 9, 2015 at 11:31
  • What is your code to handle files upload? Commented Dec 9, 2015 at 19:31
  • You can also utilize MultipartHttpServletRequest instead of HttpServletRequest. Then to access the uploaded file(s), you only need to use MultipartFile file = request.getFile(request.getFileNames().next());, which you will need to wrap with an IOException. This keeps your DTO skinny. Commented Jan 5, 2016 at 22:02

2 Answers 2

18
+50

As par I know you can handle the multipart file exception by using this.

@ControllerAdvice
public class MyErrorController extends ResponseEntityExceptionHandler {

Logger logger = org.slf4j.LoggerFactory.getLogger(getClass());

@ExceptionHandler(MultipartException.class)
@ResponseBody
String handleFileException(HttpServletRequest request, Throwable ex) {
    //return your json insted this string.
    return "File upload error";
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

After digging into source code, I found that CommonsMultipartResolver is handling the FileSizeLimitExceededException exception and throwing throw new MultipartException("Could not parse multipart servlet request", ex); here ex is the FileSizeLimitExceededException, which is actually thrown by commons.fileupload.FileuploadBase class. So you cannot handle the FileSizeLimitExceededException. Use generic implementation file upload errors using MultipartException.
@RampelliSrinivas it will be very help full if you can explain the comment you wrote above a bit.
4

Add a special exception handler into your Controller:

@ExceptionHandler(FileSizeLimitExceededException.class)
public YourReturnType uploadedAFileTooLarge(FileSizeLimitExceededException e) {
    /*...*/
}

(If this does not work, you have to enable exception handling in your configuration. Normally Spring does this by default.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.