spring mvc 上传文件和表单时报400错误

bug复现

代码

这是html的代码,用的是所有的设置看起来都是正确的。

stuId:
detailId:
remarks:
picture:

这是控制层的代码

    @RequestMapping(value = "/both", method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String both(MultipartFile picture, ApplyScore applyScore) {
        return applyScore.getStuId() + ", " + picture.getOriginalFilename();
    }

显示情况

打开浏览器进入html页面中后是这样子的

spring mvc 上传文件和表单时报400错误_第1张图片

点击提交后报400错误,也就是下图

spring mvc 上传文件和表单时报400错误_第2张图片

问题原因

经过多方资料查证,发现问题出在了文件的变量名上,file的name不可以是picture,我将所有的picture改成了myfile之后再次提交就成功了。

以下是修改过之后的代码。

stuId:
detailId:
remarks:
picture:
    @RequestMapping(value = "/both", method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String both(MultipartFile myfile, ApplyScore applyScore) {
        return applyScore.getStuId() + ", " + myfile.getOriginalFilename();
    }

以下是正确的程序运行截图

spring mvc 上传文件和表单时报400错误_第3张图片

spring mvc 上传文件和表单时报400错误_第4张图片

总结

变量名尽量取的不容易和程序的一些关键词重复,否则容易出现错误。

 

 

你可能感兴趣的:(spring,SpringMvc)