Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 910243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:00:42+00:00 2026-05-15T17:00:42+00:00

MaxUploadSizeExceededException exception appears when I upload a file whose size exceeds the maximum allowed.

  • 0

MaxUploadSizeExceededException exception appears when I upload a file whose size exceeds the maximum allowed. I want to show an error message when this exception appears (like a validation error message). How can I handle this exception to do something like this in Spring 3?

Thanks.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:00:43+00:00Added an answer on May 15, 2026 at 5:00 pm

    I finally figured out a solution that works using a HandlerExceptionResolver.

    Add multipart resolver to your Spring config:

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
       <!--  the maximum size of an uploaded file in bytes -->
       <!-- <property name="maxUploadSize" value="10000000"/> -->
       <property name="maxUploadSize" value="1000"/>
    </bean>   
    

    Model – UploadedFile.java:

    package com.mypkg.models;
    
    import org.springframework.web.multipart.commons.CommonsMultipartFile;
    
    public class UploadedFile
    {
        private String title;
    
        private CommonsMultipartFile fileData;
    
        public String getTitle()
        {
            return title;
        }
    
        public void setTitle(String title)
        {
            this.title = title;
        }
    
        public CommonsMultipartFile getFileData()
        {
            return fileData;
        }
    
        public void setFileData(CommonsMultipartFile fileData)
        {
            this.fileData = fileData;
        }
    
    }
    

    View – /upload.jsp:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
        <head>
            <title>Test File Upload</title>
        </head>
        <body>
            <h1>Select a file to upload</h1>
            <c:if test="${not empty errors}">
                <h2 style="color:red;">${errors}.</h2>
            </c:if>
            <form:form modelAttribute="uploadedFile" method="post" enctype="multipart/form-data" name="uploadedFileform" id="uploadedFileform">
                <table width="600" border="0" align="left" cellpadding="0" cellspacing="0" id="pdf_upload_form">
                    <tr>
                        <td width="180"><label class="title">Title:</label></td>
                        <td width="420"><form:input id="title" path="title" cssClass="areaInput" size="30" maxlength="128"/></td>
                    </tr>
                    <tr>
                        <td width="180"><label class="title">File:</label></td>
                        <td width="420"><form:input id="fileData" path="fileData" type="file" /></td>
                     </tr>
                     <tr>
                        <td width="180"></td>
                        <td width="420"><input type="submit" value="Upload File" /></td>
                     </tr>
                </table>
            </form:form>
        </body>
    </html>
    

    Controller – FileUploadController.java:
    package com.mypkg.controllers;

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.multipart.MaxUploadSizeExceededException;
    import org.springframework.web.servlet.HandlerExceptionResolver;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.mypkg.models.UploadedFile;
    
    @Controller
    public class FileUploadController  implements HandlerExceptionResolver
    {
        @RequestMapping(value = "/upload", method = RequestMethod.GET)
        public String getUploadForm(Model model)
        {
            model.addAttribute("uploadedFile", new UploadedFile());
            return "/upload";
        }
    
        @RequestMapping(value = "/upload", method = RequestMethod.POST)
        public String create(UploadedFile uploadedFile, BindingResult result)
        {
            // Do something with the file
            System.out.println("#########  File Uploaded with Title: " + uploadedFile.getTitle());
            System.out.println("#########  Creating local file: /var/test-file-upload/" + uploadedFile.getFileData().getOriginalFilename());
    
            try
            {
    
                InputStream in = uploadedFile.getFileData().getInputStream();
                FileOutputStream f = new FileOutputStream(
                        "/var/test-file-upload/" + uploadedFile.getFileData().getOriginalFilename());
                int ch = 0;
                while ((ch = in.read()) != -1)
                {
                    f.write(ch);
                }
                f.flush();
                f.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
    
            return "redirect:/";
        }
    
        /*** Trap Exceptions during the upload and show errors back in view form ***/
        public ModelAndView resolveException(HttpServletRequest request,
                HttpServletResponse response, Object handler, Exception exception)
        {        
            Map<String, Object> model = new HashMap<String, Object>();
            if (exception instanceof MaxUploadSizeExceededException)
            {
                model.put("errors", exception.getMessage());
            } else
            {
                model.put("errors", "Unexpected error: " + exception.getMessage());
            }
            model.put("uploadedFile", new UploadedFile());
            return new ModelAndView("/upload", model);
        }
    
    }
    
    ========================================================================
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I intercept and send custom error messages with file upload when file
I can't figure out how to handle more than one kind of exception by
I am having trouble with catching and gracefully handling commons fileupload's FileUploadBase.SizeLimitExceededException or spring's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.