【问题标题】:Spring MVC - upload file shows The request sent by the client was syntactically incorrectSpring MVC - 上传文件显示客户端发送的请求语法不正确
【发布时间】:2015-05-19 06:07:08
【问题描述】:

当我使用表单 sumbmition 上传带有其他详细信息的文件时,它显示错误 HTTP 状态 400 - 类型状态报告 信息 描述客户端发送的请求语法错误。

jsp页面

         <form:form method="POST" action="addbanners?${_csrf.parameterName}=${_csrf.token}" modelAttribute="banner" enctype="multipart/form-data">
    <h2>New Banner</h2>
    <table>
    <tr><td>Banner Name</td>
    <td><form:input type="text" name="thematicdayid" id="thematicdayid" path="bannerName" /></td></tr>
    <tr><td>Banner Image</td>
    <td><form:input name="uploadBanner" type="file" id="uploadBanner" path="bannerImage"/></td></tr>
    <tr><td> <button type="submit" class="btn btn-success" id="btnAddBanner">Add</button></td></tr>
    </table>
    </form:form>

文件控制器.java

    @RequestMapping(value = "/addbanners", method = RequestMethod.POST)
public ModelAndView addbanners(@ModelAttribute Banner banner,@RequestParam("file") MultipartFile file){

    System.out.println("File"+file);
    ImageUpload imageUpload=new ImageUpload();
    if(!file.isEmpty())
    {
    String path=context.getRealPath("");
    String imagename=imageUpload.uploadImage(file,path);
    banner.setBannerImage(imagename);

    filewriterServices.saveBannerDetails(banner);
    }
    ModelAndView model = new ModelAndView();
    model.setViewName("filepage");
    return model;
}

我在 root-context.xml 中添加以下详细信息

<beans:bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <beans:property name="maxUploadSize" value="100000" />
</beans:bean> 

【问题讨论】:

    标签: java spring-mvc file-upload spring-security


    【解决方案1】:

    改变

    public ModelAndView addbanners(@ModelAttribute Banner banner,@RequestParam("file") MultipartFile file)
    

    public ModelAndView addbanners(@ModelAttribute Banner banner,@RequestParam("uploadBanner") MultipartFile file)
    

    【讨论】:

      【解决方案2】:

      我通过改变得到了答案

      public ModelAndView addbanners(@ModelAttribute Banner banner,@RequestParam("file") MultipartFile file) to
      
      public ModelAndView addbanners(@ModelAttribute Banner banner,@RequestParam("uploadBanner") MultipartFile file)
      

      改变

      <form:input name="uploadBanner" type="file" id="uploadBanner" path="bannerImage"/> to 
      <input name="uploadBanner" type="file" id="uploadBanner" path="bannerImage"/> 
      

      感谢@Akash Rajbanshi 帮助我找到我的错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-21
        • 1970-01-01
        • 2013-01-12
        • 1970-01-01
        • 2018-01-08
        • 2015-10-31
        相关资源
        最近更新 更多