我有一个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大小的文件上,一切正常。
在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
。我该如何解决?
我已经删除了MultipartFile的bean,并将这3行添加到了application.properties中,这解决了我的问题:
multipart.enabled=true
multipart.max-file-size=-1
multipart.max-request-size=-1
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句