Spring MVC ajax文件上传错误

时间:2016-01-20 10:19:28

标签: java ajax spring-mvc

我有一个Spring MVC REST控制器来处理.zip文件上传,比如

 @RequestMapping(value = "/browser/file", method = RequestMethod.POST)
 public String getUpload(@RequestParam("file") MultipartFile file, @RequestParam("path") String path) {
     //do something
 }

我将此配置用于MultipartFile:

@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver filterMultipartResolver = new CommonsMultipartResolver();
    filterMultipartResolver.setMaxUploadSize(17000000);
    return filterMultipartResolver;
}

我的HTML上传表单是

<form enctype="multipart/form-data" action="/rest/visualization/browser/file" id="hdfsLoader">
    <span class="pull-right">
        <input name="file" class="filestyle" id="zipUpload" data-input="false" type="file" data-buttonText="Upload" data-classButton="btn btn-primary" data-classIcon="icon-plus">
    </span>
</form>

我使用ajax POST请求发送数据:

var data = new FormData();
data.append( "file", $('#zipUpload')[0].files[0]);
var path = $('#hdfs_path').val(); //some external value
$.ajax({
    type: 'POST',
    url: form.attr("action") + "?path=" + path,
    data: data,
    processData: false,
    contentType: false,
    cache: false,
    success: (function (data) {
        alert('Success');
    })
});

我在3个不同的.zip文件上测试了这段代码。 在 15.8 MB -size文件上,一切正常。

9.72 MB 上,我有org.apache.commons.fileupload.FileUploadException: Stream closed

701 KB 上,它是DefaultHandlerExceptionResolver:186 - Handler execution resulted in exception: Required MultipartFile parameter 'file' is not present。 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我删除了我的MultipartFile bean并将这3行添加到我的application.properties中,这解决了我的问题:

multipart.enabled=true
multipart.max-file-size=-1
multipart.max-request-size=-1
相关问题