Required request part 'file' is not present

SpringMVC Bug(一)

環(huán)境:springmvc-4.x

? tomcat7.9

? idea 2017

? jdk 1.8

錯誤提示:Required request part 'file' is not present

  • 遇到這種問題一般情況下都是參數(shù)未對應上,但是在我本次處的問題卻并不是這么一回事

前端代碼

  <form action="testFileUpload" method="post" enctype="multipart/form-data">
    file:<input type="file" name="file">
    desc:<input type="text" name="desc">
    <input type="submit" value="submit">
  </form>

配置代碼

<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="maxUploadSize" value="10240000"></property>
 </bean>

在這一步不知道大家有木有發(fā)現(xiàn)問題:

接下來看后端代碼

    public String testFileUpload(@RequestParam(value = "file") MultipartFile file,@RequestParam(value = "desc") String desc)
    {
        System.out.println("desc"+desc);
        System.out.println(file.getOriginalFilename());
        return "success";
    }

這就是全部邏輯代碼,運行出的錯就是

Required request part 'file' is not present

在一番Debug之后找到了錯誤原因,是配置文件出了問題:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="maxUploadSize" value="10240000"></property>
 </bean>

這樣之后就解決了問題,

但是這是為什么呢?


1550908155055.png

因為dispatcher會自行調用名為multipartResolver的實例,如果沒有加iddispatcher就不知道該調用誰了。

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容