Error in file upload to Java Controller using Angular

Asked

Viewed 263 times

0

Opa,

I am implementing a file upload, in a form, I can already select the file, pass to Angularjs that recognizes it (even without ng-model) and when I pass to my java controller gives the following error:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: The current request is not a multipart request
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
br.com.kolss.web.security.AccessFilter.doFilter(AccessFilter.java:55)
root cause

org.springframework.web.multipart.MultipartException: The current request is not a multipart request
org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.assertIsMultipartRequest(RequestParamMethodArgumentResolver.java:216)
org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.resolveName(RequestParamMethodArgumentResolver.java:167)
org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:88)
org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
br.com.kolss.web.security.AccessFilter.doFilter(AccessFilter.java:55)
note The full stack trace of the root cause is available in the JBoss     Web/7.0.13.Final logs.

My html form:

                       <form name="form" id="form_sample_2" role="form" method="post"
                           class="form-horizontal ng-pristine ng-valid" enctype="multipart/form-data" novalidate>  

                           <div class="form-group">
                                  <label class="control-label col-md-3">Upload Certificado:</label>
                                    <div class="col-md-9">                                       
                                      <span class="button"><input type="file" name="file" onchange="angular.element(this).scope().uploadFile(this.files)"/>
                                   </div>                                                                     
                           </div>                                                         
                       </form>

My . js:

$scope.uploadFile = function(files) {
    var fd = new FormData();
    //Take the first selected file
    fd.append("file", files[0]);

    $http.post('cadastrocertificado/uploadCertificado', fd).then(function(response) {
    }, function(response) { 
        console.log("erro") 
    });

};

Java controller method:

@RequestMapping(value = "/cadastrocertificado/uploadCertificado", method = RequestMethod.POST)
public String uploadCertificado(@RequestParam("file") MultipartFile file) throws IOException, ServiceException{
        if(!file.isEmpty()) {

            log.info("Nome arquivo: " + file.getName());
            log.info("Tamanho: " + file.getSize());
            log.info("só quero cair aqui... ");

        }       

}

Any help is valid. Thank you !

  • 1

    Post the method that receives the file.

  • Aff, the main kkk, sorry, ready !

  • 1

    @Leonardoleonardo see if my change in the name became good and better defines the problem. Any.

  • Diego, thank you. I agree with you. I just don’t leave it so abstract because I don’t do searches this way you know, I get key words and so connect better the results. But thanks.

  • @Leonardoleonardo is because here on the site you can add the technology, or language as tags, and leave the abstract title even, so the person can search for the problem, and the tag is who will show her what language/framework/technology is.

  • I understood Diego, I usually play on google, rs, I find more effective not filter only from here. VLW

Show 1 more comment

1 answer

0


Oops, I got it... I added the headers to the requisition:

$scope.uploadFile = function(files) {
    var fd = new FormData();
    //Take the first selected file
    fd.append("file", files[0]);

    $http.post('cadastrocertificado/uploadCertificado', fd, {
        withCredentials: true,
        headers: {'Content-Type': undefined }}).then(function(response) {

    }, function(response) { 
        console.log("erro") 
    });

};

And this bean in applicationContext.xml:

<beans>
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="50000000"/>
    </bean>
</beans>

Then hit the legal controller.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.