0

I have a web app with such config. Please look at multipartResolver bean.

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.websystique.springmvc")
public class HelloWorldConfiguration extends WebMvcConfigurerAdapter{

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        registry.viewResolver(viewResolver);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
    }


    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver multipartResolver() {

        final CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(100000);

        return multipartResolver;
    }
}

also I have controller, which should accept file from client

 @RequestMapping(value = "/file/", method = RequestMethod.POST)
    public ResponseEntity<Void> createUser(HttpServletRequest request) throws IOException {

        System.out.println("!!!");
        MultipartHttpServletRequest mRequest =
                (MultipartHttpServletRequest) request; // Exception is here
        //some code
    }

I send file in this jsp snippet

<input type="file" id="file" name="file" accept=".xls,.xlsx" enctype="multipart/form-data" />
          <button ng-click="ctrl.add()">Add</button>

by this angular code

function functionAdd() {
        var f = document.getElementById('file').files[0];
        var fd = new FormData();

        fd.append("file", f);
        return $http.post(REST_SERVICE_URI1, fd);
    }

I've got this stacktrace.

java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
    at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:410)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:383)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

Maybe somebody can see a mistake.

Alex
  • 2,343
  • 3
  • 16
  • 35

1 Answers1

1

How to POST FormData Using the $http Service

When using the FormData API to POST files and data, it is important to set the Content-Type header to undefined.

var fd = new FormData()
for (var i in $scope.files) {
    fd.append("fileToUpload", $scope.files[i]);
}
var config = {headers: {'Content-Type': undefined}};

var httpPromise = $http.post(url, fd, config);

By default the AngularJS framework uses content type application/json. By setting Content-Type: undefined, the AngularJS framework omits the content type header allowing the XHR API to set the content type. When sending a FormData object, the XHR API sets the content type to multipart/form-data with the proper boundaries and base64 encoding.

For more information, see MDN Web API Reference - XHR Send method


Can you explain why it isn't proper to use multipart/form-data? instead of undefined.

multipart/form-data without the proper boundaries causes problems. The XHR send() API automatically sets the boundary and includes it with each part.

By setting Content-Type: undefined, the AngularJS framework omits the content type header allowing the XHR API to set the content type. When sending a FormData object, the XHR API sets the content type to multipart/form-data with the proper boundaries and base64 encoding.

For more information, see What is the boundary in multipart/form-data?

georgeawg
  • 46,994
  • 13
  • 63
  • 85
  • I can consider this as a right answer. I've already done something similar and it works for me! thanks – Alex Dec 26 '17 at 19:39
  • Can you explain why it isn't proper to use `multipart/form-data`? instead of `undefined`. – Alex Dec 26 '17 at 19:50
  • `multipart/form-data` without the proper boundaries causes problems. The [XHR send() API](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send) automatically sets the boundary and includes it with each part. For more information, see [What is the boundary in multipart/form-data?](https://stackoverflow.com/questions/3508338/what-is-the-boundary-in-multipart-form-data). – georgeawg Dec 26 '17 at 20:00