3

I want to upload files with a restservice. I found very much in the web, but still can't fix my problem. Maybe because I'am using Spring in the Rest service?! Here is my code:

HTML

<input type="button" value="Upload document" id="button">
<input id="fileToUpload" type="file">

JQUERY

jQuery("document").ready(function () {
    $("#button").on("click", function () {

          var fileInput= $('input[name="fileInput"]')[0].files[0]; 
          var data = new FormData();
          data.append('file', fileInput);
          $.ajax({
            url: '.../upload',
            type: 'POST',
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            success: function(data){
                alert("successfully.");
            },
            error: function(data){
                alert("failed.");
            }
          });
    });
});

JAVA (RESTSERVICE)

@RequestMapping(value = "/upload", method = { RequestMethod.POST }, produces = { MediaType.MULTIPART_FORM_DATA_VALUE,
        MediaType.APPLICATION_FORM_URLENCODED_VALUE, MediaType.APPLICATION_JSON_VALUE })
public void uploadingFile(@RequestBody InputStream uploadedInputStream) throws IOException {
    System.out.println("uploadedInputStream: " + uploadedInputStream);
}

EXCEPTION

 Servlet.service() for servlet REST API Dispatcher threw exception: java.lang.IllegalStateException: Parameters processing failed.

Is my Rest service ok? Can't find my bug :(

POM.XML

<!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>

APPLICATION-CONTEXT.XML

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">


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


SOLUTION

JAVA (RESTSERVICE)

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
public void uploadingFile(@RequestParam("file") MultipartFile uploadedInputStream) throws IOException {
    System.out.println("...the parameter binding works now");
}

JAVA (SPRING CONFIG)

@Bean
public static CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver cmr = new CommonsMultipartResolver();
    cmr.setMaxUploadSize(50000000);
    return cmr;
}

(ALTERNATIVE - APPLICATION.XML)

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="50000000" />
   </bean>
Louisa
  • 55
  • 10
  • Check http://stackoverflow.com/questions/25884711/spring-file-upload-restful-web-service – seenukarthi Mar 17 '16 at 10:07
  • Well, I change the parameter type in rest service to "MultipartFile" instead "InputStream" and in jQuery the content type to "multipart/form-data" instead of "application/x-www-form-urlencoded". The exception here is: "java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?" . Adding "" to application.xml didn't solve it... – Louisa Mar 17 '16 at 10:21
  • you can set contentType property to false. – nnunes10 Mar 17 '16 at 10:32
  • thx I change it ... still have the same problem "java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?" – Louisa Mar 17 '16 at 11:05

1 Answers1

2

Your handler method should look like bellow:

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void uploadingFile(MultipartHttpServletRequest request, HttpServletResponse response) throws IOException {

    Iterator<String> itr =  request.getFileNames();

    MultipartFile file = request.getFile(itr.next());
    System.out.println(file.getOriginalFilename() +" uploaded!");
}

Don't forget to add the following lines in your application context:

<mvc:annotation-driven />

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="1048576"/>
</bean>
nnunes10
  • 550
  • 4
  • 14
  • I change my method - but now I have the next Excption: "Expected MultipartHttpServletRequest: is a MultipartResolver configured?". In my application.xml I add: "" – Louisa Mar 17 '16 at 10:37
  • I adding both (in application-context and the library) but still the same error.... do you have another great idea? ;) – Louisa Mar 17 '16 at 10:47
  • it should work.. could you please add your application-context ? – nnunes10 Mar 17 '16 at 11:30
  • and if you are using Maven, please put your pom.xml. – nnunes10 Mar 17 '16 at 11:49
  • Furthermore, you need commons-io.jar ;) could you please try it? – nnunes10 Mar 17 '16 at 11:58
  • above you can see the application-context and an extract of the pom.xml. commons-io.jar is inside. – Louisa Mar 17 '16 at 12:20
  • Solve it with your help! The final error was that the logic of the application-context.xml is store in a Java-Class and not in the xml by itsself. – Louisa Mar 17 '16 at 12:58