简体   繁体   中英

Spring MVC parsing multipart content request

I`m going right on this tutorial https://spring.io/guides/gs/uploading-files/ and implemented a multipart file upload controller.

However, stuck on getting this error:

type Exception report

message Request processing failed; nested exception is java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?

description The server encountered an internal error that prevented it from fulfilling this request.

Here is my code for controller , just right from official tutorial:

    package com.springapp.mvc;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.multipart.MultipartFile;

    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;

@Controller
public class FileUploadController {
        @RequestMapping(value="/api/items/upload_image", method=RequestMethod.POST)
        public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
                                                     @RequestParam("file") MultipartFile file){
            if (!file.isEmpty()) {
                try {
                    byte[] bytes = file.getBytes();
                    BufferedOutputStream stream =
                            new BufferedOutputStream(new FileOutputStream(new File(name)));
                    stream.write(bytes);
                    stream.close();
                    return "You successfully uploaded " + name + "!";
                } catch (Exception e) {
                    return "You failed to upload " + name + " => " + e.getMessage();
                }
            } else {
                return "You failed to upload " + name + " because the file was empty.";
            }
        }
}

And just for additional info, this is how i send request from front-end on angularjs. Request senging successfully:

    Upload.upload({
        url: '/api/items/upload_image',
        file: $file,
        name: $file.name,
        progress: function(e){}
    }).then(function(data, status, headers, config) {
        // file is uploaded successfully
        console.log(data);
    });

What i the problem can be? All needed dependencies included.

This was a solution : include this dependencies in pom.xml

<!-- Apache Commons FileUpload -->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

<!-- Apache Commons IO -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

Add this bean in configuration:

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- setting maximum upload size -->
    <property name="maxUploadSize" value="100000" />
</bean>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM