-1

I am having problems when i try to send a great file. When the file is small there is not problem.

Controller

@SuppressWarnings({ "unchecked", "rawtypes" })
    @RequestMapping(value = "newFile", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject newFile(final HttpSession session, final HttpServletResponse response,
            @RequestPart("document") final DocumentAjusteDTO dto, @RequestPart("file") final MultipartFile file)
            throws IOException {

js

    formDoc.append('file', (file.files[0]));
            var documentData = {
                tipo: tipo,
                periodo: periodo
            };
            formDoc.append('document', new Blob([JSON.stringify(documentData)], {
                type: "application/json"
            }));
var ajaxData = {
            data: formDoc,
            sendMethod: 'POST',
            target: 'new_file',
            properties: {
                contentType: false,
                processData: false
            },
            callbackSuccess: function (e) {
                ....
            }
        };
        Ajax.call(ajaxData);
    }

};

when the file is< 5mb ok. file > 5mb ko

this is tomcat log

HandlerMethod details:
Controller [com.isb.hpa.ajustes.web.controllers.AjustesController]
Method [public org.jose4j.json.internal.json_simple.JSONObject com.isb.hpa.ajustes.web.controllers.AjustesController.newFile(javax.servlet.http.HttpSession,javax.servlet.http.HttpServletResponse,com.isb.hpa.logic.rest.beans.DocumentAjusteDTO,org.springframework.web.multipart.MultipartFile) throws java.io.IOException]

org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'document' is not present
        at ....
mantamusica
  • 220
  • 2
  • 11

1 Answers1

0

Your Exception says that there is no 'document' in your request. You can add required = false to your argument in case your request contains only MultipartFile:

@RequestPart("document",  required = false) final DocumentAjusteDTO dto

Add these properties to your application to avoid file size errors:

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=12MB

Change 10 and 12 to bigger numbers if your files are bigger in size.

Also, add the following header to your Ajax request as said here.

headers: {
         "Content-Type": undefined
  },

Content-Type: undefined. This causes the browser to set the Content-Type to multipart/form-data and fill the boundary correctly. Manually setting Content-Type to multipart/form-data will fail to fill in the boundary parameter of the request.

  • I need the document, i only can prove with application.properties, when i work in local this problem doesn´t exist. when I try to upload it to the server, the problem appears – mantamusica Aug 18 '20 at 11:54
  • ah, maybe that's because your endpoints don't match, your server wait requests from @RequestMapping(value = **"newFile"**, ...) you send requests to target: **'new_file'**, change Mapping value to **new_file**, if it wouldn't solve your problem please provide stacktrace – Edgar Khachatryan Aug 18 '20 at 15:10
  • i have a config ajax.js where i transforme de newFile in new_file. – mantamusica Aug 20 '20 at 09:33