Spring无法识别Multipartfile

时间:2015-10-15 12:31:45

标签: spring

使用spring上传文件

WARN  DefaultHandlerExceptionResolver - Handler execution resulted in exception: Required MultipartF
ile parameter 'file' is not present

上传控制器

@RequestMapping(value="/upload", 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.";
        }
    }

的Javascript

var form = document.getElementById('the-form');
            form.onsubmit = function() {
                var formData = new FormData(form);

                formData.append('name', "asdasdasd");
                formData.append('file', file);

                var xhr = new XMLHttpRequest();
                // Add any event handlers here...
                xhr.open('POST', form.getAttribute('action'), true);
                xhr.send(formData);

                return false; // To avoid actual submission of the form
            }

应用程序上下文

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- one of the properties available; the maximum file size in bytes -->
        <property name="maxUploadSize" value="8000000"/>
    </bean>

当我发送参数字符串,整数等时工作正常。但是打破多部分文件。

任何想法可能出错?

EDIT 这是表格

<form id="the-form" method="post" action="/upload" enctype="multipart/form-data">
                <input name="file" type="file">
                <input type="submit" value="Upload" />
            </form>

文件变量

lastModified: 1444832093455
lastModifiedDate: Wed Oct 14 2015 17:14:53 GMT+0300 (FLE Daylight Time)
name: "file"
size: 89
type: ""
webkitRelativePath: ""

0 个答案:

没有答案