Good day!

Tell me how to organize a check on the size of the file uploaded to the server. On the servlet I set the limit in the annotation:

@MultipartConfig(maxFileSize = 2097152) 

Actually the question is how to organize a check for the size of the uploaded file on the server if the file is larger than the allowed size?

I check the file like this:

 Part part = null; try{ part = request.getPart("photo"); }catch(Exception e){ LOG.error("Error in getPart"); } 

But when loading a file, the size is larger than the limit, no exceptions are thrown, but ERR_CONNECTION_ABORTED takes off, i.e. The server did not fully read the user's request. I use the Tomcat 7 server.

I will be glad to any advice!

    2 answers 2

    If the file size exceeds the limit specified in @MultipartConfig(maxFileSize) , an exception will be thrown when accessing the HttpServletRequest.getParts() method.

    For example, when deploying a servlet running on Wildfly 10 application server, an exception looks like this:

     java.lang.RuntimeException: java.io.IOException: UT000054: The maximum size 10485760 for an individual file in a multipart request was exceeded ... Caused by: java.io.IOException: UT000054: The maximum size 10485760 for an individual file in a multipart request was exceeded 

    If your task is simply to prevent the download of large files, then additionally do not have to do anything, because the process will be aborted on line

     Part part = request.getPart("photo") 

    and will not reach to save to the database. If additional processing is required when retrieving large files, getPart should be getPart in try/catch

    UPDATE

    When you deploy an application running Tomcat 7, the thrown exception looks like this:

     java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field binaryFile exceeds its maximum permitted size of 2097152 bytes. ... Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field binaryFile exceeds its maximum permitted size of 2097152 bytes. 

    Here is the simplest example of code that catches an exception and redirects the user to the error page:

     @WebServlet("/file") @MultipartConfig( fileSizeThreshold = 1024 * 1024 * 2, maxFileSize = 1024 * 1024 * 2, maxRequestSize = 1024 * 1024 * 2) public class FilesServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { for (Part part : req.getParts()) { //работа с полученными файлами } } catch (Exception e) { e.printStackTrace(); if (e.getCause() != null && e.getCause() instanceof FileUploadBase.FileSizeLimitExceededException) { resp.sendRedirect(getServletContext().getContextPath() + "/fileSizeError.html"); } } } } 
    • I do additional processing to notify the user that the file is more than the required one, and so, when loading a large file ERR_CONNECTION_ABORTED takes off, i.e. The server did not fully read the user's request. Tell me how to handle this error? I use Tomcat 7 server. - Roman
    • If you catch an exception in a catch block, it should not be thrown on the client side. In the catch block, you can implement any required logic, for example, redirecting to the error page - resp.sendRedirect (getServletContext (). GetContextPath () + "/errormMaxUploadSize.html"), or perform other manipulations with the response. What exactly will be more convenient to do depends largely on how the client side is implemented (for example, which framework is used). The specifics of the client framework is beyond the scope of the question, you can create a new question about this. - bobzer
    • You did not understand, I mean that no exceptions do not take off, it simply transfers to the page with the error ERR_CONNECTION_ABORTED. I added all this in the question - Roman
    • The exception is thrown, I do not know why you can not catch it. Completed the answer for Tomcat 7. If this does not work either - take a "clean" Tomcat, deploy an empty application with a servlet alone, without any other classes and libraries, and test. - bobzer

    If the file size is larger than this, an IllegalStateException will be IllegalStateException .

     try { parts = request.getPart("photo"); } catch (IllegalStateException e) { // что-нибудь } 
    • What's wrong? The answer is wrong? - Nick
    • I did not evaluate, but throws only the error err_aborted, this exception does not exist - Roman
    • A voice against the system ( community spirit ). Try to write more detailed answers. - Nicolas Chabanovsky