HTTP Status 400 - Required CommonsMultipartFile[] parameter 'XXX' is not present
程序员文章站
2022-07-15 13:09:21
...
<!DOCTYPE html>
<html>
<head>
<title>Apache Tomcat/9.0.0.M17 - Error report</title>
<style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style>
</head>
<body>
<h1>HTTP Status 400 - Required CommonsMultipartFile[] parameter 'files' is not present</h1>
<div class="line"></div>
<p>
<b>type</b> Status report
</p>
<p>
<b>message</b>
<u>Required CommonsMultipartFile[] parameter 'files' is not present</u>
</p>
<p>
<b>description</b>
<u>The request sent by the client was syntactically incorrect.</u>
</p>
<hr class="line">
<h3>Apache Tomcat/9.0.0.M17</h3>
</body>
</html>
问题详细描述:文件上传的时候在未选择文件直接提交form的时候,报出"HTTP Status 400 - Required CommonsMultipartFile[] parameter 'XXX' is not present"的问题,这里的问题就是在form当中存在file,而file没有选择文件的情况。
问题原因:问题主要原因是在使用Google浏览器上传的时候如果file为空Spring的controller层也会接收到一个CommonsMultipartFile[]的length为0的实例化的CommonsMultipartFile[],而在IE、Edge、世界之窗返回在Spring的Controller当中的CommonsMultipartFile[]接收到的是null值,如果方法中的参数:
解决方案:
@PostMapping({"affairsMod.do", "affairsWrite.do"}){}
@RequestParam("files") CommonsMultipartFile[] files
////如果这个样设置要求files这个参数是不能为空的,因此就会抛出这个错误,因此需要修改成如下:
@PostMapping({"affairsMod.do", "affairsWrite.do"})
public JsonBaseResult affairsWriteAndMod(@RequestParam(value = "files",required = false) CommonsMultipartFile[] files) {}