Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest

Ask Question @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.

it works properly ` var REST_SERVICE_URI1 = 'localhost:8080/Spring4MVCAngularJSExample/file/` with enitial mapping on /Spring4MVCAngularJSExample – Alex Dec 26, 2017 at 14:06

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

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?

I can consider this as a right answer. I've already done something similar and it works for me! thanks – Alex Dec 26, 2017 at 19:39 multipart/form-data without the proper boundaries causes problems. The XHR send() API automatically sets the boundary and includes it with each part. For more information, see What is the boundary in multipart/form-data?. – georgeawg Dec 26, 2017 at 20:00

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.