java - Spring MVC - 上传文件显示客户端发送的请求在语法上不正确

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

当我使用表单提交上传包含其他详细信息的文件时,它显示错误 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>

文件 Controller .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> 

最佳答案

改变

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

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

关于java - Spring MVC - 上传文件显示客户端发送的请求在语法上不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30317902/

相关文章:

java - 我应该如何在 Spring MVC 中创建 Groovy Controller

Spring:在@RequestParam 中类型不匹配时处理错误

php - 使用 XMLHttpRequest 上传文件

java - 无法在项目 mongo-demo 上执行目标 io.thorntail :thorntail-maven-plugin:2. 2.0.Final:package (default)

java - 最佳实践问题 : How to save a collection of images and a java object in a single file? 读取文件以进行呈现

spring-mvc - Spring MVC : How to load @PropertySource from WEB-INF

php - SpreadsheetReader 对象创建无一异常(exception)地失败

java - 如何使用GWT文件上传?

java - eclipselink 刷新其共享缓存需要多长时间

java - 将 Hashmap 恢复为之前的值