我正在尝试上传文件,但是却收到Required MultipartFile parameter 'file' is not present
400
Bad Request
org.springframework.web.bind.MissingServletRequestParameterException
。
我的代码在这里:
<form id="uploadrecords" method="post" enctype="multipart/form-data">
<div class="col-md-12">
<div class="form-group" style="margin-top: 11px !important;">
<label for="uploadfile" class="col-md-4 control-label"><span class="mandatory"></span>File to upload:</label>
<div class="col-md-6">
<input type="file" class="filestyle" data-buttonName="btn-primary" name="file" id="upload" accept="*"/>
</div>
</div>
<div class="form-actions" style="margin-top: 51px;margin-left: 358px;">
<button type="submit" class="btn btn-success" style="padding: 6px 12px;" id="upload" ><i class="fa fa-cloud-upload"></i> Upload</button>
<button type="button" class="btn btn-danger" style="padding: 6px 12px;" id="cancel" ><i class="fa fa-ban"></i> Cancel</button>
</div>
</div>
</form>
jQuery代码在这里:
$("form#uploadrecords").submit(function(){
var formData = new FormData($(this)[0]);
$.ajax({
url : '/uploadfile',
type: 'POST',
data: formData,
async: false,
beforeSend: beforeSendHandler,
success: function (data) {
var msg=data.msg;
var obj=data.obj;
if(obj != 0){
$("#countfail").html(obj);
$("#download-modal").modal('show');
}else{
bootbox.alert(msg);
}
},
cache: false,
contentType: false,
processData: false
});
});
Java代码:
@RequestMapping(value = "/uploadfile", headers = "Content-Type=multipart/form-data", method = RequestMethod.POST)
public @ResponseBody StatusResponse upload( @RequestParam(value = "file") MultipartFile file, HttpServletRequest request,
HttpServletResponse response) throws IOException, NoSuchFieldException, SecurityException, ParseException {
StatusResponse sr = new StatusResponse();
System.out.println("filename::::" + file.getOriginalFilename());
//my logic here
return sr;
}
它没有达到控制器类。我在
Firebug console
中发现错误。我正在使用
spring boot
和jboss-eap-6.4
。我的代码有什么问题。如何解决这个问题呢?
最佳答案
如果您使用@EnableAutoConfiguration,则需要按照此处的说明进行以下操作https://github.com/spring-projects/spring-boot/issues/2958
@EnableAutoConfiguration(排除= {MultipartAutoConfiguration.class})
定义以下豆
@Bean(name =“ multipartResolver”)public CommonsMultipartResolver commonsMultipartResolver(){CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setMaxUploadSize(50 * 1024 * 1024);返回解析器; }
@Bean @Order(0)public MultipartFilter multipartFilter(){MultipartFilter multipartFilter = new MultipartFilter(); multipartFilter.setMultipartResolverBeanName(“ multipartResolver”);返回multipartFilter; }
关于java - 上传文件时,不存在必需的MultipartFile参数'file',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46251875/