File upload error for Java Controller using Angular

0

Opa,

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

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 form html:

                       <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. Thanks!

    
asked by anonymous 11.02.2016 / 13:46

1 answer

0

Oops, I got it ... I added the headers in the request:

$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 he hit the legal controller.

    
11.02.2016 / 14:13